首页 > 解决方案 > 如何为 symfony5 redis 缓存池设置命名空间和项目过期时间

问题描述

我尝试在我的 Synfony5 应用程序中配置两个缓存池以使用某个命名空间并为项目设置默认到期日期。在无数次尝试了无数次变体之后,我感觉我的配置正在循环中。

到目前为止我所理解的: 在RedisAdapter 的构造函数中,您可以设置命名空间和默认过期时间在 createConnection 方法中,您可以设置 redis 服务器的 url。

然而,RedisAdapter 的构造函数似乎已经需要一个 redis 客户端(= redis 连接?)RedisAdapter:

/**
 * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient     The redis client
 * @param string                                                   $namespace       The default namespace
 * @param int                                                      $defaultLifetime The default lifetime
 */
public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
    $this->init($redisClient, $namespace, $defaultLifetime, $marshaller);
}

如何将我的命名空间和 defaultLifetimes 注入 RedisAdapter?

到目前为止我尝试了什么:cache.yaml:

framework:
    cache:
        pools:
            cache.sap:
                adapter: cache.adapter.redis
                provider: app.service.puc_sap_redis_adapter
            cache.pers:
                adapter: cache.adapter.redis
                provider: app.service.puc_pers_redis_adapter

服务.yaml:

app.my_redis_adapter:
    class: 'Redis'
    factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
    arguments:
        - 'redis://%env(string:REDIS_URL)%:%env(int:REDIS_PORT)%'
        - { retry_interval: 2, timeout: 5 }

app.service.puc_sap_redis_adapter:
    class: Symfony\Component\Cache\Adapter\RedisAdapter
    arguments:
        $redisClient: '@app.my_redis_adapter'
        $namespace: 'sapData'
        $defaultLifetime: '%env(SAP_CACHE_TIMEOUT)%'

app.service.puc_pers_redis_adapter:
    class: Symfony\Component\Cache\Adapter\RedisAdapter
    arguments:
        $redisClient: '@app.my_redis_adapter'
        $namespace: 'persData'
        $defaultLifetime: '%env(CACHE_TIMEOUT)%'

这让我收到错误消息:

line: 62, 
file: "/var/www/vendor/symfony/cache/Traits/RedisTrait.php", 
message: "\"Symfony\\Component\\Cache\\Traits\\RedisTrait::init()\" 
expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\\ClientInterface, 
\"Symfony\\Component\\Cache\\Adapter\\RedisAdapter\" given."

如何为我的两个缓存池配置命名空间和过期时间?

标签: cachingredissymfony5

解决方案


经过几天的血汗和泪水,我把它留在这里,这样其他人就不必经历这种深深的绝望了。

这就是它的工作原理。您将不需要额外的类“只是”在您的环境文件夹中的这个漂亮的 cache.yaml:

framework:
    cache:
        pools:
            cache.sap:
                adapter: app.cache.adapter.sap_redis  # custom namespace and item expiration defined there
                provider: app.cache.custom_redis_provider # Which server connection should be used
            cache.pers:
                adapter: app.cache.adapter.pers_redis # custom namespace and item expiration defined there
                provider: app.cache.custom_redis_provider # Which server connection should be used

services:
    app.cache.custom_redis_provider: # this defines our connection to the redis server
        class: \Redis
        factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
        arguments:
            - 'redis://%env(string:REDIS_URL)%:%env(int:REDIS_PORT)%' # this defines the url to the redis server. "redis" up front is mandatory
            - { retry_interval: 2, timeout: 5 }  # defines number of connection retries and connection timeout (not item expiration!)

    app.cache.adapter.sap_redis: # here we pass namespace and expiration timeout into the constructor of the redis adapter
        parent: 'cache.adapter.redis'
        tags:
            - { name: 'cache.pool', namespace: 'sapData', default_lifetime: '%env(int:SAP_CACHE_TIMEOUT)%' }

    app.cache.adapter.pers_redis: # here we pass a different namespace and expiration timeout into the constructor of the redis adapter
        parent: 'cache.adapter.redis'
        tags:
            - { name: 'cache.pool', namespace: 'persData', default_lifetime: '%env(int:CACHE_TIMEOUT)%' }

推荐阅读