Please consider reworking the Redis::connect API to better support Dependency Injection. Currently connection configuration must be passed when connect() is called, which results in poor encapsulation.
What I would suggest doing is separating the connection from the configuration so the connection can be established lazily.
Example:
class RedisCache implements CacheInterface {
private $redis;
public function __construct(Redis $client) {
$this->redis = $client;
}
private function getConnection()
{
// why should RedisCache care about the server host, it just needs a redis connection?
$client->isConnected() || $client->connect();
return $this->redis;
}
/** implement CacheInterface *//
}
$redis = new Redis();
$redis->setHost('somehost');
$redis->setPort(6379);
$cache = new RedisCache($redis);
// reuse connection
$logger = new RedisLogger($redis);
// if logger and cache are never used, no connection will be made.
Thanks.
I would like to +1, as I convert an old app from memcache --> redis, looking for a replacement for http://php.net/manual/en/memcache.addserver.php, which is described: Thus there is no overhead in adding a large number of servers to the pool, even though they might not all be used.
Thanks again for this stable extension, unlike the entire memcache(d) mess!
Would be really cool to have a lazy connection :+1:
Still interested for this as it would avoid the usage of dynamic proxy and issues like snc/SncRedisBundle#559
We will discuss this issue with @michael-grunder. I have a thoughts to completely remove connect method and always make connection lazy
I like the idea as well, we just can't do it until a major version bump.
I've created a 6.0 milestone and added this feature to it. Ideally I would like to do a rather substantial rewrite for PhpRedis 6 with a bunch of big changes, but it will come down to how much time we can reasonably invest in such an endeavor.
Thanks to both of you :pray:
Most helpful comment
We will discuss this issue with @michael-grunder. I have a thoughts to completely remove
connectmethod and always make connection lazy