Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: how to separate cache and session into different redis database?

I want to put session and cache data into redis. This is my configuration in database.php:

'redis' => array(

    'cluster' => false,

    'default' => array(
        'host'     => '192.168.56.101',
        'port'     => 6379,
        'database' => 0,
    ),

    'session' => array(
        'host'     => '192.168.56.101',
        'port'     => 6379,
        'database' => 1,
    ),
),

session.php:

return array(
    'driver' => 'redis',
    'connection' => 'session',
);

cache.php:

'driver' => 'redis',

However, where I write code like this:

Cache::remember('aa',1,function(){
    return 'bb';
});

cache driver uses the same redis database as session driver does, which results in:

127.0.0.1:6379[1]> keys *
1) "aa"
2) "e0606244bec40b0352fb2b7b65d98049e49f6189"

Anyone knows how to force cache to use a specific redis connection? Or I have to mix them up together?

like image 532
Nemo Jia Avatar asked Jan 08 '15 07:01

Nemo Jia


2 Answers

Introduction

Here is my note, for some other guy who running in to this problem, I think this is should be in the docs.

By default, redis gives you 16 separate databases, but laravel out of the box will try to use database 0 for both sessions and cache.

Our solution is to let Redis caching using database 0, and database 1 for Session, there for solving the session clear by running php artisan cache:clear problem.

Note: Tested at Laravel 5.1

1. Setting up Session Redis connection

Modify config/database.php, add session key to the redis option:

'redis' => [

   'cluster' => false,

   '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,
   ],
],

2. Make use of the session connection

Modify config/session.php, change the following:

'connection' => null,

to:

'connection' => 'session',

3. Using Redis as session driver

Modify .env, change SESSION_DRIVER:

SESSION_DRIVER=redis

4. Testing out

Execute the following artisan command, then check your login state:

php artisan cache:clear

If the login state persists, voilà!

like image 124
CharlieJade Avatar answered Nov 12 '22 22:11

CharlieJade


Laravel 5.5:

database.php should look like this:

'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,
    ],

],

And in the session.php you have to update also the key "connection" to the right key. In this case 'session'

'connection' => 'session',
like image 4
Marcel Harper Avatar answered Nov 13 '22 00:11

Marcel Harper