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.
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.
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,
],
],
session
connectionModify config/session.php
, change the following:
'connection' => null,
to:
'connection' => 'session',
Modify .env
, change SESSION_DRIVER
:
SESSION_DRIVER=redis
Execute the following artisan command, then check your login state:
php artisan cache:clear
If the login state persists, voilà!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With