Answer the question
In order to leave comments, you need to log in
How to manage user sessions in Laravel using Redis?
This is my first experience with Redis, so I might be missing something very simple
. Hi! The documentation states that you can work with sessions through session()
or Request
.
When the sessions were in MySQL, to get all user sessions it was enough to do this:
$sessions = request()->user()->sessions()->get();
When sessions are stored in Redis, the query above gives an error:
SQLSTATE[42S02]: Base table or view not found: 1146
Illuminate\Support\Facades\Redis::connection('session')->keys('*')
- sessions work,
in config/database.php
session
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', 1),
],
'session' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_SESSION_DB', 2),
],
],
.env
I added a variable to the file
and SESSION_CONNECTION=session
What REDIS_PASSWORD=password
php artisan optimize:clear
php artisan clear-compiled
composer dump-autoload
reboot
Answer the question
In order to leave comments, you need to log in
The Redis driver does not offer the same flexibility as the database
If you can select any field from the database,
then in Redis you have a key => value,
while the key name has nothing to do with the user id
, of course you can take everything sessions from the radish and iterate over them with the appropriate time costs, a
separate connection is made so that the sessions are not mixed with the cache,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question