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

查看:59
本文介绍了在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: ['Symfony\Component\Cache\Adapter\RedisAdapter', '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).

任何帮助都将不胜感激!

Any help greatly appreciated!

推荐答案

是的,您必须设置一个固定键(如

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

如果要使用相同的Redis,还可以将环境名称(dev,staging,prod ...- SYMFONY_ENV APP_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天全站免登陆