diff --git a/core/Cache/ListGenerator.class.php b/core/Cache/ListGenerator.class.php new file mode 100644 index 0000000000..6e20f24f72 --- /dev/null +++ b/core/Cache/ListGenerator.class.php @@ -0,0 +1,21 @@ +host = $host; $this->port = $port; $this->timeout = $timeout; - - $this->redis = new Redis(); - - try { - $this->redis->pconnect($this->host, $this->port, $this->timeout); - $this->isAlive(); - } catch (RedisException $e) { - $this->alive = false; - } } public function __destruct() @@ -68,6 +60,8 @@ public function __destruct() public function clean() { + $this->ensureTriedToConnect(); + try { $this->redis->flushDB(); } catch (RedisException $e) { @@ -79,6 +73,8 @@ public function clean() public function isAlive() { + $this->ensureTriedToConnect(); + try { $this->alive = $this->redis->ping() == '+PONG'; } catch (RedisException $e) { @@ -90,6 +86,8 @@ public function isAlive() public function append($key, $data) { + $this->ensureTriedToConnect(); + try { return $this->redis->append($key, $data); } catch (RedisException $e) { @@ -99,6 +97,8 @@ public function append($key, $data) public function decrement($key, $value) { + $this->ensureTriedToConnect(); + try { return $this->redis->decrBy($key, $value); } catch (RedisException $e) { @@ -108,6 +108,8 @@ public function decrement($key, $value) public function delete($key) { + $this->ensureTriedToConnect(); + try { return $this->redis->delete($key); } catch (RedisException $e) { @@ -117,6 +119,8 @@ public function delete($key) public function get($key) { + $this->ensureTriedToConnect(); + try { return $this->redis->get($key); } catch (RedisException $e) { @@ -128,6 +132,8 @@ public function get($key) public function increment($key, $value) { + $this->ensureTriedToConnect(); + try { return $this->redis->incrBy($key, $value); } catch (RedisException $e) { @@ -140,15 +146,17 @@ public function increment($key, $value) * * @return RedisNoSQLList */ - public function fetchList($key) + public function fetchList($key, $timeout = null) { - return new RedisNoSQLList($this->redis, $key); + $this->ensureTriedToConnect(); + + return new RedisNoSQLList($this->redis, $key, $timeout); } /** * @param string $key * - * @return ISet + * @return RedisNoSQLSet */ public function fetchSet($key) { @@ -158,7 +166,7 @@ public function fetchSet($key) /** * @param string $key * - * @return IHash + * @return RedisNoSQLHash */ public function fetchHash($key) { @@ -167,6 +175,8 @@ public function fetchHash($key) protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM) { + $this->ensureTriedToConnect(); + switch ($action) { case 'set': case 'replace': @@ -181,5 +191,24 @@ protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM throw new UnimplementedFeatureException(); } } + + protected function ensureTriedToConnect() + { + if ($this->triedConnect) + return $this; + + $this->triedConnect = true; + + $this->redis = new Redis(); + + try { + $this->redis->pconnect($this->host, $this->port, $this->timeout); + $this->isAlive(); + } catch (RedisException $e) { + $this->alive = false; + } + + return $this; + } } diff --git a/core/DB/NoSQL/RedisNoSQLList.class.php b/core/DB/NoSQL/RedisNoSQLList.class.php index fa949c1b75..83360dc1d3 100644 --- a/core/DB/NoSQL/RedisNoSQLList.class.php +++ b/core/DB/NoSQL/RedisNoSQLList.class.php @@ -14,11 +14,13 @@ final class RedisNoSQLList implements Listable private $redis = null; private $key = null; private $position = null; + private $timeout = null; - public function __construct(Redis $redis, $key) + public function __construct(Redis $redis, $key, $timeout = null) { $this->redis = $redis; $this->key = $key; + $this->timeout = $timeout; } /** @@ -29,6 +31,9 @@ public function append($value) { $this->redis->rpush($this->key, $value); + if ($this->timeout) + $this->redis->setTimeout($this->key, $this->timeout); + return $this; } @@ -40,6 +45,9 @@ public function prepend($value) { $this->redis->lpush($this->key, $value); + if ($this->timeout) + $this->redis->setTimeout($this->key, $this->timeout); + return $this; } @@ -82,6 +90,9 @@ public function set($index, $value) { $this->redis->lset($this->key, $index, $value); + if ($this->timeout) + $this->redis->expire($this->key, $this->timeout); + return $this; }