Laravel:如何将缓存和会话分离到不同的 redis 数据库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27835300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:39:15  来源:igfitidea点击:

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

phpcachinglaravel

提问by Nemo Jia

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

我想将会话和缓存数据放入redis。这是我在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:

会话.php:

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

cache.php:

缓存.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:

缓存驱动程序使用与会话驱动程序相同的 redis 数据库,这导致:

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?

任何人都知道如何强制缓存使用特定的 redis 连接?还是我必须把它们混合在一起?

回答by CharlieJade

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.

默认情况下,redis 为您提供 16 个独立的数据库,但开箱即用的 laravel 会尝试将数据库 0 用于会话和缓存。

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:clearproblem.

我们的解决方案是让Redis缓存使用数据库0,数据库1用于Session,以解决会话清除运行php artisan cache:clear问题。

Note: Tested at Laravel 5.1

注意:在 Laravel 5.1 测试

1. Setting up Session Redis connection

1.设置Session Redis连接

Modify config/database.php, add sessionkey to the redisoption:

修改config/database.phpsessionredis选项中添加key :

'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 sessionconnection

2.利用session连接

Modify config/session.php, change the following:

修改config/session.php,更改以下内容:

'connection' => null,

to:

到:

'connection' => 'session',

3. Using Redis as session driver

3. 使用Redis作为会话驱动

Modify .env, change SESSION_DRIVER:

修改.env,改变SESSION_DRIVER

SESSION_DRIVER=redis

4. Testing out

4. 测试

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

执行以下 artisan 命令,然后检查您的登录状态:

php artisan cache:clear

If the login state persists, voilà!

如果登录状态仍然存在,瞧!

回答by Marcel Harper

Laravel 5.5:

Laravel 5.5:

database.php should look like this:

database.php 应如下所示:

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

在 session.php 中,您还必须将密钥“连接”更新为正确的密钥。在这种情况下“会话”

'connection' => 'session',

回答by Alexander

Laravel 5now supports this.

Laravel 5现在支持这一点。

https://github.com/laravel/framework/commit/d10a840514d122fa638eb5baa24c8eae4818da3e

https://github.com/laravel/framework/commit/d10a840514d122fa638eb5baa24c8eae4818da3e

You can select redis connection by modifying config/cache.php

可以通过修改选择redis连接 config/cache.php

'stores' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'your-connection-name',
    ],
],


Laravel 4 CacheManagerdoes not support selecting redis connection.

Laravel 4CacheManager不支持选择 redis 连接。

What you need to do is to modify/extend CacheManagerand override createRedisDriver()method.

您需要做的是修改/扩展CacheManager和覆盖createRedisDriver()方法。

Modify this line

修改这一行

return $this->repository(new RedisStore($redis, $this->getPrefix()));

To

return $this->repository(
    new RedisStore($redis, $this->getPrefix(), 
    $this->app['config']['cache.redis'])
);

Now you can define your configuration in cache.php

现在您可以在 cache.php 中定义您的配置

'redis' => 'your-connection-name'