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