清除 Artisan 缓存后,会导致会话丢失

迹象

将CACHE_DRIVER和SESSION_DRIVER都设为redis

$ php artisan cache:clear

赶到现场时,会话突然消失令人困扰。

导致这个问题的原因是什么?

因为Cache和Session都使用了相同的Redis数据库

应对措施 duì cuò shī)

只需要将用于缓存的数据库和用于会话的数据库分开即可

具体实例

在`config/database.php`中定义了用于会话的Redis连接,并在`config/session.php`中指定了该连接。

...
    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

        'session' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 1,
        ],

    ],
...
...
    /*
    |--------------------------------------------------------------------------
    | Session Database Connection
    |--------------------------------------------------------------------------
    |
    | When using the "database" or "redis" session drivers, you may specify a
    | connection that should be used to manage these sessions. This should
    | correspond to a connection in your database configuration options.
    |
    */

    'connection' => 'session',
...
广告
将在 10 秒后关闭
bannerAds