1
1
<?php
2
2
/***************************************************************************
3
- * Copyright (C) 2006-2008 by Konstantin V. Arkhipov *
3
+ * Copyright (C) 2006-2012 by Konstantin V. Arkhipov *
4
4
* *
5
5
* This program is free software; you can redistribute it and/or modify *
6
6
* it under the terms of the GNU Lesser General Public License as *
@@ -21,38 +21,36 @@ class PeclMemcached extends CachePeer
21
21
{
22
22
const DEFAULT_PORT = 11211 ;
23
23
const DEFAULT_HOST = '127.0.0.1 ' ;
24
+ const DEFAULT_TIMEOUT = 1 ;
24
25
25
- private $ instance = null ;
26
+ private $ instance = null ;
27
+ private $ requestTimeout = null ;
28
+ private $ connectTimeout = null ;
29
+ private $ host = null ;
30
+ private $ port = null ;
31
+ private $ triedConnect = false ;
26
32
27
33
/**
28
34
* @return PeclMemcached
29
35
**/
30
36
public static function create (
31
37
$ host = Memcached::DEFAULT_HOST ,
32
- $ port = Memcached::DEFAULT_PORT
38
+ $ port = Memcached::DEFAULT_PORT ,
39
+ $ connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
33
40
)
34
41
{
35
- return new self ($ host , $ port );
42
+ return new self ($ host , $ port, $ connectTimeout );
36
43
}
37
44
38
45
public function __construct (
39
46
$ host = Memcached::DEFAULT_HOST ,
40
- $ port = Memcached::DEFAULT_PORT
47
+ $ port = Memcached::DEFAULT_PORT ,
48
+ $ connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
41
49
)
42
50
{
43
- $ this ->instance = new Memcache ();
44
-
45
- try {
46
- try {
47
- $ this ->instance ->pconnect ($ host , $ port );
48
- } catch (BaseException $ e ) {
49
- $ this ->instance ->connect ($ host , $ port );
50
- }
51
-
52
- $ this ->alive = true ;
53
- } catch (BaseException $ e ) {
54
- // bad luck.
55
- }
51
+ $ this ->host = $ host ;
52
+ $ this ->port = $ port ;
53
+ $ this ->connectTimeout = $ connectTimeout ;
56
54
}
57
55
58
56
public function __destruct ()
@@ -66,11 +64,20 @@ public function __destruct()
66
64
}
67
65
}
68
66
67
+ public function isAlive ()
68
+ {
69
+ $ this ->ensureTriedToConnect ();
70
+
71
+ return parent ::isAlive ();
72
+ }
73
+
69
74
/**
70
75
* @return PeclMemcached
71
76
**/
72
77
public function clean ()
73
78
{
79
+ $ this ->ensureTriedToConnect ();
80
+
74
81
try {
75
82
$ this ->instance ->flush ();
76
83
} catch (BaseException $ e ) {
@@ -82,6 +89,8 @@ public function clean()
82
89
83
90
public function increment ($ key , $ value )
84
91
{
92
+ $ this ->ensureTriedToConnect ();
93
+
85
94
try {
86
95
return $ this ->instance ->increment ($ key , $ value );
87
96
} catch (BaseException $ e ) {
@@ -91,6 +100,8 @@ public function increment($key, $value)
91
100
92
101
public function decrement ($ key , $ value )
93
102
{
103
+ $ this ->ensureTriedToConnect ();
104
+
94
105
try {
95
106
return $ this ->instance ->decrement ($ key , $ value );
96
107
} catch (BaseException $ e ) {
@@ -100,6 +111,8 @@ public function decrement($key, $value)
100
111
101
112
public function getList ($ indexes )
102
113
{
114
+ $ this ->ensureTriedToConnect ();
115
+
103
116
return
104
117
($ return = $ this ->get ($ indexes ))
105
118
? $ return
@@ -108,6 +121,8 @@ public function getList($indexes)
108
121
109
122
public function get ($ index )
110
123
{
124
+ $ this ->ensureTriedToConnect ();
125
+
111
126
try {
112
127
return $ this ->instance ->get ($ index );
113
128
} catch (BaseException $ e ) {
@@ -124,6 +139,8 @@ public function get($index)
124
139
125
140
public function delete ($ index )
126
141
{
142
+ $ this ->ensureTriedToConnect ();
143
+
127
144
try {
128
145
// second parameter required, wrt new memcached protocol:
129
146
// delete key 0 (see process_delete_command in the memcached.c)
@@ -138,6 +155,8 @@ public function delete($index)
138
155
139
156
public function append ($ key , $ data )
140
157
{
158
+ $ this ->ensureTriedToConnect ();
159
+
141
160
try {
142
161
return $ this ->instance ->append ($ key , $ data );
143
162
} catch (BaseException $ e ) {
@@ -147,10 +166,58 @@ public function append($key, $data)
147
166
Assert::isUnreachable ();
148
167
}
149
168
169
+ /**
170
+ * @param float $requestTimeout time in seconds
171
+ * @return PeclMemcached
172
+ */
173
+ public function setTimeout ($ requestTimeout )
174
+ {
175
+ $ this ->ensureTriedToConnect ();
176
+ $ this ->requestTimeout = $ requestTimeout ;
177
+ $ this ->instance ->setServerParams ($ this ->host , $ this ->port , $ requestTimeout );
178
+
179
+ return $ this ;
180
+ }
181
+
182
+ /**
183
+ * @return float
184
+ */
185
+ public function getTimeout ()
186
+ {
187
+ return $ this ->requestTimeout ;
188
+ }
189
+
190
+ protected function ensureTriedToConnect ()
191
+ {
192
+ if ($ this ->triedConnect )
193
+ return $ this ;
194
+
195
+ $ this ->triedConnect = true ;
196
+ $ this ->instance = new Memcache ();
197
+
198
+ try {
199
+
200
+ try {
201
+ $ this ->instance ->pconnect ($ this ->host , $ this ->port , $ this ->connectTimeout );
202
+ } catch (BaseException $ e ) {
203
+ $ this ->instance ->connect ($ this ->host , $ this ->port , $ this ->connectTimeout );
204
+ }
205
+
206
+ $ this ->alive = true ;
207
+
208
+ } catch (BaseException $ e ) {
209
+ // bad luck.
210
+ }
211
+
212
+ return $ this ;
213
+ }
214
+
150
215
protected function store (
151
216
$ action , $ key , $ value , $ expires = Cache::EXPIRES_MEDIUM
152
217
)
153
218
{
219
+ $ this ->ensureTriedToConnect ();
220
+
154
221
try {
155
222
return
156
223
$ this ->instance ->$ action (
@@ -167,5 +234,5 @@ protected function store(
167
234
168
235
Assert::isUnreachable ();
169
236
}
237
+
170
238
}
171
- ?>
0 commit comments