LoginSignup
3
5

More than 5 years have passed since last update.

artisan cache:clear したらセッションがふっとんだ

Posted at

現象

CACHE_DRIVERとSESSION_DRIVERをどちらもredisにしたうえで

$ php artisan cache:clear

したところセッションが消えてこまった

原因

CacheとSessionで使っているRedisのDBが同じだから

対策

Cache用のDBと、Session用のDBを分けてやればよい

具体例

config/database.php でセッション用のredisの接続を定義して
config/session.php でその接続を指定するようにしました

config/database.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,
        ],

    ],
...
config/session.php
...
    /*
    |--------------------------------------------------------------------------
    | 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',
...
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5