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

How to set namespace and item expiration for a symfony5 redis cachepool

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

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

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

/**
 * @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

services.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."

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

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

这就是它的工作原理。您将不需要额外的 class “只是”文件夹中适合您的环境的这个漂亮的 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)%' }

您也可以在通常的 cache-pool configuration 内设置这些参数。

framework:
    cache:
        default_memcached_provider: 'memcached://localhost'
        # could also replace with 
        # default_redis_provider: 'redis://localhost' # or '%env(REDIS_DSN)%'

        pools:
            # creates a "custom_thing.cache" service
            # autowireable via "CacheInterface $customThingCache"
            # uses the "app" cache configuration
            custom_thing.cache:
                adapter: cache.app 

            # creates a "my_cache_pool" service
            # autowireable via "CacheInterface $myCachePool"
            my_cache_pool:
                adapter: cache.adapter.filesystem

            # uses the default_memcached_provider from above
            acme.cache:
                adapter: cache.adapter.memcached

            # control adapter's configuration - customised provider adaptor & DSN
            foobar.cache:
                adapter: cache.adapter.memcached
                provider: 'memcached://user:password@example.com'

            # uses the "foobar.cache" pool as its backend but controls
            # the lifetime and (like all pools) has a separate cache namespace
            short_cache:
                adapter: foobar.cache
                default_lifetime: 60

该页面(上面的链接)继续说明如何为特定命名空间标记服务,但默认情况下各种配置的池已经有一组:

Each pool manages a set of independent cache keys: keys from different pools never collide, even if they share the same backend. This is achieved by prefixing keys with a namespace that’s generated by hashing the name of the pool, the name of the compiled container class and a configurable seed that defaults to the project directory.