Thursday, 27 June 2019

Configuring Redis as Cache interface in a Laravel 5 Application

I have a Laravel application that I am integrating a Redis Server into to handle the application caching. However I am having issues with the Redis configuration, as I am not able to access the Redis interface using the Laravel cache facade.

I set up a redis-server and comfirmed that it is working on the server:

> redis-cli
127.0.0.1:6379 > ping
PONG

I then followed the integration documentation for Redis/Laravel from here: https://laravel.com/docs/5.7/redis

I installed the composer predis/predis package...
I set it up in Laravel to use the default redis config:

app/config/cache.php:

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

app/config/database.php:

'redis' => [

    'client' => 'predis',

    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],

    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ],
]

.env:

BROADCAST_DRIVER=log
CACHE_DRIVER=redis
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379


Now when I test I can access Redis via the Redis Facade...

use Illuminate\Support\Facades\Redis;

...but not from the Cache Facade.

use Illuminate\Support\Facades\Cache;

    // ************* this works
    Redis::set('testFromRedisSet', 'RedisSet');

    // ************* none of these work...
    Cache::store('redis')->put('testFromStoreCachePut', 'CacheStorePut', 600);

    Cache::put('testFromCachePut', 'CachePut', 600);

    Cache::remember('testFromCacheRemember', 60, function() {
       return "CacheRemember";
    });
    Cache::rememberForever('testFromCacheRememberForever', function() {
        return "CacheRememberForever";
    });

127.0.0.1:6379> KEYS '*'
1) "testFromRedisSet"
127.0.0.1:6379>


Interestingly enough the Cache Facade still appears to be functioning properly. If I monitor the cache requests in Telescope, the 4 cache requests in the example that are not showing up in the Redis Server are still being cached somewhere, and using the Cache facade to retrieve these 4 values works fine.

...all these retrieve the correct values:

Cache::get('testFromCacheStoreRedisPut');
Cache::get('testFromCachePut');
Cache::get('testFromCacheRemember');
Cache::get('testFromCacheRememberForever');


So what is going on here?

It looks like Laravel is utilizing the [redis] cache driver correctly, because when I shut down the Redis Server and retest the app the entire applications throws a Predis Connection Exception:

In AbstractConnection.php line 155: Connection refused [tcp://127.0.0.1:6379]

Where are the cache keys being stored and why can't I view the stored keys in the terminal using "redis-cli"?

redis-cli KEYS '*'



from Configuring Redis as Cache interface in a Laravel 5 Application

No comments:

Post a Comment