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



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

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

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

/**
* @param Redis|RedisArray|RedisCluster|PredisClientInterface $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: ['SymfonyComponentCacheAdapterRedisAdapter', 'createConnection']
arguments:
- 'redis://%env(string:REDIS_URL)%:%env(int:REDIS_PORT)%'
- { retry_interval: 2, timeout: 5 }
app.service.puc_sap_redis_adapter:
class: SymfonyComponentCacheAdapterRedisAdapter
arguments:
$redisClient: '@app.my_redis_adapter'
$namespace: 'sapData'
$defaultLifetime: '%env(SAP_CACHE_TIMEOUT)%'
app.service.puc_pers_redis_adapter:
class: SymfonyComponentCacheAdapterRedisAdapter
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."

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

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

这就是它的工作原理。你将不需要额外的课程";只是";这个漂亮的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: ['SymfonyComponentCacheAdapterRedisAdapter', '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)%' }

您还可以在通常的缓存池配置中设置这些参数。

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

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

每个池管理一组独立的缓存键:来自不同池的键永远不会冲突,即使它们共享相同的后端。这是通过在键前面加一个命名空间来实现的,该命名空间是通过哈希池的名称、编译的容器类的名称和默认为项目目录的可配置种子生成的。

最新更新