Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[lhc/web/wiklou.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 /** @var MemcachedClient */
31 protected $client;
32
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 *
41 * @param array $params
42 */
43 function __construct( $params ) {
44 parent::__construct( $params );
45
46 // Default class-specific parameters
47 $params += [
48 'compress_threshold' => 1500,
49 'connect_timeout' => 0.5
50 ];
51
52 $this->client = new MemcachedClient( $params );
53 $this->client->set_servers( $params['servers'] );
54 }
55
56 public function setDebug( $debug ) {
57 $this->client->set_debug( $debug );
58 }
59
60 protected function doGet( $key, $flags = 0, &$casToken = null ) {
61 $casToken = null;
62
63 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
64 }
65
66 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
67 return $this->client->set(
68 $this->validateKeyEncoding( $key ),
69 $value,
70 $this->fixExpiry( $exptime )
71 );
72 }
73
74 protected function doDelete( $key, $flags = 0 ) {
75 return $this->client->delete( $this->validateKeyEncoding( $key ) );
76 }
77
78 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
79 return $this->client->add(
80 $this->validateKeyEncoding( $key ),
81 $value,
82 $this->fixExpiry( $exptime )
83 );
84 }
85
86 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
87 return $this->client->cas(
88 $casToken,
89 $this->validateKeyEncoding( $key ),
90 $value,
91 $this->fixExpiry( $exptime )
92 );
93 }
94
95 public function incr( $key, $value = 1 ) {
96 $n = $this->client->incr( $this->validateKeyEncoding( $key ), $value );
97
98 return ( $n !== false && $n !== null ) ? $n : false;
99 }
100
101 public function decr( $key, $value = 1 ) {
102 $n = $this->client->decr( $this->validateKeyEncoding( $key ), $value );
103
104 return ( $n !== false && $n !== null ) ? $n : false;
105 }
106
107 protected function doChangeTTL( $key, $exptime, $flags ) {
108 return $this->client->touch(
109 $this->validateKeyEncoding( $key ),
110 $this->fixExpiry( $exptime )
111 );
112 }
113
114 protected function doGetMulti( array $keys, $flags = 0 ) {
115 foreach ( $keys as $key ) {
116 $this->validateKeyEncoding( $key );
117 }
118
119 return $this->client->get_multi( $keys );
120 }
121
122 protected function serialize( $value ) {
123 return is_int( $value ) ? $value : $this->client->serialize( $value );
124 }
125
126 protected function unserialize( $value ) {
127 return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
128 }
129 }