How to clear redis cache while saving session data: Laravel 5

I use redis as a session driver, and I want to clear the cache by storing session data, so basically the user can stay on the system. Any suggestions regarding restructuring or handling the current situation?

Note I do not want to use a separate instance of redis for sessions and other cache data.

+5
source share
4 answers

Introduction

By default, redis gives you 16 separate databases, but laravel out of the box will try to use database 0 for sessions and cache.

Our solution is to enable Redis caching using database 0 and database 1 for the session in order to solve the session problem by running the php artisan cache:clear task.

1. Configuring a Redis Session Connection

Modify config/database.php , add the session key to the redis option:

 'redis' => [ 'cluster' => false, 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], 'session' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 1, ], ], 

2. Use session connection

Change config/session.php , change the following:

 'connection' => null, 

in

 'connection' => 'session', 

3. Using Redis as a Session Driver

Change .env , change SESSION_DRIVER :

 SESSION_DRIVER=redis 

4. Testing

Run the following artisan command, then check your login status:

 php artisan cache:clear 

If the input state is saved, voilà!

+10
source

I do not know Laravel, but overall the best two options:

  • Change the format of the cache keys. You must use the keys with the version cache so that you can do this in the future, that is, "cache.1". therefore, you can increase, and then all your keys are irrelevant at once.

  • Move the cache to a different db number in the same redis instance. So you can also do FLUSHDB on this db number to clear the cache.

In both cases, after you do this, if the cache keys do not expire in time, you must create a script that uses SCAN to delete the old keys. See http://redis.io/commands/scan

As a side note, it’s usually a bad idea to keep the cache and other things in the same redis instance, as in the caches, you usually use LRU-based eviction, and you don’t want to mix it with less volatile keys.

+3
source

Laravel Cache :: clear () sends a Redis flushall command that resets everything that is not very useful in my experience. You will need to extend the cache class and create your own set to index the cache data that you want to clear. Then create another function to read the set and execute the Redis del () command for each key in the set. Put some working code and, if necessary, specify in detail

Notagolfers' suggestion to split the cache and session into different redis databases is not a challenge, but you still need to extend the cache class to implement the Redis database configuration switch.

+1
source

https://laravel.com/docs/5.2/redis#configuration

 'redis' => [ 'cluster' => false, 'default' => [ 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ], ], 

There is a “database” in the parameters of redis connection, just select different databases for the session and cache. I just hope the redis cache driver uses flushdb not flushall for cleaning :).

+1
source

Source: https://habr.com/ru/post/1241915/


All Articles