Skip to content

Commit 6b7393e

Browse files
committed
Merge pull request #116 from ents/RedisNoSQLLazyPullRequest
Redis no sql lazy
2 parents 83eb399 + a700da4 commit 6b7393e

File tree

3 files changed

+80
-19
lines changed

3 files changed

+80
-19
lines changed

core/Cache/ListGenerator.class.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/***************************************************************************
3+
* Copyright (C) 2012 by Artem Naumenko *
4+
* *
5+
* This program is free software; you can redistribute it and/or modify *
6+
* it under the terms of the GNU Lesser General Public License as *
7+
* published by the Free Software Foundation; either version 3 of the *
8+
* License, or (at your option) any later version. *
9+
* *
10+
***************************************************************************/
11+
12+
/**
13+
* @param string $key
14+
*
15+
* @return Listable
16+
*/
17+
18+
interface ListGenerator
19+
{
20+
public function fetchList($key);
21+
}

core/DB/NoSQL/RedisNoSQL.class.php

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
* *
1010
***************************************************************************/
1111

12-
final class RedisNoSQL extends CachePeer
12+
final class RedisNoSQL extends CachePeer implements ListGenerator
1313
{
1414
const DEFAULT_HOST = 'localhost';
1515
const DEFAULT_PORT = '6379';
1616
const DEFAULT_TIMEOUT = 1.0;
1717

18-
private $redis = null;
19-
private $host = null;
20-
private $port = null;
21-
private $timeout = null;
18+
private $redis = null;
19+
private $host = null;
20+
private $port = null;
21+
private $timeout = null;
22+
private $triedConnect = false;
2223

2324
/**
2425
* @param type $host
@@ -44,15 +45,6 @@ public function __construct(
4445
$this->host = $host;
4546
$this->port = $port;
4647
$this->timeout = $timeout;
47-
48-
$this->redis = new Redis();
49-
50-
try {
51-
$this->redis->pconnect($this->host, $this->port, $this->timeout);
52-
$this->isAlive();
53-
} catch (RedisException $e) {
54-
$this->alive = false;
55-
}
5648
}
5749

5850
public function __destruct()
@@ -68,6 +60,8 @@ public function __destruct()
6860

6961
public function clean()
7062
{
63+
$this->ensureTriedToConnect();
64+
7165
try {
7266
$this->redis->flushDB();
7367
} catch (RedisException $e) {
@@ -79,6 +73,8 @@ public function clean()
7973

8074
public function isAlive()
8175
{
76+
$this->ensureTriedToConnect();
77+
8278
try {
8379
$this->alive = $this->redis->ping() == '+PONG';
8480
} catch (RedisException $e) {
@@ -90,6 +86,8 @@ public function isAlive()
9086

9187
public function append($key, $data)
9288
{
89+
$this->ensureTriedToConnect();
90+
9391
try {
9492
return $this->redis->append($key, $data);
9593
} catch (RedisException $e) {
@@ -99,6 +97,8 @@ public function append($key, $data)
9997

10098
public function decrement($key, $value)
10199
{
100+
$this->ensureTriedToConnect();
101+
102102
try {
103103
return $this->redis->decrBy($key, $value);
104104
} catch (RedisException $e) {
@@ -108,6 +108,8 @@ public function decrement($key, $value)
108108

109109
public function delete($key)
110110
{
111+
$this->ensureTriedToConnect();
112+
111113
try {
112114
return $this->redis->delete($key);
113115
} catch (RedisException $e) {
@@ -117,6 +119,8 @@ public function delete($key)
117119

118120
public function get($key)
119121
{
122+
$this->ensureTriedToConnect();
123+
120124
try {
121125
return $this->redis->get($key);
122126
} catch (RedisException $e) {
@@ -128,6 +132,8 @@ public function get($key)
128132

129133
public function increment($key, $value)
130134
{
135+
$this->ensureTriedToConnect();
136+
131137
try {
132138
return $this->redis->incrBy($key, $value);
133139
} catch (RedisException $e) {
@@ -140,15 +146,17 @@ public function increment($key, $value)
140146
*
141147
* @return RedisNoSQLList
142148
*/
143-
public function fetchList($key)
149+
public function fetchList($key, $timeout = null)
144150
{
145-
return new RedisNoSQLList($this->redis, $key);
151+
$this->ensureTriedToConnect();
152+
153+
return new RedisNoSQLList($this->redis, $key, $timeout);
146154
}
147155

148156
/**
149157
* @param string $key
150158
*
151-
* @return ISet
159+
* @return RedisNoSQLSet
152160
*/
153161
public function fetchSet($key)
154162
{
@@ -158,7 +166,7 @@ public function fetchSet($key)
158166
/**
159167
* @param string $key
160168
*
161-
* @return IHash
169+
* @return RedisNoSQLHash
162170
*/
163171
public function fetchHash($key)
164172
{
@@ -167,6 +175,8 @@ public function fetchHash($key)
167175

168176
protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM)
169177
{
178+
$this->ensureTriedToConnect();
179+
170180
switch ($action) {
171181
case 'set':
172182
case 'replace':
@@ -181,5 +191,24 @@ protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
181191
throw new UnimplementedFeatureException();
182192
}
183193
}
194+
195+
protected function ensureTriedToConnect()
196+
{
197+
if ($this->triedConnect)
198+
return $this;
199+
200+
$this->triedConnect = true;
201+
202+
$this->redis = new Redis();
203+
204+
try {
205+
$this->redis->pconnect($this->host, $this->port, $this->timeout);
206+
$this->isAlive();
207+
} catch (RedisException $e) {
208+
$this->alive = false;
209+
}
210+
211+
return $this;
212+
}
184213
}
185214

core/DB/NoSQL/RedisNoSQLList.class.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ final class RedisNoSQLList implements Listable
1414
private $redis = null;
1515
private $key = null;
1616
private $position = null;
17+
private $timeout = null;
1718

18-
public function __construct(Redis $redis, $key)
19+
public function __construct(Redis $redis, $key, $timeout = null)
1920
{
2021
$this->redis = $redis;
2122
$this->key = $key;
23+
$this->timeout = $timeout;
2224
}
2325

2426
/**
@@ -29,6 +31,9 @@ public function append($value)
2931
{
3032
$this->redis->rpush($this->key, $value);
3133

34+
if ($this->timeout)
35+
$this->redis->setTimeout($this->key, $this->timeout);
36+
3237
return $this;
3338
}
3439

@@ -40,6 +45,9 @@ public function prepend($value)
4045
{
4146
$this->redis->lpush($this->key, $value);
4247

48+
if ($this->timeout)
49+
$this->redis->setTimeout($this->key, $this->timeout);
50+
4351
return $this;
4452
}
4553

@@ -82,6 +90,9 @@ public function set($index, $value)
8290
{
8391
$this->redis->lset($this->key, $index, $value);
8492

93+
if ($this->timeout)
94+
$this->redis->expire($this->key, $this->timeout);
95+
8596
return $this;
8697
}
8798

0 commit comments

Comments
 (0)