3 * Object caching using memcached.
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.
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.
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
25 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
29 class MemcachedPhpBagOStuff
extends BagOStuff
{
32 * @var MemCachedClientforWiki
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
47 * @param $params array
49 function __construct( $params ) {
50 if ( !isset( $params['servers'] ) ) {
51 $params['servers'] = $GLOBALS['wgMemCachedServers'];
53 if ( !isset( $params['debug'] ) ) {
54 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
56 if ( !isset( $params['persistent'] ) ) {
57 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
59 if ( !isset( $params['compress_threshold'] ) ) {
60 $params['compress_threshold'] = 1500;
62 if ( !isset( $params['timeout'] ) ) {
63 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
65 if ( !isset( $params['connect_timeout'] ) ) {
66 $params['connect_timeout'] = 0.1;
69 $this->client
= new MemCachedClientforWiki( $params );
70 $this->client
->set_servers( $params['servers'] );
71 $this->client
->set_debug( $params['debug'] );
77 public function setDebug( $debug ) {
78 $this->client
->set_debug( $debug );
85 public function get( $key ) {
86 return $this->client
->get( $this->encodeKey( $key ) );
93 public function getBatch( array $keys ) {
94 $callback = array( $this, 'encodeKey' );
95 return $this->client
->get_multi( array_map( $callback, $keys ) );
101 * @param $exptime int
104 public function set( $key, $value, $exptime = 0 ) {
105 return $this->client
->set( $this->encodeKey( $key ), $value, $exptime );
113 public function delete( $key, $time = 0 ) {
114 return $this->client
->delete( $this->encodeKey( $key ), $time );
119 * @param $timeout int
122 public function lock( $key, $timeout = 0 ) {
123 return $this->client
->lock( $this->encodeKey( $key ), $timeout );
130 public function unlock( $key ) {
131 return $this->client
->unlock( $this->encodeKey( $key ) );
139 public function add( $key, $value, $exptime = 0 ) {
140 return $this->client
->add( $this->encodeKey( $key ), $value, $exptime );
149 public function replace( $key, $value, $exptime = 0 ) {
150 return $this->client
->replace( $this->encodeKey( $key ), $value, $exptime );
158 public function incr( $key, $value = 1 ) {
159 return $this->client
->incr( $this->encodeKey( $key ), $value );
167 public function decr( $key, $value = 1 ) {
168 return $this->client
->decr( $this->encodeKey( $key ), $value );
172 * Get the underlying client object. This is provided for debugging
175 * @return MemCachedClientforWiki
177 public function getClient() {
178 return $this->client
;
182 * Encode a key for use on the wire inside the memcached protocol.
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
190 public function encodeKey( $key ) {
191 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
192 array( $this, 'encodeKeyCallback' ), $key );
195 protected function encodeKeyCallback( $m ) {
196 return rawurlencode( $m[0] );
200 * Decode a key encoded with encodeKey(). This is provided as a convenience
201 * function for debugging.
207 public function decodeKey( $key ) {
208 return urldecode( $key );