Một số tác vụ truy xuất hoặc xử lý dữ liệu do ứng dụng của bạn thực hiện có thể tốn nhiều CPU hoặc mất vài giây để hoàn thành. Trong trường hợp này, dữ liệu đã truy xuất được lưu vào bộ đệm ẩn trong một thời gian để có thể truy xuất nhanh dữ liệu này trong các yêu cầu tiếp theo cho cùng một dữ liệu. Dữ liệu được lưu trong bộ nhớ cache thường được lưu trữ trong kho lưu trữ dữ liệu rất nhanh như Memcached hoặc Redis
Rất may, Laravel cung cấp một API thống nhất, rõ ràng cho nhiều phụ trợ bộ đệm khác nhau, cho phép bạn tận dụng khả năng truy xuất dữ liệu cực nhanh của chúng và tăng tốc ứng dụng web của bạn
Cấu hình
Tệp cấu hình bộ đệm của ứng dụng của bạn được đặt tại
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
5. Trong tệp này, bạn có thể chỉ định trình điều khiển bộ đệm nào bạn muốn được sử dụng theo mặc định trong toàn bộ ứng dụng của mình. Laravel hỗ trợ các phụ trợ bộ nhớ đệm phổ biến như Memcached, Redis, DynamoDB và cơ sở dữ liệu quan hệ ngay lập tức. Ngoài ra, có sẵn trình điều khiển bộ đệm dựa trên tệp, trong khi trình điều khiển bộ đệm "null" và namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
6 cung cấp phụ trợ bộ đệm thuận tiện cho các thử nghiệm tự động của bạnTệp cấu hình bộ đệm cũng chứa nhiều tùy chọn khác, được ghi lại trong tệp, vì vậy hãy đảm bảo đọc qua các tùy chọn này. Theo mặc định, Laravel được định cấu hình để sử dụng trình điều khiển bộ đệm
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
7, trình điều khiển lưu trữ các đối tượng được lưu trong bộ nhớ cache, được tuần tự hóa trên hệ thống tệp của máy chủ. Đối với các ứng dụng lớn hơn, bạn nên sử dụng trình điều khiển mạnh hơn như Memcached hoặc Redis. Bạn thậm chí có thể định cấu hình nhiều cấu hình bộ đệm cho cùng một trình điều khiểnĐiều kiện tiên quyết của trình điều khiển
cơ sở dữ liệu
Khi sử dụng trình điều khiển bộ đệm
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
8, bạn sẽ cần thiết lập một bảng để chứa các mục bộ đệm. Bạn sẽ tìm thấy một ví dụ khai báo namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
9 cho bảng bên dướiSchema::create['cache', function [$table] {
$table->string['key']->unique[];
$table->integer['expiration'];
Lưu ý
Bạn cũng có thể sử dụng lệnh Artisan0 để tạo di chuyển với lược đồ phù hợp.
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
Memcached
Sử dụng trình điều khiển Memcached yêu cầu phải cài đặt gói Memcached PECL. Bạn có thể liệt kê tất cả các máy chủ Memcached của mình trong tệp cấu hình
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
5. Tệp này đã chứa mục nhập $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
2 để giúp bạn bắt đầu 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
Nếu cần, bạn có thể đặt tùy chọn
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
3 thành đường dẫn ổ cắm UNIX. Nếu bạn làm điều này, tùy chọn $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
4 sẽ được đặt thành $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
5namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
3làm lại
Trước khi sử dụng bộ đệm Redis với Laravel, bạn sẽ cần cài đặt tiện ích mở rộng PhpRedis PHP qua PECL hoặc cài đặt gói
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
6 [~1. 0] qua Nhà soạn nhạc. Laravel Sail đã bao gồm tiện ích mở rộng này. Ngoài ra, các nền tảng triển khai Laravel chính thức như Laravel Forge và Laravel Vapor đều được cài đặt tiện ích mở rộng PhpRedis theo mặc địnhĐể biết thêm thông tin về cách định cấu hình Redis, hãy tham khảo trang tài liệu Laravel của nó
DynamoDB
Trước khi sử dụng trình điều khiển bộ đệm DynamoDB, bạn phải tạo một bảng DynamoDB để lưu trữ tất cả dữ liệu được lưu trong bộ đệm. Thông thường, bảng này nên được đặt tên là
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7. Tuy nhiên, bạn nên đặt tên bảng dựa trên giá trị của giá trị cấu hình $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
8 trong tệp cấu hình $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 của ứng dụng của bạnBảng này cũng phải có khóa phân vùng chuỗi có tên tương ứng với giá trị của mục cấu hình
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
80 trong tệp cấu hình $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 của ứng dụng của bạn. Theo mặc định, khóa phân vùng phải được đặt tên là namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
82Sử dụng bộ đệm
Lấy một phiên bản bộ đệm
Để có được một phiên bản lưu trữ bộ đệm, bạn có thể sử dụng mặt tiền
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83, đây là những gì chúng tôi sẽ sử dụng trong suốt tài liệu này. Mặt tiền namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83 cung cấp quyền truy cập ngắn gọn, thuận tiện vào các triển khai cơ bản của hợp đồng bộ đệm Laravelnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
Truy cập nhiều cửa hàng bộ đệm
Sử dụng mặt tiền
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83, bạn có thể truy cập các kho lưu trữ bộ đệm khác nhau thông qua phương pháp namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
86. Khóa được truyền cho phương thức namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
86 phải tương ứng với một trong các cửa hàng được liệt kê trong mảng cấu hình namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
88 trong tệp cấu hình $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 của bạn$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
Truy xuất các mục từ bộ đệm
Phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11 của mặt tiền namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83 được sử dụng để truy xuất các mục từ bộ đệm. Nếu mục không tồn tại trong bộ nhớ cache, namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
12 sẽ được trả lại. Nếu muốn, bạn có thể chuyển đối số thứ hai cho phương thức namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11 chỉ định giá trị mặc định mà bạn muốn được trả về nếu mục không tồn tạinamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
8Bạn thậm chí có thể chuyển một bao đóng làm giá trị mặc định. Kết quả của việc đóng sẽ được trả về nếu mục được chỉ định không tồn tại trong bộ đệm. Chuyển một bao đóng cho phép bạn trì hoãn việc truy xuất các giá trị mặc định từ cơ sở dữ liệu hoặc dịch vụ bên ngoài khác
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
1Kiểm tra sự tồn tại của vật phẩm
Phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
14 có thể được sử dụng để xác định xem một mục có tồn tại trong bộ nhớ cache hay không. Phương thức này cũng sẽ trả về namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
15 nếu mục tồn tại nhưng giá trị của nó là namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
12Giá trị tăng/giảm
Các phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
17 và namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
18 có thể được sử dụng để điều chỉnh giá trị của các mục số nguyên trong bộ đệm. Cả hai phương thức này đều chấp nhận một đối số thứ hai tùy chọn cho biết số lượng để tăng hoặc giảm giá trị của mụcnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
1Truy xuất và lưu trữ
Đôi khi, bạn có thể muốn truy xuất một mục từ bộ đệm, nhưng cũng lưu trữ một giá trị mặc định nếu mục được yêu cầu không tồn tại. Ví dụ: bạn có thể muốn truy xuất tất cả người dùng từ bộ đệm hoặc nếu họ không tồn tại, hãy truy xuất họ từ cơ sở dữ liệu và thêm họ vào bộ đệm. Bạn có thể làm điều này bằng cách sử dụng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
19namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
3Nếu mục không tồn tại trong bộ đệm, lệnh đóng được truyền cho phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
10 sẽ được thực thi và kết quả của nó sẽ được đặt trong bộ đệmBạn có thể sử dụng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11 để lấy một mục từ bộ đệm hoặc lưu trữ mãi mãi nếu mục đó không tồn tạinamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
6Truy xuất và Xóa
Nếu bạn cần truy xuất một mục từ bộ đệm và sau đó xóa mục đó, bạn có thể sử dụng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
12. Giống như phương thức namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11, namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
12 sẽ được trả về nếu item không tồn tại trong cache 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
0Lưu trữ các mục trong bộ đệm
Bạn có thể sử dụng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
15 trên mặt tiền namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83 để lưu trữ các mục trong bộ đệm 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
1Nếu thời gian lưu trữ không được chuyển sang phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
15, vật phẩm sẽ được lưu trữ vô thời hạn 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
2Thay vì chuyển số giây dưới dạng số nguyên, bạn cũng có thể chuyển một phiên bản
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
18 biểu thị thời gian hết hạn mong muốn của mục được lưu trong bộ nhớ cache 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
3Lưu trữ nếu không có
Phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
19 sẽ chỉ thêm mục vào bộ đệm nếu nó chưa tồn tại trong kho lưu trữ bộ đệm. Phương thức sẽ trả về namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
30 nếu mục đó thực sự được thêm vào bộ đệm. Nếu không, phương thức sẽ trả về namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
15. Phương pháp namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
19 là một hoạt động nguyên tử 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
4Lưu trữ vật phẩm mãi mãi
Phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
33 có thể được sử dụng để lưu trữ vĩnh viễn một mục trong bộ đệm. Vì các mục này sẽ không hết hạn nên chúng phải được xóa thủ công khỏi bộ đệm bằng phương pháp namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
34 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
5Lưu ý
Nếu bạn đang sử dụng trình điều khiển Memcached, các mục được lưu trữ "mãi mãi" có thể bị xóa khi bộ đệm đạt đến giới hạn kích thước.
Xóa các mục khỏi bộ đệm
Bạn có thể xóa các mục khỏi bộ đệm bằng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
34Bạn cũng có thể xóa các mục bằng cách cung cấp số giây hết hạn bằng 0 hoặc âm
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
6Bạn có thể xóa toàn bộ bộ đệm bằng phương pháp
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
36Cảnh báo
Xóa bộ đệm không tôn trọng "tiền tố" bộ đệm đã định cấu hình của bạn và sẽ xóa tất cả các mục nhập khỏi bộ đệm. Hãy cân nhắc kỹ điều này khi xóa bộ nhớ cache được chia sẻ bởi các ứng dụng khác.
Trình trợ giúp bộ đệm
Ngoài việc sử dụng mặt tiền
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83, bạn cũng có thể sử dụng chức năng toàn cầu $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 để truy xuất và lưu trữ dữ liệu qua bộ đệm. Khi hàm $value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 được gọi với một đối số chuỗi đơn, nó sẽ trả về giá trị của khóa đã choNếu bạn cung cấp một mảng các cặp khóa/giá trị và thời gian hết hạn cho hàm, thì hàm này sẽ lưu trữ các giá trị trong bộ đệm trong khoảng thời gian đã chỉ định
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
7Khi hàm
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
7 được gọi mà không có bất kỳ đối số nào, nó sẽ trả về một phiên bản của triển khai namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
61, cho phép bạn gọi các phương thức bộ nhớ đệm khác 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
8Lưu ý
Khi thử nghiệm lệnh gọi hàm7 toàn cầu, bạn có thể sử dụng phương thức
$value = Cache::store['file']->get['foo'];
Cache::store['redis']->put['bar', 'baz', 600]; // 10 Minutes
63 giống như khi bạn đang thử nghiệm mặt tiền.
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
Thẻ bộ đệm
Cảnh báo
Thẻ bộ đệm không được hỗ trợ khi sử dụng trình điều khiển bộ đệm7,
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
65 hoặc
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
8. Hơn nữa, khi sử dụng nhiều thẻ có bộ đệm được lưu trữ "mãi mãi", hiệu suất sẽ tốt nhất với trình điều khiển chẳng hạn như
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
67, tự động xóa các bản ghi cũ.
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
Lưu trữ các mục bộ nhớ cache được gắn thẻ
Thẻ bộ đệm cho phép bạn gắn thẻ các mục có liên quan trong bộ đệm và sau đó xóa tất cả các giá trị đã lưu trong bộ nhớ cache đã được chỉ định một thẻ nhất định. Bạn có thể truy cập bộ đệm được gắn thẻ bằng cách chuyển vào một mảng tên thẻ được sắp xếp theo thứ tự. Các mục được lưu trữ qua thẻ có thể không được truy cập nếu không cung cấp các thẻ được sử dụng để lưu trữ giá trị. Ví dụ: hãy truy cập bộ đệm được gắn thẻ và
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
15 một giá trị vào bộ đệm 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
9Truy cập các mục bộ nhớ cache được gắn thẻ
Để truy xuất một mục bộ đệm được gắn thẻ, hãy chuyển cùng một danh sách các thẻ được sắp xếp theo thứ tự cho phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
69 và sau đó gọi phương thức namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11 bằng khóa bạn muốn truy xuấtnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
30Xóa các mục bộ nhớ cache được gắn thẻ
Bạn có thể xóa tất cả các mục được gán thẻ hoặc danh sách thẻ. Ví dụ: câu lệnh này sẽ xóa tất cả các bộ đệm được gắn thẻ
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
01, 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
02 hoặc cả hai. Vì vậy, cả 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
03 và 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
04 sẽ bị xóa khỏi bộ đệmnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
31Ngược lại, câu lệnh này sẽ chỉ xóa các giá trị được lưu trong bộ nhớ cache được gắn thẻ
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
02, do đó, 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
03 sẽ bị xóa chứ không phải 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
04namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
32Khóa nguyên tử
Cảnh báo
Để sử dụng tính năng này, ứng dụng của bạn phải sử dụng trình điều khiển bộ đệm ẩn67,
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
09,
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
65,
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
8,
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
7 hoặc
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
6 làm trình điều khiển bộ nhớ đệm mặc định cho ứng dụng của bạn. Ngoài ra, tất cả các máy chủ phải giao tiếp với cùng một máy chủ bộ đệm trung tâm.
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
Điều kiện tiên quyết của trình điều khiển
cơ sở dữ liệu
Khi sử dụng trình điều khiển bộ đệm
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
8, bạn sẽ cần thiết lập một bảng để chứa khóa bộ đệm của ứng dụng của bạn. Bạn sẽ tìm thấy một ví dụ khai báo namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
9 cho bảng bên dướinamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
33Quản lý khóa
Khóa nguyên tử cho phép thao tác với các khóa phân tán mà không phải lo lắng về điều kiện chủng tộc. Ví dụ: Laravel Forge sử dụng khóa nguyên tử để đảm bảo rằng chỉ có một tác vụ từ xa đang được thực thi trên máy chủ tại một thời điểm. Bạn có thể tạo và quản lý khóa bằng phương pháp
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
16namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
34Phương thức
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
11 cũng chấp nhận một bao đóng. Sau khi lệnh đóng được thực thi, Laravel sẽ tự động giải phóng khóanamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
35Nếu khóa không khả dụng tại thời điểm bạn yêu cầu, bạn có thể hướng dẫn Laravel đợi trong một số giây được chỉ định. Nếu không thể lấy được khóa trong thời hạn quy định, một
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
18 sẽ bị némnamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
36Ví dụ trên có thể được đơn giản hóa bằng cách chuyển một bao đóng cho phương thức
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
19. Khi một lần đóng được truyền cho phương thức này, Laravel sẽ cố lấy khóa trong số giây đã chỉ định và sẽ tự động giải phóng khóa sau khi quá trình đóng đã được thực thinamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
37Quản lý khóa trên các quy trình
Đôi khi, bạn có thể muốn lấy khóa trong một quy trình và giải phóng nó trong quy trình khác. Ví dụ: bạn có thể nhận được khóa trong khi yêu cầu web và muốn mở khóa khi kết thúc công việc xếp hàng đợi được kích hoạt bởi yêu cầu đó. Trong trường hợp này, bạn nên chuyển "mã thông báo chủ sở hữu" có phạm vi khóa cho công việc được xếp hàng đợi để công việc có thể khởi tạo lại khóa bằng cách sử dụng mã thông báo đã cho
Trong ví dụ bên dưới, chúng tôi sẽ gửi một công việc đã xếp hàng đợi nếu khóa được mua thành công. Ngoài ra, chúng tôi sẽ chuyển mã thông báo chủ sở hữu của khóa cho công việc được xếp hàng đợi thông qua phương thức
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
20 của khóanamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
38Trong công việc
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
21 của ứng dụng, chúng tôi có thể khôi phục và giải phóng khóa bằng cách sử dụng mã thông báo chủ sở hữunamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
39Nếu bạn muốn mở khóa mà không tôn trọng chủ sở hữu hiện tại của nó, bạn có thể sử dụng phương pháp
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
22namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
0Thêm trình điều khiển bộ đệm tùy chỉnh
Viết Người Lái Xe
Để tạo trình điều khiển bộ đệm tùy chỉnh của chúng tôi, trước tiên chúng tôi cần triển khai hợp đồng
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
23. Vì vậy, việc triển khai bộ đệm MongoDB có thể trông giống như thế nàynamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
1Chúng ta chỉ cần triển khai từng phương thức này bằng kết nối MongoDB. Để biết ví dụ về cách triển khai từng phương thức này, hãy xem
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
24 trong mã nguồn khung của Laravel. Khi quá trình triển khai của chúng tôi hoàn tất, chúng tôi có thể hoàn tất đăng ký trình điều khiển tùy chỉnh của mình bằng cách gọi phương thức 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
26 của mặt tiền namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
2Lưu ý
Nếu bạn đang băn khoăn không biết nên đặt mã trình điều khiển bộ nhớ cache tùy chỉnh của mình ở đâu, thì bạn có thể tạo một không gian tên27 trong thư mục
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
28 của mình. Tuy nhiên, hãy nhớ rằng Laravel không có cấu trúc ứng dụng cứng nhắc và bạn có thể tự do tổ chức ứng dụng của mình theo sở thích của mình.
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
Đăng ký tài xế
Để đăng ký trình điều khiển bộ đệm tùy chỉnh với Laravel, chúng tôi sẽ sử dụng phương pháp
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
26 trên mặt tiền namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
83. Vì các nhà cung cấp dịch vụ khác có thể cố gắng đọc các giá trị được lưu trong bộ nhớ cache trong phương pháp 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
31 của họ, nên chúng tôi sẽ đăng ký trình điều khiển tùy chỉnh của mình trong một cuộc gọi lại 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
32. Bằng cách sử dụng lệnh gọi lại 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
32, chúng tôi có thể đảm bảo rằng trình điều khiển tùy chỉnh được đăng ký ngay trước khi phương thức 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
31 được gọi trên các nhà cung cấp dịch vụ của ứng dụng của chúng tôi nhưng sau khi phương thức 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
35 được gọi trên tất cả các nhà cung cấp dịch vụ. Chúng tôi sẽ đăng ký cuộc gọi lại 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
32 của chúng tôi trong phương thức 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
35 của lớp 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
38 của ứng dụng của chúng tôinamespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
3Đối số đầu tiên được truyền cho phương thức
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
26 là tên của trình điều khiển. Điều này sẽ tương ứng với tùy chọn 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
40 của bạn trong tệp cấu hình namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
5. Đối số thứ hai là một bao đóng sẽ trả về một phiên bản 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
42. Việc đóng sẽ được thông qua một phiên bản 'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
43, đây là một phiên bản của bộ chứa dịch vụSau khi tiện ích mở rộng của bạn được đăng ký, hãy cập nhật tùy chọn
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
40 của tệp cấu hình namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
* Show a list of all users of the application.
$value = Cache::get['key'];
5 thành tên tiện ích mở rộng của bạnSự kiện
Để thực thi mã trên mọi hoạt động của bộ đệm, bạn có thể lắng nghe các sự kiện được kích hoạt bởi bộ đệm. Thông thường, bạn nên đặt các trình lắng nghe sự kiện này trong lớp
'host' => env['MEMCACHED_HOST', '127.0.0.1'],
'port' => env['MEMCACHED_PORT', 11211],
46 của ứng dụng của bạn