Laravel:如何缓存:使用Redis Clusters时清除应用程序?

时间:2020-08-26 19:34:01

标签: laravel redis

简短:

在Redis群集中,仅允许您使用一个数据库。

如果您调用命令php artisian cache:clear会发生什么?

详细信息:

通常,当您调用命令时

php artisian cache:clear

由于使用了不同的数据库,因此不会删除您的会话和队列Redis数据库:

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

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

您会看到Redis使用缓存连接,并且Redis的缓存连接默认将数据库设置为1。

现在,如果我想迁移到Redis群集,这部分将变得很棘手。因为Redis群集仅允许您拥有一个数据库。

根据此blog post,我的配置必须如下所示:

//the redis driver
'redis' => [
    'client' => env('REDIS_CLIENT', 'phpredis'),
    'options' => [
        'cluster' => 'redis',
    ],
    //the `default` connection of the redis driver
    //Note: The Laravel Redis facade is hard coded to use this connection unless its connection is explicitly specified
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => `d:`
    ],
    //the `cache` connection of the redis driver
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => `c:`
    ],
    //the `queue` connection of the redis driver
    'queue' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => 'q:',
    ],
    //the `session` connection of the redis driver
    'session' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        #since config/session.php does not have an app level prefix we can change
        #this prefx to `myapp:s:` if we share redis server between apps
        `prefix` => `s:`
    ],
],

您可能会看到,database设置为0。当我调用命令php artisian cache:clear时会发生什么?

如果您查看官方文档https://laravel.com/docs/7.x/redis#configuration并向下滚动至部分:Configuring Clusters,则会看到数据库也设置为0。但是,有一件事尚不清楚。它使用数组键clusters

因此,我的问题是:如何缓存:使用Redis群集时清除应用程序?

0 个答案:

没有答案