Laracon EU tickets are now available! Get your tickets today!



Laravel provides an expressive, unified API for various caching backends. The cache configuration is located at config/cache.php. In this file you may specify which cache driver you would like to be used by default throughout your application. Laravel supports popular caching backends like Memcached and Redis out of the box.


Driver Prerequisites







'memcached' => [
        'host' => '',
        'port' => 11211,
        'weight' => 100


'memcached' => [
        'port' => 0,
        'weight' => 100


Before using a Redis cache with Laravel, you will need to either install the predis/predis package (~1.0) via Composer or install the PhpRedis PHP extension via PECL.


Cache Usage

Obtaining A Cache Instance




namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;

class UserController extends Controller
     * Show a list of all users of the application.
     * @return Response
    public function index()


Accessing Multiple Cache Stores



Cache::store('redis')->put('bar', 'baz', 600); // 10 Minutes

Retrieving Items From The Cache






Checking For Item Existence

The has method may be used to determine if an item exists in the cache. This method will return false if the value is null:


Incrementing / Decrementing Values



Retrieve & Store


$value = Cache::remember('users', $seconds, function () {


You may use the rememberForever method to retrieve an item from the cache or store it forever:


Retrieve & Delete



Storing Items In The Cache

You may use the put method on the Cache facade to store items in the cache:

Cache::put('key', 'value', $seconds);

If the storage time is not passed to the put method, the item will be stored indefinitely:

Cache::put('key', 'value');

Instead of passing the number of seconds as an integer, you may also pass a DateTime instance representing the expiration time of the cached item:

Cache::put('key', 'value', now()->addMinutes(10));

Store If Not Present


Cache::add('key', 'value', $seconds);

Storing Items Forever




Removing Items From The Cache

You may remove items from the cache using the forget method:


You may also remove items by providing a zero or negative TTL:

Cache::put('key', 'value', 0);

Cache::put('key', 'value', -5);

You may clear the entire cache using the flush method:



Atomic Locks

{note} To utilize this feature, your application must be using the memcached, dynamodb, or redis cache driver as your application's default cache driver. In addition, all servers must be communicating with the same central cache server.

Atomic locks allow for the manipulation of distributed locks without worrying about race conditions. For example, Laravel Forge uses atomic locks to ensure that only one remote task is being executed on a server at a time. You may create and manage locks using the Cache::lock method:

use Illuminate\Support\Facades\Cache;

$lock = Cache::lock('foo', 10);

if ($lock->get()) {
    // Lock acquired for 10 seconds...


The get method also accepts a Closure. After the Closure is executed, Laravel will automatically release the lock:

Cache::lock('foo')->get(function () {
    // Lock acquired indefinitely and automatically released...

If the lock is not available at the moment you request it, you may instruct Laravel to wait for a specified number of seconds. If the lock can not be acquired within the specified time limit, an Illuminate\Contracts\Cache\LockTimeoutException will be thrown:

use Illuminate\Contracts\Cache\LockTimeoutException;


try {

    // Lock acquired after waiting maximum of 5 seconds...
} catch (LockTimeoutException $e) {
    // Unable to acquire lock...
} finally {

Cache::lock('foo', 10)->block(5, function () {

Managing Locks Across Processes

Sometimes, you may wish to acquire a lock in one process and release it in another process. For example, you may acquire a lock during a web request and wish to release the lock at the end of a queued job that is triggered by that request. In this scenario, you should pass the lock's scoped "owner token" to the queued job so that the job can re-instantiate the lock using the given token:

// Within Controller...
$podcast = Podcast::find($id);

$lock = Cache::lock('foo', 120);

if ($result = $lock->get()) {
    ProcessPodcast::dispatch($podcast, $lock->owner());

// Within ProcessPodcast Job...
Cache::restoreLock('foo', $this->owner)->release();

If you would like to release a lock without respecting its current owner, you may use the forceRelease method:


The Cache Helper


$value = cache('key');


cache(['key' => 'value'], $seconds);

cache(['key' => 'value'], now()->addMinutes(10));

When the cache function is called without any arguments, it returns an instance of the Illuminate\Contracts\Cache\Factory implementation, allowing you to call other caching methods:

cache()->remember('users', $seconds, function () {


Cache Tags


Storing Tagged Cache Items




Accessing Tagged Cache Items




Removing Tagged Cache Items





Adding Custom Cache Drivers

Writing The Driver

To create our custom cache driver, we first need to implement the Illuminate\Contracts\Cache\Store contract. So, a MongoDB cache implementation would look something like this:


namespace App\Extensions;

use Illuminate\Contracts\Cache\Store;

class MongoStore implements Store
    public function get($key) {}
    public function many(array $keys);
    public function put($key, $value, $seconds) {}
    public function putMany(array $values, $seconds);
    public function forever($key, $value) {}
    public function forget($key) {}
    public function flush() {}
    public function getPrefix() {}


    return Cache::repository(new MongoStore);


Registering The Driver



namespace App\Providers;

use App\Extensions\MongoStore;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;

class CacheServiceProvider extends ServiceProvider
     * Perform post-registration booting of services.
     * @return void
    public function boot()
            return Cache::repository(new MongoStore);

     * Register bindings in the container.
     * @return void
    public function register()


Once your extension is registered, update your config/cache.php configuration file's driver option to the name of your extension.



 * The event listener mappings for the application.
 * @var array
protected $listen = [
    'Illuminate\Cache\Events\CacheHit' => [

    'Illuminate\Cache\Events\CacheMissed' => [

    'Illuminate\Cache\Events\KeyForgotten' => [

    'Illuminate\Cache\Events\KeyWritten' => [