Use protocol-relative URL for link to MediaWiki.org (as with the footer logo on each...
[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 $key string
68 * @param $value
69 * @param $exptime int
70 * @return bool
71 */
72 public function set( $key, $value, $exptime = 0 ) {
73 return $this->client->set( $this->encodeKey( $key ), $value, $exptime );
74 }
75
76 /**
77 * @param $key string
78 * @param $time int
79 * @return bool
80 */
81 public function delete( $key, $time = 0 ) {
82 return $this->client->delete( $this->encodeKey( $key ), $time );
83 }
84
85 /**
86 * @param $key
87 * @param $timeout int
88 * @return
89 */
90 public function lock( $key, $timeout = 0 ) {
91 return $this->client->lock( $this->encodeKey( $key ), $timeout );
92 }
93
94 /**
95 * @param $key string
96 * @return Mixed
97 */
98 public function unlock( $key ) {
99 return $this->client->unlock( $this->encodeKey( $key ) );
100 }
101
102 /**
103 * @param $key string
104 * @param $value int
105 * @return Mixed
106 */
107 public function add( $key, $value, $exptime = 0 ) {
108 return $this->client->add( $this->encodeKey( $key ), $value, $exptime );
109 }
110
111 /**
112 * @param $key string
113 * @param $value int
114 * @param $exptime
115 * @return Mixed
116 */
117 public function replace( $key, $value, $exptime = 0 ) {
118 return $this->client->replace( $this->encodeKey( $key ), $value, $exptime );
119 }
120
121 /**
122 * @param $key string
123 * @param $value int
124 * @return Mixed
125 */
126 public function incr( $key, $value = 1 ) {
127 return $this->client->incr( $this->encodeKey( $key ), $value );
128 }
129
130 /**
131 * @param $key string
132 * @param $value int
133 * @return Mixed
134 */
135 public function decr( $key, $value = 1 ) {
136 return $this->client->decr( $this->encodeKey( $key ), $value );
137 }
138
139 /**
140 * Get the underlying client object. This is provided for debugging
141 * purposes.
142 *
143 * @return MemCachedClientforWiki
144 */
145 public function getClient() {
146 return $this->client;
147 }
148
149 /**
150 * Encode a key for use on the wire inside the memcached protocol.
151 *
152 * We encode spaces and line breaks to avoid protocol errors. We encode
153 * the other control characters for compatibility with libmemcached
154 * verify_key. We leave other punctuation alone, to maximise backwards
155 * compatibility.
156 */
157 public function encodeKey( $key ) {
158 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
159 array( $this, 'encodeKeyCallback' ), $key );
160 }
161
162 protected function encodeKeyCallback( $m ) {
163 return rawurlencode( $m[0] );
164 }
165
166 /**
167 * Decode a key encoded with encodeKey(). This is provided as a convenience
168 * function for debugging.
169 *
170 * @param $key string
171 *
172 * @return string
173 */
174 public function decodeKey( $key ) {
175 return urldecode( $key );
176 }
177 }
178