* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convo...
[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 * Constructor.
9 *
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
17 */
18 function __construct( $params ) {
19 if ( !isset( $params['servers'] ) ) {
20 $params['servers'] = $GLOBALS['wgMemCachedServers'];
21 }
22 if ( !isset( $params['debug'] ) ) {
23 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
24 }
25 if ( !isset( $params['persistent'] ) ) {
26 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
27 }
28 if ( !isset( $params['compress_threshold'] ) ) {
29 $params['compress_threshold'] = 1500;
30 }
31 if ( !isset( $params['timeout'] ) ) {
32 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
33 }
34 if ( !isset( $params['connect_timeout'] ) ) {
35 $params['connect_timeout'] = 0.1;
36 }
37
38 $this->client = new MemCachedClientforWiki( $params );
39 $this->client->set_servers( $params['servers'] );
40 $this->client->set_debug( $params['debug'] );
41 }
42
43 public function setDebug( $debug ) {
44 $this->client->set_debug( $debug );
45 }
46
47 public function get( $key ) {
48 return $this->client->get( $this->encodeKey( $key ) );
49 }
50
51 public function set( $key, $value, $exptime = 0 ) {
52 return $this->client->set( $this->encodeKey( $key ), $value, $exptime );
53 }
54
55 public function delete( $key, $time = 0 ) {
56 return $this->client->delete( $this->encodeKey( $key ), $time );
57 }
58
59 public function lock( $key, $timeout = 0 ) {
60 return $this->client->lock( $this->encodeKey( $key ), $timeout );
61 }
62
63 public function unlock( $key ) {
64 return $this->client->unlock( $this->encodeKey( $key ) );
65 }
66
67 public function add( $key, $value, $exptime = 0 ) {
68 return $this->client->add( $this->encodeKey( $key ), $value, $exptime );
69 }
70
71 public function replace( $key, $value, $exptime = 0 ) {
72 return $this->client->replace( $this->encodeKey( $key ), $value, $exptime );
73 }
74
75 public function incr( $key, $value = 1 ) {
76 return $this->client->incr( $this->encodeKey( $key ), $value );
77 }
78
79 public function decr( $key, $value = 1 ) {
80 return $this->client->decr( $this->encodeKey( $key ), $value );
81 }
82
83 /**
84 * Get the underlying client object. This is provided for debugging
85 * purposes.
86 */
87 public function getClient() {
88 return $this->client;
89 }
90
91 /**
92 * Encode a key for use on the wire inside the memcached protocol.
93 *
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
97 * compatibility.
98 */
99 public function encodeKey( $key ) {
100 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
101 array( $this, 'encodeKeyCallback' ), $key );
102 }
103
104 protected function encodeKeyCallback( $m ) {
105 return urlencode( $m[0] );
106 }
107
108 /**
109 * Decode a key encoded with encodeKey(). This is provided as a convenience
110 * function for debugging.
111 */
112 public function decodeKey( $key ) {
113 return urldecode( $key );
114 }
115 }
116