I try to configure two cache pools in my Synfony5 app to use a certain namespace and set a default expiration date for the items. After trying for the umpteenth time the umteenth variation I get the feeling that my configuration is going in circles.
What I understood so far:
In the constructor of the RedisAdapter you can set the namespace and the default expiration time
In the createConnection method you set the url of your redis server.
However the constructor of the RedisAdapter seems to already need a redis client (= redis connection?)
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);
}
How can I inject my namespaces and defaultLifetimes into the RedisAdapter?
What I tried so far:
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)%'
This gets me the error message:
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."
How can I configure the namespaces and expiration time for my two cache pools?
2
Answers
After several days of blood, sweat and tears I leave this here so nobody else will have to experience this deep desperation.
This is how it works. You will need no extra class "just" this nifty cache.yaml in the folder for your environment:
You can also set those parameters within the usual cache-pool configuration.
The page (linked above) goes on to say how to tag a service for a specific namespace, but the various configured pools already have one set by default: