如何为 symfony5 redis 缓存池设置命名空间和项目过期

Cal*_*ane 0 caching redis symfony5

我尝试在 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);
}
Run Code Online (Sandbox Code Playgroud)

如何将命名空间和 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
Run Code Online (Sandbox Code Playgroud)

服务.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)%'
Run Code Online (Sandbox Code Playgroud)

这让我收到错误消息:

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."
Run Code Online (Sandbox Code Playgroud)

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

Cal*_*ane 5

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

这就是它的工作原理。您不需要额外的类,“只需”在您的环境文件夹中使用这个漂亮的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)%' }
Run Code Online (Sandbox Code Playgroud)