Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using redis as a session driver and I want to clear the cache while keeping the session data, so basically user can stay logged in. Any suggestions regarding restructuring or handling the current situation?

Note: I don't want to use separate redis instance for sessions and other cache data.

like image 962
rajangupta Avatar asked Jan 31 '16 07:01

rajangupta


1 Answers

Intro

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

Our solution is to let Redis caching using database 0, and database 1 for Session, there for solving the session clear by running php artisan cache:clear problem.

1. Setting up Session Redis connection

Modify config/database.php, add 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. Make use of the session connection

Modify config/session.php, change the following:

'connection' => null,

to:

'connection' => 'session',

3. Using Redis as session driver

Modify .env, change SESSION_DRIVER:

SESSION_DRIVER=redis

4. Testing out

Execute the following artisan command, then check your login state:

php artisan cache:clear

If the login state persists, voilà!

like image 151
CharlieJade Avatar answered Sep 20 '22 17:09

CharlieJade