Skip to Content
🎉 Bref 3.0 is released. Read more →

Caching

Since Laravel 11, Laravel’s default cache store is database. If your application has no database, set the cache store in serverless.yml. The serverless.yml generated for Laravel uses the array store:

serverless.yml
provider: environment: CACHE_STORE: array

The array store caches nothing between requests, so the cache can never be stale. Avoid the file store: it writes to /tmp, which each Lambda instance has its own copy of. Once your application runs on several instances, clearing a cache key only clears it in one instance, and the others keep serving the old value.

If you want a cache shared across all Lambda instances, for example if your application caches a lot of data or uses locks (like API rate limiting or withoutOverlapping()), use the database or DynamoDB.

Database Cache

If your application uses a database, it is the simplest option. Set CACHE_STORE: database in serverless.yml (or remove CACHE_STORE to use Laravel’s default), and make sure the cache table exists: it is created by the create_cache_table migration of new Laravel applications, or by php artisan make:cache-table.

DynamoDB Cache

To use DynamoDB as a cache store, set the following lines in config/cache.php:

config/cache.php
'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'), 'attributes' => [ 'key' => 'id', 'expiration' => 'ttl', ] ],

Then follow this section of the documentation to deploy your DynamoDB table using the Serverless Framework, and set CACHE_STORE: dynamodb and DYNAMODB_CACHE_TABLE in serverless.yml.

The DynamoDB cache store requires the aws/aws-sdk-php package, which is large: remove the AWS services you don’t use to stay under Lambda’s size limit.

Last updated on