* Remove manual query building in search mysql
[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 function __construct( $params ) {
25 if ( !isset( $params['servers'] ) ) {
26 $params['servers'] = $GLOBALS['wgMemCachedServers'];
27 }
28 if ( !isset( $params['debug'] ) ) {
29 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
30 }
31 if ( !isset( $params['persistent'] ) ) {
32 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
33 }
34 if ( !isset( $params['compress_threshold'] ) ) {
35 $params['compress_threshold'] = 1500;
36 }
37 if ( !isset( $params['timeout'] ) ) {
38 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
39 }
40 if ( !isset( $params['connect_timeout'] ) ) {
41 $params['connect_timeout'] = 0.1;
42 }
43
44 $this->client = new MemCachedClientforWiki( $params );
45 $this->client->set_servers( $params['servers'] );
46 $this->client->set_debug( $params['debug'] );
47 }
48
49 public function setDebug( $debug ) {
50 $this->client->set_debug( $debug );
51 }
52
53 public function get( $key ) {
54 return $this->client->get( $this->encodeKey( $key ) );
55 }
56
57 public function set( $key, $value, $exptime = 0 ) {
58 return $this->client->set( $this->encodeKey( $key ), $value, $exptime );
59 }
60
61 public function delete( $key, $time = 0 ) {
62 return $this->client->delete( $this->encodeKey( $key ), $time );
63 }
64
65 public function lock( $key, $timeout = 0 ) {
66 return $this->client->lock( $this->encodeKey( $key ), $timeout );
67 }
68
69 public function unlock( $key ) {
70 return $this->client->unlock( $this->encodeKey( $key ) );
71 }
72
73 public function add( $key, $value, $exptime = 0 ) {
74 return $this->client->add( $this->encodeKey( $key ), $value, $exptime );
75 }
76
77 public function replace( $key, $value, $exptime = 0 ) {
78 return $this->client->replace( $this->encodeKey( $key ), $value, $exptime );
79 }
80
81 public function incr( $key, $value = 1 ) {
82 return $this->client->incr( $this->encodeKey( $key ), $value );
83 }
84
85 public function decr( $key, $value = 1 ) {
86 return $this->client->decr( $this->encodeKey( $key ), $value );
87 }
88
89 /**
90 * Get the underlying client object. This is provided for debugging
91 * purposes.
92 */
93 public function getClient() {
94 return $this->client;
95 }
96
97 /**
98 * Encode a key for use on the wire inside the memcached protocol.
99 *
100 * We encode spaces and line breaks to avoid protocol errors. We encode
101 * the other control characters for compatibility with libmemcached
102 * verify_key. We leave other punctuation alone, to maximise backwards
103 * compatibility.
104 */
105 public function encodeKey( $key ) {
106 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
107 array( $this, 'encodeKeyCallback' ), $key );
108 }
109
110 protected function encodeKeyCallback( $m ) {
111 return rawurlencode( $m[0] );
112 }
113
114 /**
115 * Decode a key encoded with encodeKey(). This is provided as a convenience
116 * function for debugging.
117 */
118 public function decodeKey( $key ) {
119 return urldecode( $key );
120 }
121 }
122