]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/libs/objectcache/MemcachedPhpBagOStuff.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / libs / objectcache / MemcachedPhpBagOStuff.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 pure-PHP memcached client, exposing a BagOStuff interface.
26  *
27  * @ingroup Cache
28  */
29 class MemcachedPhpBagOStuff extends MemcachedBagOStuff {
30         /**
31          * Available parameters are:
32          *   - servers:             The list of IP:port combinations holding the memcached servers.
33          *   - debug:               Whether to set the debug flag in the underlying client.
34          *   - persistent:          Whether to use a persistent connection
35          *   - compress_threshold:  The minimum size an object must be before it is compressed
36          *   - timeout:             The read timeout in microseconds
37          *   - connect_timeout:     The connect timeout in seconds
38          *
39          * @param array $params
40          */
41         function __construct( $params ) {
42                 parent::__construct( $params );
43                 $params = $this->applyDefaultParams( $params );
44
45                 $this->client = new MemcachedClient( $params );
46                 $this->client->set_servers( $params['servers'] );
47                 $this->client->set_debug( $params['debug'] );
48         }
49
50         public function setDebug( $debug ) {
51                 $this->client->set_debug( $debug );
52         }
53
54         public function getMulti( array $keys, $flags = 0 ) {
55                 foreach ( $keys as $key ) {
56                         $this->validateKeyEncoding( $key );
57                 }
58
59                 return $this->client->get_multi( $keys );
60         }
61
62         public function incr( $key, $value = 1 ) {
63                 $this->validateKeyEncoding( $key );
64
65                 $ret = $this->client->incr( $key, $value );
66                 return $ret !== null ? $ret : false;
67         }
68
69         public function decr( $key, $value = 1 ) {
70                 $this->validateKeyEncoding( $key );
71
72                 $ret = $this->client->decr( $key, $value );
73                 return $ret !== null ? $ret : false;
74         }
75 }