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