Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / objectcache / MemcachedBagOStuff.php
1 <?php
2 /**
3 * Base class for memcached clients.
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 * Base class for memcached clients.
26 *
27 * @ingroup Cache
28 */
29 class MemcachedBagOStuff extends BagOStuff {
30 protected $client;
31
32 /**
33 * Fill in the defaults for any parameters missing from $params, using the
34 * backwards-compatible global variables
35 * @param array $params
36 * @return array
37 */
38 protected function applyDefaultParams( $params ) {
39 if ( !isset( $params['servers'] ) ) {
40 $params['servers'] = $GLOBALS['wgMemCachedServers'];
41 }
42 if ( !isset( $params['debug'] ) ) {
43 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
44 }
45 if ( !isset( $params['persistent'] ) ) {
46 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
47 }
48 if ( !isset( $params['compress_threshold'] ) ) {
49 $params['compress_threshold'] = 1500;
50 }
51 if ( !isset( $params['timeout'] ) ) {
52 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
53 }
54 if ( !isset( $params['connect_timeout'] ) ) {
55 $params['connect_timeout'] = 0.5;
56 }
57 return $params;
58 }
59
60 /**
61 * @param string $key
62 * @param mixed $casToken [optional]
63 * @return mixed
64 */
65 public function get( $key, &$casToken = null ) {
66 return $this->client->get( $this->encodeKey( $key ), $casToken );
67 }
68
69 /**
70 * @param string $key
71 * @param mixed $value
72 * @param int $exptime
73 * @return bool
74 */
75 public function set( $key, $value, $exptime = 0 ) {
76 return $this->client->set( $this->encodeKey( $key ), $value,
77 $this->fixExpiry( $exptime ) );
78 }
79
80 /**
81 * @param mixed $casToken
82 * @param string $key
83 * @param mixed $value
84 * @param int $exptime
85 * @return bool
86 */
87 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
88 return $this->client->cas( $casToken, $this->encodeKey( $key ),
89 $value, $this->fixExpiry( $exptime ) );
90 }
91
92 /**
93 * @param string $key
94 * @return bool
95 */
96 public function delete( $key ) {
97 return $this->client->delete( $this->encodeKey( $key ) );
98 }
99
100 /**
101 * @param string $key
102 * @param int $value
103 * @param int $exptime (default 0)
104 * @return mixed
105 */
106 public function add( $key, $value, $exptime = 0 ) {
107 return $this->client->add( $this->encodeKey( $key ), $value,
108 $this->fixExpiry( $exptime ) );
109 }
110
111 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
112 if ( !is_callable( $callback ) ) {
113 throw new Exception( "Got invalid callback." );
114 }
115
116 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
117 }
118
119 /**
120 * Get the underlying client object. This is provided for debugging
121 * purposes.
122 * @return BagOStuff
123 */
124 public function getClient() {
125 return $this->client;
126 }
127
128 /**
129 * Encode a key for use on the wire inside the memcached protocol.
130 *
131 * We encode spaces and line breaks to avoid protocol errors. We encode
132 * the other control characters for compatibility with libmemcached
133 * verify_key. We leave other punctuation alone, to maximise backwards
134 * compatibility.
135 * @param string $key
136 * @return string
137 */
138 public function encodeKey( $key ) {
139 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
140 array( $this, 'encodeKeyCallback' ), $key );
141 }
142
143 /**
144 * @param array $m
145 * @return string
146 */
147 protected function encodeKeyCallback( $m ) {
148 return rawurlencode( $m[0] );
149 }
150
151 /**
152 * TTLs higher than 30 days will be detected as absolute TTLs
153 * (UNIX timestamps), and will result in the cache entry being
154 * discarded immediately because the expiry is in the past.
155 * Clamp expires >30d at 30d, unless they're >=1e9 in which
156 * case they are likely to really be absolute (1e9 = 2011-09-09)
157 * @param int $expiry
158 * @return int
159 */
160 function fixExpiry( $expiry ) {
161 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
162 $expiry = 2592000;
163 }
164 return (int)$expiry;
165 }
166
167 /**
168 * Decode a key encoded with encodeKey(). This is provided as a convenience
169 * function for debugging.
170 *
171 * @param string $key
172 *
173 * @return string
174 */
175 public function decodeKey( $key ) {
176 return urldecode( $key );
177 }
178
179 /**
180 * Send a debug message to the log
181 * @param string $text
182 */
183 protected function debugLog( $text ) {
184 $this->logger->debug( $text );
185 }
186
187 public function modifySimpleRelayEvent( array $event ) {
188 if ( array_key_exists( 'val', $event ) ) {
189 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
190 }
191
192 return $event;
193 }
194 }