Set Redis cache prefix key on Symfony

两盒软妹~` 提交于 2021-02-17 02:09:07

问题


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 }

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.

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!


回答1:


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

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


来源:https://stackoverflow.com/questions/58388780/set-redis-cache-prefix-key-on-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!