9
9
* *
10
10
***************************************************************************/
11
11
12
- final class RedisNoSQL extends CachePeer
12
+ final class RedisNoSQL extends CachePeer implements ListGenerator
13
13
{
14
14
const DEFAULT_HOST = 'localhost ' ;
15
15
const DEFAULT_PORT = '6379 ' ;
16
16
const DEFAULT_TIMEOUT = 1.0 ;
17
17
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 ;
22
23
23
24
/**
24
25
* @param type $host
@@ -44,15 +45,6 @@ public function __construct(
44
45
$ this ->host = $ host ;
45
46
$ this ->port = $ port ;
46
47
$ 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
- }
56
48
}
57
49
58
50
public function __destruct ()
@@ -68,6 +60,8 @@ public function __destruct()
68
60
69
61
public function clean ()
70
62
{
63
+ $ this ->ensureTriedToConnect ();
64
+
71
65
try {
72
66
$ this ->redis ->flushDB ();
73
67
} catch (RedisException $ e ) {
@@ -79,6 +73,8 @@ public function clean()
79
73
80
74
public function isAlive ()
81
75
{
76
+ $ this ->ensureTriedToConnect ();
77
+
82
78
try {
83
79
$ this ->alive = $ this ->redis ->ping () == '+PONG ' ;
84
80
} catch (RedisException $ e ) {
@@ -90,6 +86,8 @@ public function isAlive()
90
86
91
87
public function append ($ key , $ data )
92
88
{
89
+ $ this ->ensureTriedToConnect ();
90
+
93
91
try {
94
92
return $ this ->redis ->append ($ key , $ data );
95
93
} catch (RedisException $ e ) {
@@ -99,6 +97,8 @@ public function append($key, $data)
99
97
100
98
public function decrement ($ key , $ value )
101
99
{
100
+ $ this ->ensureTriedToConnect ();
101
+
102
102
try {
103
103
return $ this ->redis ->decrBy ($ key , $ value );
104
104
} catch (RedisException $ e ) {
@@ -108,6 +108,8 @@ public function decrement($key, $value)
108
108
109
109
public function delete ($ key )
110
110
{
111
+ $ this ->ensureTriedToConnect ();
112
+
111
113
try {
112
114
return $ this ->redis ->delete ($ key );
113
115
} catch (RedisException $ e ) {
@@ -117,6 +119,8 @@ public function delete($key)
117
119
118
120
public function get ($ key )
119
121
{
122
+ $ this ->ensureTriedToConnect ();
123
+
120
124
try {
121
125
return $ this ->redis ->get ($ key );
122
126
} catch (RedisException $ e ) {
@@ -128,6 +132,8 @@ public function get($key)
128
132
129
133
public function increment ($ key , $ value )
130
134
{
135
+ $ this ->ensureTriedToConnect ();
136
+
131
137
try {
132
138
return $ this ->redis ->incrBy ($ key , $ value );
133
139
} catch (RedisException $ e ) {
@@ -140,15 +146,17 @@ public function increment($key, $value)
140
146
*
141
147
* @return RedisNoSQLList
142
148
*/
143
- public function fetchList ($ key )
149
+ public function fetchList ($ key, $ timeout = null )
144
150
{
145
- return new RedisNoSQLList ($ this ->redis , $ key );
151
+ $ this ->ensureTriedToConnect ();
152
+
153
+ return new RedisNoSQLList ($ this ->redis , $ key , $ timeout );
146
154
}
147
155
148
156
/**
149
157
* @param string $key
150
158
*
151
- * @return ISet
159
+ * @return RedisNoSQLSet
152
160
*/
153
161
public function fetchSet ($ key )
154
162
{
@@ -158,7 +166,7 @@ public function fetchSet($key)
158
166
/**
159
167
* @param string $key
160
168
*
161
- * @return IHash
169
+ * @return RedisNoSQLHash
162
170
*/
163
171
public function fetchHash ($ key )
164
172
{
@@ -167,6 +175,8 @@ public function fetchHash($key)
167
175
168
176
protected function store ($ action , $ key , $ value , $ expires = Cache::EXPIRES_MEDIUM )
169
177
{
178
+ $ this ->ensureTriedToConnect ();
179
+
170
180
switch ($ action ) {
171
181
case 'set ' :
172
182
case 'replace ' :
@@ -181,5 +191,24 @@ protected function store($action, $key, $value, $expires = Cache::EXPIRES_MEDIUM
181
191
throw new UnimplementedFeatureException ();
182
192
}
183
193
}
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
+ }
184
213
}
185
214
0 commit comments