1+ <?php
2+ /***************************************************************************
3+ * Copyright (C) 2006-2008 by Konstantin V. Arkhipov *
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+ * Connector for PECL's Memcache extension by Antony Dovgal.
14+ *
15+ * @see http://tony2001.phpclub.net/
16+ * @see http://pecl.php.net/package/memcache
17+ *
18+ * @ingroup Cache
19+ **/
20+ class PeclMemcache extends CachePeer
21+ {
22+ const DEFAULT_PORT = 11211 ;
23+ const DEFAULT_HOST = '127.0.0.1 ' ;
24+
25+ private $ instance = null ;
26+
27+ /**
28+ * @return PeclMemcache
29+ **/
30+ public static function create (
31+ $ host = self ::DEFAULT_HOST ,
32+ $ port = self ::DEFAULT_PORT
33+ )
34+ {
35+ return new self ($ host , $ port );
36+ }
37+
38+ public function __construct (
39+ $ host = self ::DEFAULT_HOST ,
40+ $ port = self ::DEFAULT_PORT
41+ )
42+ {
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+ }
56+ }
57+
58+ public function __destruct ()
59+ {
60+ if ($ this ->alive ) {
61+ try {
62+ $ this ->instance ->close ();
63+ } catch (BaseException $ e ) {
64+ // shhhh.
65+ }
66+ }
67+ }
68+
69+ /**
70+ * @return PeclMemcached
71+ **/
72+ public function clean ()
73+ {
74+ try {
75+ $ this ->instance ->flush ();
76+ } catch (BaseException $ e ) {
77+ $ this ->alive = false ;
78+ }
79+
80+ return parent ::clean ();
81+ }
82+
83+ public function increment ($ key , $ value )
84+ {
85+ try {
86+ return $ this ->instance ->increment ($ key , $ value );
87+ } catch (BaseException $ e ) {
88+ return null ;
89+ }
90+ }
91+
92+ public function decrement ($ key , $ value )
93+ {
94+ try {
95+ return $ this ->instance ->decrement ($ key , $ value );
96+ } catch (BaseException $ e ) {
97+ return null ;
98+ }
99+ }
100+
101+ public function getList ($ indexes )
102+ {
103+ return
104+ ($ return = $ this ->get ($ indexes ))
105+ ? $ return
106+ : array ();
107+ }
108+
109+ public function get ($ index )
110+ {
111+ try {
112+ return $ this ->instance ->get ($ index );
113+ } catch (BaseException $ e ) {
114+ if (strpos ($ e ->getMessage (), 'Invalid key ' ) !== false )
115+ return null ;
116+
117+ $ this ->alive = false ;
118+
119+ return null ;
120+ }
121+
122+ Assert::isUnreachable ();
123+ }
124+
125+ public function delete ($ index )
126+ {
127+ try {
128+ // second parameter required, wrt new memcached protocol:
129+ // delete key 0 (see process_delete_command in the memcached.c)
130+ // Warning: it is workaround!
131+ return $ this ->instance ->delete ($ index , 0 );
132+ } catch (BaseException $ e ) {
133+ return $ this ->alive = false ;
134+ }
135+
136+ Assert::isUnreachable ();
137+ }
138+
139+ public function append ($ key , $ data )
140+ {
141+ try {
142+ return $ this ->instance ->append ($ key , $ data );
143+ } catch (BaseException $ e ) {
144+ return $ this ->alive = false ;
145+ }
146+
147+ Assert::isUnreachable ();
148+ }
149+
150+ protected function store (
151+ $ action , $ key , $ value , $ expires = Cache::EXPIRES_MEDIUM
152+ )
153+ {
154+ try {
155+ return
156+ $ this ->instance ->$ action (
157+ $ key ,
158+ $ value ,
159+ $ this ->compress
160+ ? MEMCACHE_COMPRESSED
161+ : false ,
162+ $ expires
163+ );
164+ } catch (BaseException $ e ) {
165+ return $ this ->alive = false ;
166+ }
167+
168+ Assert::isUnreachable ();
169+ }
170+ }
171+ ?>
0 commit comments