Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '*'

Monitoring the Redis Server shows me that Redis is storing the data and pushing event notifications, but all data that I can't see in the termimal is prefixed with laravel_cache. However there is no data or hash key with that name present in the interface.

1561596707.950397 [1 127.0.0.1:42058] "SETEX" "laravel_cache:testFromCacheStoreRedisPut" "36000" "s:18:\"CacheStoreRedisPut\";"
1561596707.950898 [1 127.0.0.1:42058] "SETEX" "laravel_cache:testFromCachePut" "36000" "s:8:\"CachePut\";"
1561596707.951521 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRemember"
1561596707.952110 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRememberForever"
1561596707.952718 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheStoreRedisPut"
1561596707.953236 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCachePut"
1561596707.953745 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRemember"
1561596707.954191 [1 127.0.0.1:42058] "GET" "laravel_cache:testFromCacheRememberForever"
1561596709.251036 [0 127.0.0.1:42064] "SELECT" "1"
1561596709.251200 [1 127.0.0.1:42064] "GET" "laravel_cache:telescope:dump-watcher"
1561596709.263678 [1 127.0.0.1:42064] "GET" "laravel_cache:telescope:pause-recording"

How can i accesss the data being stored in the laravel_cache namespace? Even running the same GET commands shown in the server monitor does not fetch any data directly.

like image 931
VerySeriousSoftwareEndeavours Avatar asked Jun 24 '19 20:06

VerySeriousSoftwareEndeavours


People also ask

How does Redis integrate with Laravel?

Redis in Laravel In order to use Redis with Laravel, firstly yo will have to install the predis/predis package. You can install it via composer. Just run this command and you're done. Apart from this, you can also install php redis, a php extension via PECL.

How does Redis cache work Laravel?

Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data.

Can Redis be used as cache?

Caching. Redis is a great choice for implementing a highly available in-memory cache to decrease data access latency, increase throughput, and ease the load off your relational or NoSQL database and application.


1 Answers

Redis cache store uses the cache database connection by default. If you look at config/database.php it switches to database 1 for caching by default. To view the keys in the terminal, you have to select which database to use first.

127.0.0.1:6379> SELECT 1
127.0.0.1:6379[1]> KEYS *

Using different databases allows you to separate your cache and session store, it can be handy when you need to delete all cache keys (flushdb) but keep the user session. You can even add a separate connection for queues. Note that it's not recommended to use multiple databases, use multiple Redis instance instead.

like image 171
Razor Avatar answered Oct 16 '22 09:10

Razor