412f0173025c54add35fddff76ab50cc04466344
[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 protected function doGet( $key, $flags = 0 ) {
61 $casToken = null;
62
63 return $this->getWithToken( $key, $casToken, $flags );
64 }
65
66 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
67 return $this->client->get( $this->encodeKey( $key ), $casToken );
68 }
69
70 /**
71 * @param string $key
72 * @param mixed $value
73 * @param int $exptime
74 * @return bool
75 */
76 public function set( $key, $value, $exptime = 0 ) {
77 return $this->client->set( $this->encodeKey( $key ), $value,
78 $this->fixExpiry( $exptime ) );
79 }
80
81 /**
82 * @param mixed $casToken
83 * @param string $key
84 * @param mixed $value
85 * @param int $exptime
86 * @return bool
87 */
88 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
89 return $this->client->cas( $casToken, $this->encodeKey( $key ),
90 $value, $this->fixExpiry( $exptime ) );
91 }
92
93 /**
94 * @param string $key
95 * @return bool
96 */
97 public function delete( $key ) {
98 return $this->client->delete( $this->encodeKey( $key ) );
99 }
100
101 /**
102 * @param string $key
103 * @param int $value
104 * @param int $exptime (default 0)
105 * @return mixed
106 */
107 public function add( $key, $value, $exptime = 0 ) {
108 return $this->client->add( $this->encodeKey( $key ), $value,
109 $this->fixExpiry( $exptime ) );
110 }
111
112 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
113 if ( !is_callable( $callback ) ) {
114 throw new Exception( "Got invalid callback." );
115 }
116
117 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
118 }
119
120 /**
121 * Get the underlying client object. This is provided for debugging
122 * purposes.
123 * @return BagOStuff
124 */
125 public function getClient() {
126 return $this->client;
127 }
128
129 /**
130 * Encode a key for use on the wire inside the memcached protocol.
131 *
132 * We encode spaces and line breaks to avoid protocol errors. We encode
133 * the other control characters for compatibility with libmemcached
134 * verify_key. We leave other punctuation alone, to maximise backwards
135 * compatibility.
136 * @param string $key
137 * @return string
138 */
139 public function encodeKey( $key ) {
140 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
141 array( $this, 'encodeKeyCallback' ), $key );
142 }
143
144 /**
145 * @param array $m
146 * @return string
147 */
148 protected function encodeKeyCallback( $m ) {
149 return rawurlencode( $m[0] );
150 }
151
152 /**
153 * TTLs higher than 30 days will be detected as absolute TTLs
154 * (UNIX timestamps), and will result in the cache entry being
155 * discarded immediately because the expiry is in the past.
156 * Clamp expires >30d at 30d, unless they're >=1e9 in which
157 * case they are likely to really be absolute (1e9 = 2011-09-09)
158 * @param int $expiry
159 * @return int
160 */
161 function fixExpiry( $expiry ) {
162 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
163 $expiry = 2592000;
164 }
165 return (int)$expiry;
166 }
167
168 /**
169 * Decode a key encoded with encodeKey(). This is provided as a convenience
170 * function for debugging.
171 *
172 * @param string $key
173 *
174 * @return string
175 */
176 public function decodeKey( $key ) {
177 // matches %00-%20, %25, %7F (=decoded alternatives for those encoded in encodeKey)
178 return preg_replace_callback( '/%([0-1][0-9]|20|25|7F)/i', function ( $match ) {
179 return urldecode( $match[0] );
180 }, $key );
181 }
182
183 /**
184 * Send a debug message to the log
185 * @param string $text
186 */
187 protected function debugLog( $text ) {
188 $this->logger->debug( $text );
189 }
190
191 public function modifySimpleRelayEvent( array $event ) {
192 if ( array_key_exists( 'val', $event ) ) {
193 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
194 }
195
196 return $event;
197 }
198 }