Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 * - debug: Whether to set the debug flag in the underlying client.
37 * - persistent: Whether to use a persistent connection
38 * - compress_threshold: The minimum size an object must be before it is compressed
39 * - timeout: The read timeout in microseconds
40 * - connect_timeout: The connect timeout in seconds
41 *
42 * @param array $params
43 */
44 function __construct( $params ) {
45 parent::__construct( $params );
46 $params = $this->applyDefaultParams( $params );
47
48 $this->client = new MemcachedClient( $params );
49 $this->client->set_servers( $params['servers'] );
50 $this->client->set_debug( $params['debug'] );
51 }
52
53 public function setDebug( $debug ) {
54 $this->client->set_debug( $debug );
55 }
56
57 protected function doGet( $key, $flags = 0, &$casToken = null ) {
58 $casToken = null;
59
60 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
61 }
62
63 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
64 return $this->client->set(
65 $this->validateKeyEncoding( $key ),
66 $value,
67 $this->fixExpiry( $exptime )
68 );
69 }
70
71 protected function doDelete( $key, $flags = 0 ) {
72 return $this->client->delete( $this->validateKeyEncoding( $key ) );
73 }
74
75 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
76 return $this->client->add(
77 $this->validateKeyEncoding( $key ),
78 $value,
79 $this->fixExpiry( $exptime )
80 );
81 }
82
83 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
84 return $this->client->cas(
85 $casToken,
86 $this->validateKeyEncoding( $key ),
87 $value,
88 $this->fixExpiry( $exptime )
89 );
90 }
91
92 public function incr( $key, $value = 1 ) {
93 $n = $this->client->incr( $this->validateKeyEncoding( $key ), $value );
94
95 return ( $n !== false && $n !== null ) ? $n : false;
96 }
97
98 public function decr( $key, $value = 1 ) {
99 $n = $this->client->decr( $this->validateKeyEncoding( $key ), $value );
100
101 return ( $n !== false && $n !== null ) ? $n : false;
102 }
103
104 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
105 return $this->client->touch(
106 $this->validateKeyEncoding( $key ),
107 $this->fixExpiry( $exptime )
108 );
109 }
110
111 public function doGetMulti( array $keys, $flags = 0 ) {
112 foreach ( $keys as $key ) {
113 $this->validateKeyEncoding( $key );
114 }
115
116 return $this->client->get_multi( $keys );
117 }
118
119 protected function serialize( $value ) {
120 return is_int( $value ) ? $value : $this->client->serialize( $value );
121 }
122
123 protected function unserialize( $value ) {
124 return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
125 }
126 }