Symfony cache redis adapter with username and password

Recently started a new project that uses Doctrine and symfony cache.

For symfony cache I always strongly prefer phpredis with igbinary marshalling.

One issue I ran into was that my redis server is configured to use username and password while symfony cache redis adapter docs didn’t cover this scenario at the time of writing, unless I missed it and I couldnt find any example online that would show it.

Took me couple of minutes to figure out – so I wanted to share perhaps it helps someone, but the idea is – don’t use createConnection helper as in docs and just configure phpredis client yourself:

$client = new \Redis();
$isConnected = $client->pconnect($host = $config['host'], $port = $config['port'], $connectTimeout = 2.5, $reserved = null, $retryInterval = 1000, $readTimeout = 30);

//This line
$client->auth(['user' => $user,  'pass' => $pass]);

// Make sure we set igbinary as default marshaller explicitely
$marshaller = new DefaultMarshaller(true);

$cache = new RedisAdapter(
    $client,
    // the string prefixed to the keys of the items stored in this cache
    $namespacePrefix ='someprefix',
    $defaultLifetime = 60 * 60,
    $marshaller,
); 

Leave a Comment