我尝试在我的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_adapterservices.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."如何为两个缓存池配置名称空间和过期时间?
发布于 2021-06-29 14:00:32
经过几天的流血、汗水和泪水之后,我把它留在这里,这样其他人就不会经历这种极度的绝望了。
这就是它的工作原理。您将不需要额外的类“只是”这个漂亮的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)%' }发布于 2021-07-14 21:26:43
您还可以在通常的缓存池配置中设置这些参数。
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该页(上面链接)继续说明如何为特定命名空间标记服务,但默认情况下,各种配置池已经设置了一个:
每个池管理一组独立的缓存密钥:来自不同池的密钥永远不会冲突,即使它们共享相同的后端。这是通过对池的名称、已编译容器类的名称以及默认为项目目录的可配置种子进行散列生成的名称空间前缀键来实现的。
https://stackoverflow.com/questions/68162916
复制相似问题