merged from master
[lhc/web/wiklou.git] / includes / objectcache / MemcachedPhpBagOStuff.php
1 <?php
2
3 /**
4 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
5 */
6 class MemcachedPhpBagOStuff extends BagOStuff {
7
8 /**
9 * @var MemCachedClientforWiki
10 */
11 protected $client;
12
13 /**
14 * Constructor.
15 *
16 * Available parameters are:
17 * - servers: The list of IP:port combinations holding the memcached servers.
18 * - debug: Whether to set the debug flag in the underlying client.
19 * - persistent: Whether to use a persistent connection
20 * - compress_threshold: The minimum size an object must be before it is compressed
21 * - timeout: The read timeout in microseconds
22 * - connect_timeout: The connect timeout in seconds
23 *
24 * @param $params array
25 */
26 function __construct( $params ) {
27 if ( !isset( $params['servers'] ) ) {
28 $params['servers'] = $GLOBALS['wgMemCachedServers'];
29 }
30 if ( !isset( $params['debug'] ) ) {
31 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
32 }
33 if ( !isset( $params['persistent'] ) ) {
34 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
35 }
36 if ( !isset( $params['compress_threshold'] ) ) {
37 $params['compress_threshold'] = 1500;
38 }
39 if ( !isset( $params['timeout'] ) ) {
40 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
41 }
42 if ( !isset( $params['connect_timeout'] ) ) {
43 $params['connect_timeout'] = 0.1;
44 }
45
46 $this->client = new MemCachedClientforWiki( $params );
47 $this->client->set_servers( $params['servers'] );
48 $this->client->set_debug( $params['debug'] );
49 }
50
51 /**
52 * @param $debug bool
53 */
54 public function setDebug( $debug ) {
55 $this->client->set_debug( $debug );
56 }
57
58 /**
59 * @param $key string
60 * @return Mixed
61 */
62 public function get( $key ) {
63 return $this->client->get( $this->encodeKey( $key ) );
64 }
65
66 /**
67 * @param $keys Array
68 * @return Array
69 */
70 public function getBatch( array $keys ) {
71 $callback = array( $this, 'encodeKey' );
72 return $this->client->get_multi( array_map( $callback, $keys ) );
73 }
74
75 /**
76 * @param $key string
77 * @param $value
78 * @param $exptime int
79 * @return bool
80 */
81 public function set( $key, $value, $exptime = 0 ) {
82 return $this->client->set( $this->encodeKey( $key ), $value, $exptime );
83 }
84
85 /**
86 * @param $key string
87 * @param $time int
88 * @return bool
89 */
90 public function delete( $key, $time = 0 ) {
91 return $this->client->delete( $this->encodeKey( $key ), $time );
92 }
93
94 /**
95 * @param $key
96 * @param $timeout int
97 * @return
98 */
99 public function lock( $key, $timeout = 0 ) {
100 return $this->client->lock( $this->encodeKey( $key ), $timeout );
101 }
102
103 /**
104 * @param $key string
105 * @return Mixed
106 */
107 public function unlock( $key ) {
108 return $this->client->unlock( $this->encodeKey( $key ) );
109 }
110
111 /**
112 * @param $key string
113 * @param $value int
114 * @return Mixed
115 */
116 public function add( $key, $value, $exptime = 0 ) {
117 return $this->client->add( $this->encodeKey( $key ), $value, $exptime );
118 }
119
120 /**
121 * @param $key string
122 * @param $value int
123 * @param $exptime
124 * @return Mixed
125 */
126 public function replace( $key, $value, $exptime = 0 ) {
127 return $this->client->replace( $this->encodeKey( $key ), $value, $exptime );
128 }
129
130 /**
131 * @param $key string
132 * @param $value int
133 * @return Mixed
134 */
135 public function incr( $key, $value = 1 ) {
136 return $this->client->incr( $this->encodeKey( $key ), $value );
137 }
138
139 /**
140 * @param $key string
141 * @param $value int
142 * @return Mixed
143 */
144 public function decr( $key, $value = 1 ) {
145 return $this->client->decr( $this->encodeKey( $key ), $value );
146 }
147
148 /**
149 * Get the underlying client object. This is provided for debugging
150 * purposes.
151 *
152 * @return MemCachedClientforWiki
153 */
154 public function getClient() {
155 return $this->client;
156 }
157
158 /**
159 * Encode a key for use on the wire inside the memcached protocol.
160 *
161 * We encode spaces and line breaks to avoid protocol errors. We encode
162 * the other control characters for compatibility with libmemcached
163 * verify_key. We leave other punctuation alone, to maximise backwards
164 * compatibility.
165 * @return string
166 */
167 public function encodeKey( $key ) {
168 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
169 array( $this, 'encodeKeyCallback' ), $key );
170 }
171
172 protected function encodeKeyCallback( $m ) {
173 return rawurlencode( $m[0] );
174 }
175
176 /**
177 * Decode a key encoded with encodeKey(). This is provided as a convenience
178 * function for debugging.
179 *
180 * @param $key string
181 *
182 * @return string
183 */
184 public function decodeKey( $key ) {
185 return urldecode( $key );
186 }
187 }
188