在 Symfony 上设置 Redis 缓存前缀键 [英] Set Redis cache prefix key on Symfony

查看:48
本文介绍了在 Symfony 上设置 Redis 缓存前缀键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Redis 来管理我的 Symfony 3.4 应用程序中的一些缓存,配置如下:

I'm using Redis to manage some caching within my Symfony 3.4 app, configured like this:

config.yml

framework:
    cache:
        default_redis_provider: 'redis://127.0.0.1:6379'
        pools:
            cache.catalog:
                adapter: cache.adapter.redis
                provider: iwid.custom_redis_provider
                default_lifetime: 86400
                public: true
            cache.language:
                adapter: cache.adapter.redis
                provider: iwid.custom_redis_provider
                default_lifetime: 86400
                public: true

services.yml

services:
    iwid.custom_redis_provider:
        class: Redis
        factory: ['SymfonyComponentCacheAdapterRedisAdapter', 'createConnection']
        arguments:
            - 'redis://127.0.0.1:6379'
            - { retry_interval: 0, timeout: 30 }

现在,这在开发和生产环境中就像一个魅力一样,除了生产中的一件事:当我部署一个新版本时,我的部署系统创建一个新文件夹git将拉入其中,然后使用符号链接将此文件夹定位为当前根目录.

Now, this is working like a charm in dev and prod environments, except for one thing in production: when I deploy a new release, my deployer system creates a new folder and git pull inside it, then target this folder as the current rootdir with a symlink.

然后,当我部署任何版本时,我的 Redis 键的前缀会更改,因为我的应用程序的路径不同.然后,这显然会使任何以前缓存的密钥无效......这不是我想要的!

Then, when I deploy any release, the prefix of my Redis keys is changed, as the path to my app is different. Then, this obviously invalidate any previously cached keys... which is not what I want!

那么,我可以改变这一点吗,可能是通过一种固定"的缓存键(显然每个池一个).

So, ho can I change this, probably by having a sort of "fixed" cache key (one for each pool obviously).

非常感谢任何帮助!

推荐答案

是的,您必须设置一个固定键(正如他们在 Symfony 文档).

Yes you have to set a fixed key (as they said in the Symfony Doc).

如果您想使用相同的 Redis,您还可以将环境名称(dev、staging、prod... - SYMFONY_ENVAPP_ENV)与应用程序名称一起使用例如,用于登台和生产的集群.

You can use also the environment name (dev, staging, prod... - SYMFONY_ENV or APP_ENV) with the application name if you want to use the same Redis cluster for staging and prod for example.

framework:
    cache:
        ...
        prefix_seed: '%kernel.environment%_myapp'
        ...

这篇关于在 Symfony 上设置 Redis 缓存前缀键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆