Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PepsiIsBetter's avatar

Redis Facade cannot get session data

I set the session driver to redis, but I don't get session data from Redis Facade.

This is config/session.php

'driver' => env('SESSION_DRIVER', 'file'),
'connection' => env('SESSION_CONNECTION'),

This is .env

SESSION_DRIVER=redis
SESSION_CONNECTION="session-connection"

This is config/database.php

'redis' => [

        'session-connection' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
            'prefix' => 'session:',
        ],

    ],

These are codes which run in the php artisan tinker:

>>> session(['key'=>'value']);
=> null

>>> session('key');
=> "value"

>>> use Illuminate\Support\Facades\Redis;
>>> Redis::connection('session-connection')->get('key')
=> null

>>> Redis::connection('session-connection')->keys('*')
=> []

But Redis Facade can get Cache data, and I don't know why session cannot.

These are settings related to the cache.

This is config/cache.php

'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'cache-connection',
            'lock_connection' => 'cache-lock',
        ],
],
//'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache_'),

This is .env

CACHE_DRIVER=redis

This is config/database.php

'redis' => [

        'cache-connection' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
            'prefix' => 'cache:',
        ],

        'cache-lock' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => '1',
            'prefix' => 'cache-lock:',
        ],

    ],

These are codes which run in the php artisan tinker:

>>> Cache::set('cachekey',1);
=> true

>>> Cache::get('cachekey');
=> "1"

>>> use Illuminate\Support\Facades\Redis;
>>> Redis::connection('cache-connection')->get('cachekey')
=> "1"

>>> Redis::connection('cache-connection')->keys('*')
=> [
     "cache:cachekey",
   ]

Any ideas? Thank you!

0 likes
4 replies
Sinnbeck's avatar

Is it perhaps cached to use file? php artisan config:clear

Sinnbeck's avatar

Just reread the issue. Be aware that all session data is encrypted so you probably cant get it out using the redis facade (without handling the decryption). Why not just use session to get session data?

1 like
PepsiIsBetter's avatar

@Sinnbeck One reason is, I want to know if I successfully set the session driver to the redis, so I try to use Redis Facade to confirm that.

Another reason is for debugging purpose. Because I develop on the vscode, and it has a client interface that can show me all data from my redis database.

But all those global assigned session data had never appeared in my redis database window, the redis database is always empty. It is odd.

======

Well...., I just write a livewire form to test the similar issue, and I find the session data from the submitted form APPEARED in my redis database!!!

After the form submitted, it created two session data

    session(['register.string' => $this->string]); // 'hello'
    session(['register.another_string' => $this->another_string]); // ' hey'

Below is the data shows on my vscode redis client window

// The session key is:  session:sCXC1ZdM9UHqB5q3gE45iIPT8bM91MZRhysfCZ2w
s:295:"a:4:{s:6:"_token";s:40:"psrdhk9laAPjDu201uEJLoMq42IOoQIISebMTtmh";s:9:"_previous";a:1:{s:3:"url";s:40:"http://myproject.test/form";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:8:"register";a:2:{s:9:"string";s:24:"hello";s:6:"another_string";s:8:"hey";}}";

I use tinker and it returns the form session data

>>> use Illuminate\Support\Facades\Redis;
>>> Redis::connection('session-connection')->keys('*')
=> [
     "session:",
   ]

>>> Redis::connection('session-connection')->get('sCXC1ZdM9UHqB5q3gE45iIPT8bM91MZRhysfCZ2w')
=> "s:295:"a:4:{s:6:"_token";s:40:"psrdhk9laAPjDu201uEJLoMq42IOoQIISebMTtmh";s:9:"_previous";a:1:{s:3:"url";s:40:"http://myproject.test/form";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}s:8:"register";a:2:{s:9:"string";s:24:"hello";s:6:"another_string";s:8:"hey";}}";"

>>> session('sCXC1ZdM9UHqB5q3gE45iIPT8bM91MZRhysfCZ2w')
=> null

>>> session()->has('sCXC1ZdM9UHqB5q3gE45iIPT8bM91MZRhysfCZ2w')
=> false

The form submitted data shows on the vscode redis window, and I can retrieve it from the Redis Facade.

But the globally assigned session data session(['key'=>'value']); does not show on the vscode redis window, and cannot retrieve from the Redis Facade.

It's confusing.

Please or to participate in this conversation.