Home Creating an In-Memory Cache Driver with Redis as fallback in Laravel
Post
Cancel

Creating an In-Memory Cache Driver with Redis as fallback in Laravel

One of the many challenges of building a scalable application is to ensure that it can handle a large number of concurrent requests without compromising performance. Caching is a popular technique to improve application performance by storing frequently accessed data in memory. In this post, we will explore how to create an in-memory cache driver with Redis as a fallback in Laravel.

Understanding Laravel Cache Drivers

Laravel supports multiple cache drivers out of the box, including:

  • File: Stores cached data in files on the disk.
  • Database: Stores cached data in a database table.
  • Redis: Stores cached data in a Redis server.
  • Memcached: Stores cached data in a Memcached server.
  • Array: Stores cached data in an array in memory.

You can configure the cache driver in the config/cache.php file. For example, to use the Redis cache driver, set the default option to redis:

1
'default' => env('CACHE_DRIVER', 'redis'),

Creating an In-Memory Cache Driver

To create an in-memory cache driver, follow these steps:

  1. Create a new class that implements the Illuminate\Contracts\Cache\Store interface:

  2. Register the new cache driver in the config/cache.php file:

    1
    2
    3
    4
    5
    6
    
     'stores' => [
         'in-memory' => [
             'driver' => 'in-memory',
             'limit' => 1000,
         ],
     ],
    
  3. Add the in-memory cache driver to the config/cache.php file:

    1
    
     'default' => env('CACHE_DRIVER', 'in-memory'),
    

Using the In-Memory Cache Driver

You can use the in-memory cache driver like any other cache driver:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// app/Http/Controllers/ExampleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Cache;

class ExampleController extends Controller
{
    public function index()
    {
        // Store an item in the cache for 60 seconds...
        Cache::put('key', 'value', 60);

        // Retrieve an item from the cache...
        $value = Cache::get('key');

        // Retrieve an item from the cache, or store the default value forever...
        $value = Cache::rememberForever('key', function () {
            return 'value';
        });
    }
}

Conclusion

In this post, we explored how to create an in-memory cache driver with Redis as a fallback in Laravel. This cache driver is useful for high-frequent usage during a single runtime. If you have any questions or feedback, feel free to leave a comment below.

Further Reading

This post is licensed under CC BY 4.0 by the author.