048953140ce99d823ad899ca221612c187776d4b
[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 /** @var MWMemcached|Memcached */
31 protected $client;
32
33 /**
34 * Fill in the defaults for any parameters missing from $params, using the
35 * backwards-compatible global variables
36 * @param array $params
37 * @return array
38 */
39 protected function applyDefaultParams( $params ) {
40 if ( !isset( $params['servers'] ) ) {
41 $params['servers'] = $GLOBALS['wgMemCachedServers'];
42 }
43 if ( !isset( $params['debug'] ) ) {
44 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
45 }
46 if ( !isset( $params['persistent'] ) ) {
47 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
48 }
49 if ( !isset( $params['compress_threshold'] ) ) {
50 $params['compress_threshold'] = 1500;
51 }
52 if ( !isset( $params['timeout'] ) ) {
53 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
54 }
55 if ( !isset( $params['connect_timeout'] ) ) {
56 $params['connect_timeout'] = 0.5;
57 }
58 return $params;
59 }
60
61 protected function doGet( $key, $flags = 0 ) {
62 $casToken = null;
63
64 return $this->getWithToken( $key, $casToken, $flags );
65 }
66
67 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
68 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
69 }
70
71 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
72 return $this->client->set( $this->validateKeyEncoding( $key ), $value,
73 $this->fixExpiry( $exptime ) );
74 }
75
76 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
77 return $this->client->cas( $casToken, $this->validateKeyEncoding( $key ),
78 $value, $this->fixExpiry( $exptime ) );
79 }
80
81 public function delete( $key ) {
82 return $this->client->delete( $this->validateKeyEncoding( $key ) );
83 }
84
85 public function add( $key, $value, $exptime = 0 ) {
86 return $this->client->add( $this->validateKeyEncoding( $key ), $value,
87 $this->fixExpiry( $exptime ) );
88 }
89
90 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
91 if ( !is_callable( $callback ) ) {
92 throw new Exception( "Got invalid callback." );
93 }
94
95 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
96 }
97
98 /**
99 * Get the underlying client object. This is provided for debugging
100 * purposes.
101 * @return BagOStuff
102 */
103 public function getClient() {
104 return $this->client;
105 }
106
107 /**
108 * Construct a cache key.
109 *
110 * @since 1.27
111 * @param string $keyspace
112 * @param array $args
113 * @return string
114 */
115 public function makeKeyInternal( $keyspace, $args ) {
116 // Memcached keys have a maximum length of 255 characters. From that,
117 // subtract the number of characters we need for the keyspace and for
118 // the separator character needed for each argument.
119 $charsLeft = 255 - strlen( $keyspace ) - count( $args );
120
121 $that = $this;
122 $args = array_map(
123 function ( $arg ) use ( $that, &$charsLeft ) {
124 // Make sure %, #, and non-ASCII chars are escaped
125 $arg = preg_replace_callback(
126 '/[^\x21-\x22\x24\x26-\x7e]+/',
127 function ( $m ) {
128 return rawurlencode( $m[0] );
129 },
130 $arg
131 );
132
133 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
134 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
135 $arg = '#' . md5( $arg );
136 }
137
138 $charsLeft -= strlen( $arg );
139 return $arg;
140 },
141 $args
142 );
143
144 if ( $charsLeft < 0 ) {
145 $args = array( '##' . md5( implode( ':', $args ) ) );
146 }
147
148 return parent::makeKeyInternal( $keyspace, $args );
149 }
150
151 /**
152 * Ensure that a key is safe to use (contains no control characters and no
153 * characters above the ASCII range.)
154 *
155 * @param string $key
156 * @return string
157 * @throws Exception
158 */
159 public function validateKeyEncoding( $key ) {
160 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
161 throw new Exception( "Key contains invalid characters: $key" );
162 }
163 return $key;
164 }
165
166 /**
167 * TTLs higher than 30 days will be detected as absolute TTLs
168 * (UNIX timestamps), and will result in the cache entry being
169 * discarded immediately because the expiry is in the past.
170 * Clamp expires >30d at 30d, unless they're >=1e9 in which
171 * case they are likely to really be absolute (1e9 = 2011-09-09)
172 * @param int $expiry
173 * @return int
174 */
175 function fixExpiry( $expiry ) {
176 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
177 $expiry = 2592000;
178 }
179 return (int)$expiry;
180 }
181
182 /**
183 * Send a debug message to the log
184 * @param string $text
185 */
186 protected function debugLog( $text ) {
187 $this->logger->debug( $text );
188 }
189
190 public function modifySimpleRelayEvent( array $event ) {
191 if ( array_key_exists( 'val', $event ) ) {
192 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
193 }
194
195 return $event;
196 }
197 }