79f32410bb6d9210e41d5f72942c97faa0d187e0
[lhc/web/wiklou.git] / includes / objectcache / MemcachedPhpBagOStuff.php
1 <?php
2 /**
3 * Object caching using memcached.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
26 *
27 * @ingroup Cache
28 */
29 class MemcachedPhpBagOStuff extends BagOStuff {
30
31 /**
32 * @var MemCachedClientforWiki
33 */
34 protected $client;
35
36 /**
37 * Constructor.
38 *
39 * Available parameters are:
40 * - servers: The list of IP:port combinations holding the memcached servers.
41 * - debug: Whether to set the debug flag in the underlying client.
42 * - persistent: Whether to use a persistent connection
43 * - compress_threshold: The minimum size an object must be before it is compressed
44 * - timeout: The read timeout in microseconds
45 * - connect_timeout: The connect timeout in seconds
46 *
47 * @param $params array
48 */
49 function __construct( $params ) {
50 if ( !isset( $params['servers'] ) ) {
51 $params['servers'] = $GLOBALS['wgMemCachedServers'];
52 }
53 if ( !isset( $params['debug'] ) ) {
54 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
55 }
56 if ( !isset( $params['persistent'] ) ) {
57 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
58 }
59 if ( !isset( $params['compress_threshold'] ) ) {
60 $params['compress_threshold'] = 1500;
61 }
62 if ( !isset( $params['timeout'] ) ) {
63 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
64 }
65 if ( !isset( $params['connect_timeout'] ) ) {
66 $params['connect_timeout'] = 0.1;
67 }
68
69 $this->client = new MemCachedClientforWiki( $params );
70 $this->client->set_servers( $params['servers'] );
71 $this->client->set_debug( $params['debug'] );
72 }
73
74 /**
75 * @param $debug bool
76 */
77 public function setDebug( $debug ) {
78 $this->client->set_debug( $debug );
79 }
80
81 /**
82 * @param $key string
83 * @return Mixed
84 */
85 public function get( $key ) {
86 return $this->client->get( $this->encodeKey( $key ) );
87 }
88
89 /**
90 * @param $keys Array
91 * @return Array
92 */
93 public function getBatch( array $keys ) {
94 $callback = array( $this, 'encodeKey' );
95 return $this->client->get_multi( array_map( $callback, $keys ) );
96 }
97
98 /**
99 * @param $key string
100 * @param $value
101 * @param $exptime int
102 * @return bool
103 */
104 public function set( $key, $value, $exptime = 0 ) {
105 return $this->client->set( $this->encodeKey( $key ), $value, $exptime );
106 }
107
108 /**
109 * @param $key string
110 * @param $time int
111 * @return bool
112 */
113 public function delete( $key, $time = 0 ) {
114 return $this->client->delete( $this->encodeKey( $key ), $time );
115 }
116
117 /**
118 * @param $key
119 * @param $timeout int
120 * @return
121 */
122 public function lock( $key, $timeout = 0 ) {
123 return $this->client->lock( $this->encodeKey( $key ), $timeout );
124 }
125
126 /**
127 * @param $key string
128 * @return Mixed
129 */
130 public function unlock( $key ) {
131 return $this->client->unlock( $this->encodeKey( $key ) );
132 }
133
134 /**
135 * @param $key string
136 * @param $value int
137 * @return Mixed
138 */
139 public function add( $key, $value, $exptime = 0 ) {
140 return $this->client->add( $this->encodeKey( $key ), $value, $exptime );
141 }
142
143 /**
144 * @param $key string
145 * @param $value int
146 * @param $exptime
147 * @return Mixed
148 */
149 public function replace( $key, $value, $exptime = 0 ) {
150 return $this->client->replace( $this->encodeKey( $key ), $value, $exptime );
151 }
152
153 /**
154 * @param $key string
155 * @param $value int
156 * @return Mixed
157 */
158 public function incr( $key, $value = 1 ) {
159 return $this->client->incr( $this->encodeKey( $key ), $value );
160 }
161
162 /**
163 * @param $key string
164 * @param $value int
165 * @return Mixed
166 */
167 public function decr( $key, $value = 1 ) {
168 return $this->client->decr( $this->encodeKey( $key ), $value );
169 }
170
171 /**
172 * Get the underlying client object. This is provided for debugging
173 * purposes.
174 *
175 * @return MemCachedClientforWiki
176 */
177 public function getClient() {
178 return $this->client;
179 }
180
181 /**
182 * Encode a key for use on the wire inside the memcached protocol.
183 *
184 * We encode spaces and line breaks to avoid protocol errors. We encode
185 * the other control characters for compatibility with libmemcached
186 * verify_key. We leave other punctuation alone, to maximise backwards
187 * compatibility.
188 * @return string
189 */
190 public function encodeKey( $key ) {
191 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
192 array( $this, 'encodeKeyCallback' ), $key );
193 }
194
195 protected function encodeKeyCallback( $m ) {
196 return rawurlencode( $m[0] );
197 }
198
199 /**
200 * Decode a key encoded with encodeKey(). This is provided as a convenience
201 * function for debugging.
202 *
203 * @param $key string
204 *
205 * @return string
206 */
207 public function decodeKey( $key ) {
208 return urldecode( $key );
209 }
210 }
211