Cache
Introduction
Some of the data retrieval or processing tasks performed by your application could be CPU intensive or take several seconds to complete. When this is the case, it is common to cache the retrieved data for a time so it can be retrieved quickly on subsequent requests for the same data. The cached data is usually stored in a very fast data store such as Memcached or Redis.
Thankfully, Laravel provides an expressive, unified API for various cache backends, allowing you to take advantage of their blazing fast data retrieval and speed up your web application.
Configuration
Your application's cache configuration file is located at config/cache.php
. In this file, you may specify which cache store you would like to be used by default throughout your application. Laravel supports popular caching backends like Memcached, Redis, DynamoDB, and relational databases out of the box. In addition, a file based cache driver is available, while array
and "null" cache drivers provide convenient cache backends for your automated tests.
The cache configuration file also contains a variety of other options that you may review. By default, Laravel is configured to use the database
cache driver, which stores the serialized, cached objects in your application's database.
Driver Prerequisites
Database
When using the database
cache driver, you will need a database table to contain the cache data. Typically, this is included in Laravel's default 0001_01_01_000001_create_cache_table.php
database migration; however, if your application does not contain this migration, you may use the make:cache-table
Artisan command to create it:
php artisan make:cache-table
php artisan migrate
Memcached
Using the Memcached driver requires the Memcached PECL package to be installed. You may list all of your Memcached servers in the config/cache.php
configuration file. This file already contains a memcached.servers
entry to get you started:
'memcached' => [
// ...
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],
If needed, you may set the host
option to a UNIX socket path. If you do this, the port
option should be set to 0
:
'memcached' => [
// ...
'servers' => [
[
'host' => '/var/run/memcached/memcached.sock',
'port' => 0,
'weight' => 100
],
],
],
Redis
Before using a Redis cache with Laravel, you will need to either install the PhpRedis PHP extension via PECL or install the predis/predis
package (~2.0) via Composer. Laravel Sail already includes this extension. In addition, official Laravel deployment platforms such as Laravel Forge and Laravel Vapor have the PhpRedis extension installed by default.
For more information on configuring Redis, consult its Laravel documentation page.
DynamoDB
Before using the DynamoDB cache driver, you must create a DynamoDB table to store all of the cached data. Typically, this table should be named cache
. However, you should name the table based on the value of the stores.dynamodb.table
configuration value within the cache
configuration file. The table name may also be set via the DYNAMODB_CACHE_TABLE
environment variable.
This table should also have a string partition key with a name that corresponds to the value of the stores.dynamodb.attributes.key
configuration item within your application's cache
configuration file. By default, the partition key should be named key
.
Next, install the AWS SDK so that your Laravel application can communicate with DynamoDB:
composer require aws/aws-sdk-php
In addition, you should ensure that values are provided for the DynamoDB cache store configuration options. Typically these options, such as AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
, should be defined in your application's .env
configuration file:
'dynamodb' => [
'driver' => 'dynamodb',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'table' => env('DYNAMODB_CACHE_TABLE', 'cache'),
'endpoint' => env('DYNAMODB_ENDPOINT'),
],
Cache Usage
Obtaining a Cache Instance
To obtain a cache store instance, you may use the Cache
facade, which is what we will use throughout this documentation. The Cache
facade provides convenient, terse access to the underlying implementations of the Laravel cache contracts:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Cache;
class UserController extends Controller
{
/**
* Show a list of all users of the application.
*/
public function index(): array
{
$value = Cache::get('key');
return [
// ...
];
}
}