9c43ede8949f5317e72e11e88ad18f3f585097c6
[lhc/web/wiklou.git] / includes / objectcache / MemcachedPeclBagOStuff.php
1 <?php
2
3 /**
4 * A wrapper class for the PECL memcached client
5 *
6 * @ingroup Cache
7 */
8 class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
9
10 /**
11 * Constructor
12 *
13 * Available parameters are:
14 * - servers: The list of IP:port combinations holding the memcached servers.
15 * - persistent: Whether to use a persistent connection
16 * - compress_threshold: The minimum size an object must be before it is compressed
17 * - timeout: The read timeout in microseconds
18 * - connect_timeout: The connect timeout in seconds
19 * - serializer: May be either "php" or "igbinary". Igbinary produces more compact
20 * values, but serialization is much slower unless the php.ini option
21 * igbinary.compact_strings is off.
22 */
23 function __construct( $params ) {
24 $params = $this->applyDefaultParams( $params );
25
26 if ( $params['persistent'] ) {
27 $this->client = new Memcached( __CLASS__ );
28 } else {
29 $this->client = new Memcached;
30 }
31
32 if ( !isset( $params['serializer'] ) ) {
33 $params['serializer'] = 'php';
34 }
35
36 // The compression threshold is an undocumented php.ini option for some
37 // reason. There's probably not much harm in setting it globally, for
38 // compatibility with the settings for the PHP client.
39 ini_set( 'memcached.compression_threshold', $params['compress_threshold'] );
40
41 // Set timeouts
42 $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 );
43 $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] );
44 $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] );
45 $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 );
46
47 // Set libketama mode since it's recommended by the documentation and
48 // is as good as any. There's no way to configure libmemcached to use
49 // hashes identical to the ones currently in use by the PHP client, and
50 // even implementing one of the libmemcached hashes in pure PHP for
51 // forwards compatibility would require MWMemcached::get_sock() to be
52 // rewritten.
53 $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true );
54
55 // Set the serializer
56 switch ( $params['serializer'] ) {
57 case 'php':
58 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP );
59 break;
60 case 'igbinary':
61 if ( !extension_loaded( 'igbinary' ) ) {
62 throw new MWException( __CLASS__.': the igbinary extension is not loaded ' .
63 'but igbinary serialization was requested.' );
64 }
65 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY );
66 break;
67 default:
68 throw new MWException( __CLASS__.': invalid value for serializer parameter' );
69 }
70 foreach ( $params['servers'] as $host ) {
71 list( $ip, $port ) = IP::splitHostAndPort( $host );
72 $this->client->addServer( $ip, $port );
73 }
74 }
75
76 /**
77 * @param $key string
78 * @return Mixed
79 */
80 public function get( $key ) {
81 $this->debugLog( "get($key)" );
82 return $this->checkResult( $key, parent::get( $key ) );
83 }
84
85 /**
86 * @param $key string
87 * @param $value
88 * @param $exptime int
89 * @return bool
90 */
91 public function set( $key, $value, $exptime = 0 ) {
92 $this->debugLog( "set($key)" );
93 return $this->checkResult( $key, parent::set( $key, $value, $exptime ) );
94 }
95
96 /**
97 * @param $key string
98 * @param $time int
99 * @return bool
100 */
101 public function delete( $key, $time = 0 ) {
102 $this->debugLog( "delete($key)" );
103 return $this->checkResult( $key, parent::delete( $key, $time ) );
104 }
105
106 /**
107 * @param $key string
108 * @param $value int
109 * @return Mixed
110 */
111 public function add( $key, $value, $exptime = 0 ) {
112 $this->debugLog( "add($key)" );
113 return $this->checkResult( $key, parent::add( $key, $value, $exptime ) );
114 }
115
116 /**
117 * @param $key string
118 * @param $value int
119 * @param $exptime
120 * @return Mixed
121 */
122 public function replace( $key, $value, $exptime = 0 ) {
123 $this->debugLog( "replace($key)" );
124 return $this->checkResult( $key, parent::replace( $key, $value, $exptime ) );
125 }
126
127 /**
128 * @param $key string
129 * @param $value int
130 * @return Mixed
131 */
132 public function incr( $key, $value = 1 ) {
133 $this->debugLog( "incr($key)" );
134 $result = $this->client->increment( $key, $value );
135 return $this->checkResult( $key, $result );
136 }
137
138 /**
139 * @param $key string
140 * @param $value int
141 * @return Mixed
142 */
143 public function decr( $key, $value = 1 ) {
144 $this->debugLog( "decr($key)" );
145 $result = $this->client->decrement( $key, $value );
146 return $this->checkResult( $key, $result );
147 }
148
149 /**
150 * Check the return value from a client method call and take any necessary
151 * action. Returns the value that the wrapper function should return. At
152 * present, the return value is always the same as the return value from
153 * the client, but some day we might find a case where it should be
154 * different.
155 *
156 * @param $key The key used by the caller, or false if there wasn't one.
157 * @param $result The return value
158 */
159 protected function checkResult( $key, $result ) {
160 if ( $result !== false ) {
161 return $result;
162 }
163 switch ( $this->client->getResultCode() ) {
164 case Memcached::RES_SUCCESS:
165 break;
166 case Memcached::RES_DATA_EXISTS:
167 case Memcached::RES_NOTSTORED:
168 case Memcached::RES_NOTFOUND:
169 $this->debugLog( "result: " . $this->client->getResultMessage() );
170 break;
171 default:
172 $msg = $this->client->getResultMessage();
173 if ( $key !== false ) {
174 $server = $this->client->getServerByKey( $key );
175 $serverName = "{$server['host']}:{$server['port']}";
176 $msg = "Memcached error for key \"$key\" on server \"$serverName\": $msg";
177 } else {
178 $msg = "Memcached error: $msg";
179 }
180 wfDebugLog( 'memcached-serious', $msg );
181 }
182 return $result;
183 }
184
185 /**
186 * @param $keys Array
187 * @return Array
188 */
189 public function getBatch( array $keys ) {
190 $this->debugLog( 'getBatch(' . implode( ', ', $keys ) . ')' );
191 $callback = array( $this, 'encodeKey' );
192 $result = $this->client->getMulti( array_map( $callback, $keys ) );
193 return $this->checkResult( false, $result );
194 }
195
196 /* NOTE: there is no cas() method here because it is currently not supported
197 * by the BagOStuff interface and other BagOStuff subclasses, such as
198 * SqlBagOStuff.
199 */
200 }