11<?php
22/***************************************************************************
3- * Copyright (C) 2006-2008 by Konstantin V. Arkhipov *
3+ * Copyright (C) 2006-2012 by Konstantin V. Arkhipov *
44 * *
55 * This program is free software; you can redistribute it and/or modify *
66 * it under the terms of the GNU Lesser General Public License as *
@@ -21,38 +21,36 @@ class PeclMemcached extends CachePeer
2121 {
2222 const DEFAULT_PORT = 11211 ;
2323 const DEFAULT_HOST = '127.0.0.1 ' ;
24+ const DEFAULT_TIMEOUT = 1 ;
2425
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 ;
2632
2733 /**
2834 * @return PeclMemcached
2935 **/
3036 public static function create (
3137 $ host = Memcached::DEFAULT_HOST ,
32- $ port = Memcached::DEFAULT_PORT
38+ $ port = Memcached::DEFAULT_PORT ,
39+ $ connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
3340 )
3441 {
35- return new self ($ host , $ port );
42+ return new self ($ host , $ port, $ connectTimeout );
3643 }
3744
3845 public function __construct (
3946 $ host = Memcached::DEFAULT_HOST ,
40- $ port = Memcached::DEFAULT_PORT
47+ $ port = Memcached::DEFAULT_PORT ,
48+ $ connectTimeout = PeclMemcached::DEFAULT_TIMEOUT
4149 )
4250 {
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 ;
5654 }
5755
5856 public function __destruct ()
@@ -66,11 +64,20 @@ public function __destruct()
6664 }
6765 }
6866
67+ public function isAlive ()
68+ {
69+ $ this ->ensureTriedToConnect ();
70+
71+ return parent ::isAlive ();
72+ }
73+
6974 /**
7075 * @return PeclMemcached
7176 **/
7277 public function clean ()
7378 {
79+ $ this ->ensureTriedToConnect ();
80+
7481 try {
7582 $ this ->instance ->flush ();
7683 } catch (BaseException $ e ) {
@@ -82,6 +89,8 @@ public function clean()
8289
8390 public function increment ($ key , $ value )
8491 {
92+ $ this ->ensureTriedToConnect ();
93+
8594 try {
8695 return $ this ->instance ->increment ($ key , $ value );
8796 } catch (BaseException $ e ) {
@@ -91,6 +100,8 @@ public function increment($key, $value)
91100
92101 public function decrement ($ key , $ value )
93102 {
103+ $ this ->ensureTriedToConnect ();
104+
94105 try {
95106 return $ this ->instance ->decrement ($ key , $ value );
96107 } catch (BaseException $ e ) {
@@ -100,6 +111,8 @@ public function decrement($key, $value)
100111
101112 public function getList ($ indexes )
102113 {
114+ $ this ->ensureTriedToConnect ();
115+
103116 return
104117 ($ return = $ this ->get ($ indexes ))
105118 ? $ return
@@ -108,6 +121,8 @@ public function getList($indexes)
108121
109122 public function get ($ index )
110123 {
124+ $ this ->ensureTriedToConnect ();
125+
111126 try {
112127 return $ this ->instance ->get ($ index );
113128 } catch (BaseException $ e ) {
@@ -124,6 +139,8 @@ public function get($index)
124139
125140 public function delete ($ index )
126141 {
142+ $ this ->ensureTriedToConnect ();
143+
127144 try {
128145 // second parameter required, wrt new memcached protocol:
129146 // delete key 0 (see process_delete_command in the memcached.c)
@@ -138,6 +155,8 @@ public function delete($index)
138155
139156 public function append ($ key , $ data )
140157 {
158+ $ this ->ensureTriedToConnect ();
159+
141160 try {
142161 return $ this ->instance ->append ($ key , $ data );
143162 } catch (BaseException $ e ) {
@@ -147,10 +166,58 @@ public function append($key, $data)
147166 Assert::isUnreachable ();
148167 }
149168
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+
150215 protected function store (
151216 $ action , $ key , $ value , $ expires = Cache::EXPIRES_MEDIUM
152217 )
153218 {
219+ $ this ->ensureTriedToConnect ();
220+
154221 try {
155222 return
156223 $ this ->instance ->$ action (
@@ -167,5 +234,5 @@ protected function store(
167234
168235 Assert::isUnreachable ();
169236 }
237+
170238 }
171- ?>
0 commit comments