4 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
6 class MemcachedPhpBagOStuff
extends BagOStuff
{
10 * Available parameters are:
11 * - servers: The list of IP:port combinations holding the memcached servers.
12 * - debug: Whether to set the debug flag in the underlying client.
13 * - persistent: Whether to use a persistent connection
14 * - compress_threshold: The minimum size an object must be before it is compressed
15 * - timeout: The read timeout in microseconds
16 * - connect_timeout: The connect timeout in seconds
18 function __construct( $params ) {
19 if ( !isset( $params['servers'] ) ) {
20 $params['servers'] = $GLOBALS['wgMemCachedServers'];
22 if ( !isset( $params['debug'] ) ) {
23 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
25 if ( !isset( $params['persistent'] ) ) {
26 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
28 if ( !isset( $params['compress_threshold'] ) ) {
29 $params['compress_threshold'] = 1500;
31 if ( !isset( $params['timeout'] ) ) {
32 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
34 if ( !isset( $params['connect_timeout'] ) ) {
35 $params['connect_timeout'] = 0.1;
38 $this->client
= new MemCachedClientforWiki( $params );
39 $this->client
->set_servers( $params['servers'] );
40 $this->client
->set_debug( $params['debug'] );
43 public function setDebug( $debug ) {
44 $this->client
->set_debug( $debug );
47 public function get( $key ) {
48 return $this->client
->get( $this->encodeKey( $key ) );
51 public function set( $key, $value, $exptime = 0 ) {
52 return $this->client
->set( $this->encodeKey( $key ), $value, $exptime );
55 public function delete( $key, $time = 0 ) {
56 return $this->client
->delete( $this->encodeKey( $key ), $time );
59 public function lock( $key, $timeout = 0 ) {
60 return $this->client
->lock( $this->encodeKey( $key ), $timeout );
63 public function unlock( $key ) {
64 return $this->client
->unlock( $this->encodeKey( $key ) );
67 public function add( $key, $value, $exptime = 0 ) {
68 return $this->client
->add( $this->encodeKey( $key ), $value, $exptime );
71 public function replace( $key, $value, $exptime = 0 ) {
72 return $this->client
->replace( $this->encodeKey( $key ), $value, $exptime );
75 public function incr( $key, $value = 1 ) {
76 return $this->client
->incr( $this->encodeKey( $key ), $value );
79 public function decr( $key, $value = 1 ) {
80 return $this->client
->decr( $this->encodeKey( $key ), $value );
84 * Get the underlying client object. This is provided for debugging
87 public function getClient() {
92 * Encode a key for use on the wire inside the memcached protocol.
94 * We encode spaces and line breaks to avoid protocol errors. We encode
95 * the other control characters for compatibility with libmemcached
96 * verify_key. We leave other punctuation alone, to maximise backwards
99 public function encodeKey( $key ) {
100 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
101 array( $this, 'encodeKeyCallback' ), $key );
104 protected function encodeKeyCallback( $m ) {
105 return urlencode( $m[0] );
109 * Decode a key encoded with encodeKey(). This is provided as a convenience
110 * function for debugging.
112 public function decodeKey( $key ) {
113 return urldecode( $key );