From b709c8a08b152ed3632825a765b9b988ea31b2ab Mon Sep 17 00:00:00 2001 From: anaumenko Date: Tue, 10 Jul 2012 17:35:46 +0400 Subject: [PATCH 1/2] RedisNoSQL is lazy now --- core/DB/NoSQL/RedisNoSQL.class.php | 65 +++++++++++++++++++------- core/DB/NoSQL/RedisNoSQLList.class.php | 13 +++++- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/core/DB/NoSQL/RedisNoSQL.class.php b/core/DB/NoSQL/RedisNoSQL.class.php index d6c1b1a86c..a1b93ac5d6 100644 --- a/core/DB/NoSQL/RedisNoSQL.class.php +++ b/core/DB/NoSQL/RedisNoSQL.class.php @@ -9,16 +9,17 @@ * * ***************************************************************************/ - final class RedisNoSQL extends CachePeer + final class RedisNoSQL extends CachePeer implements ListGenerator { const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = '6379'; const DEFAULT_TIMEOUT = 1.0; - private $redis = null; - private $host = null; - private $port = null; - private $timeout = null; + private $redis = null; + private $host = null; + private $port = null; + private $timeout = null; + private $triedConnect = false; /** * @param type $host @@ -44,15 +45,6 @@ public function __construct( $this->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; } From a700da4020dd714dc49dda1157c08cca4c889e8d Mon Sep 17 00:00:00 2001 From: anaumenko Date: Tue, 10 Jul 2012 17:36:05 +0400 Subject: [PATCH 2/2] RedisNoSQL is lazy now --- core/Cache/ListGenerator.class.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 core/Cache/ListGenerator.class.php 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 @@ +