4 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
6 class MemcachedPhpBagOStuff
extends BagOStuff
{
9 * @var MemCachedClientforWiki
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
24 * @params $params array
26 function __construct( $params ) {
27 if ( !isset( $params['servers'] ) ) {
28 $params['servers'] = $GLOBALS['wgMemCachedServers'];
30 if ( !isset( $params['debug'] ) ) {
31 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
33 if ( !isset( $params['persistent'] ) ) {
34 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
36 if ( !isset( $params['compress_threshold'] ) ) {
37 $params['compress_threshold'] = 1500;
39 if ( !isset( $params['timeout'] ) ) {
40 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
42 if ( !isset( $params['connect_timeout'] ) ) {
43 $params['connect_timeout'] = 0.1;
46 $this->client
= new MemCachedClientforWiki( $params );
47 $this->client
->set_servers( $params['servers'] );
48 $this->client
->set_debug( $params['debug'] );
51 public function setDebug( $debug ) {
52 $this->client
->set_debug( $debug );
55 public function get( $key ) {
56 return $this->client
->get( $this->encodeKey( $key ) );
59 public function set( $key, $value, $exptime = 0 ) {
60 return $this->client
->set( $this->encodeKey( $key ), $value, $exptime );
63 public function delete( $key, $time = 0 ) {
64 return $this->client
->delete( $this->encodeKey( $key ), $time );
67 public function lock( $key, $timeout = 0 ) {
68 return $this->client
->lock( $this->encodeKey( $key ), $timeout );
71 public function unlock( $key ) {
72 return $this->client
->unlock( $this->encodeKey( $key ) );
75 public function add( $key, $value, $exptime = 0 ) {
76 return $this->client
->add( $this->encodeKey( $key ), $value, $exptime );
79 public function replace( $key, $value, $exptime = 0 ) {
80 return $this->client
->replace( $this->encodeKey( $key ), $value, $exptime );
83 public function incr( $key, $value = 1 ) {
84 return $this->client
->incr( $this->encodeKey( $key ), $value );
87 public function decr( $key, $value = 1 ) {
88 return $this->client
->decr( $this->encodeKey( $key ), $value );
92 * Get the underlying client object. This is provided for debugging
95 public function getClient() {
100 * Encode a key for use on the wire inside the memcached protocol.
102 * We encode spaces and line breaks to avoid protocol errors. We encode
103 * the other control characters for compatibility with libmemcached
104 * verify_key. We leave other punctuation alone, to maximise backwards
107 public function encodeKey( $key ) {
108 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
109 array( $this, 'encodeKeyCallback' ), $key );
112 protected function encodeKeyCallback( $m ) {
113 return rawurlencode( $m[0] );
117 * Decode a key encoded with encodeKey(). This is provided as a convenience
118 * function for debugging.
120 public function decodeKey( $key ) {
121 return urldecode( $key );