For the laravel 9 (it is similar for an older ones):
1.update session configuration
you must put in .env:
SESSION_CONNECTION=session
that configuration is then loaded at config/session.php
'connection' => env('SESSION_CONNECTION'),
That tells laravel to store sessions in separated redis/database connection
2.update redis configuration
add new redis connection at the config/database.php for sessions
'redis' => [
...
'session' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
...
]
you can use and database number from 0 to 16. For other connections put different database number.
3.clear cache(without touching sessions)
now you can clear the redis cache with this piece of code:
$redisConnection = \Illuminate\Support\Facades\Redis::connection('default');
$redisConnection->flushDB();
TIP: You can put that code in custom command and run it with the artisan
BONUS: You can see and clear all redis data for each connection with:
$redisSession = \Illuminate\Support\Facades\Redis::connection('session');//session|queue|default
$redisSessionKeys = $redisSession->keys('*');
$redisSession->flushDB();
dd($redisSessionKeys);
BONUS2: You can also add "redis database" for queues and then you have queue jobs, sessions and cache separated and you can clear only one of them
'redis' => [
...
'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,
],
'queue' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2,
],
]