Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[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 */
36 protected function applyDefaultParams( $params ) {
37 if ( !isset( $params['servers'] ) ) {
38 $params['servers'] = $GLOBALS['wgMemCachedServers'];
39 }
40 if ( !isset( $params['debug'] ) ) {
41 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
42 }
43 if ( !isset( $params['persistent'] ) ) {
44 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
45 }
46 if ( !isset( $params['compress_threshold'] ) ) {
47 $params['compress_threshold'] = 1500;
48 }
49 if ( !isset( $params['timeout'] ) ) {
50 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
51 }
52 if ( !isset( $params['connect_timeout'] ) ) {
53 $params['connect_timeout'] = 0.5;
54 }
55 return $params;
56 }
57
58 /**
59 * @param $key string
60 * @param $casToken[optional] mixed
61 * @return Mixed
62 */
63 public function get( $key, &$casToken = null ) {
64 return $this->client->get( $this->encodeKey( $key ), $casToken );
65 }
66
67 /**
68 * @param $key string
69 * @param $value
70 * @param $exptime int
71 * @return bool
72 */
73 public function set( $key, $value, $exptime = 0 ) {
74 return $this->client->set( $this->encodeKey( $key ), $value,
75 $this->fixExpiry( $exptime ) );
76 }
77
78 /**
79 * @param $key string
80 * @param $casToken mixed
81 * @param $value
82 * @param $exptime int
83 * @return bool
84 */
85 public function cas( $casToken, $key, $value, $exptime = 0 ) {
86 return $this->client->cas( $casToken, $this->encodeKey( $key ),
87 $value, $this->fixExpiry( $exptime ) );
88 }
89
90 /**
91 * @param $key string
92 * @param $time int
93 * @return bool
94 */
95 public function delete( $key, $time = 0 ) {
96 return $this->client->delete( $this->encodeKey( $key ), $time );
97 }
98
99 /**
100 * @param $key string
101 * @param $value int
102 * @param int $exptime (default 0)
103 * @return Mixed
104 */
105 public function add( $key, $value, $exptime = 0 ) {
106 return $this->client->add( $this->encodeKey( $key ), $value,
107 $this->fixExpiry( $exptime ) );
108 }
109
110 /**
111 * @param $key string
112 * @param $value int
113 * @param $exptime
114 * @return Mixed
115 */
116 public function replace( $key, $value, $exptime = 0 ) {
117 return $this->client->replace( $this->encodeKey( $key ), $value,
118 $this->fixExpiry( $exptime ) );
119 }
120
121 /**
122 * Get the underlying client object. This is provided for debugging
123 * purposes.
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 $key string
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 $m array
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 expiries >30d at 30d, unless they're >=1e9 in which
157 * case they are likely to really be absolute (1e9 = 2011-09-09)
158 */
159 function fixExpiry( $expiry ) {
160 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
161 $expiry = 2592000;
162 }
163 return $expiry;
164 }
165
166 /**
167 * Decode a key encoded with encodeKey(). This is provided as a convenience
168 * function for debugging.
169 *
170 * @param $key string
171 *
172 * @return string
173 */
174 public function decodeKey( $key ) {
175 return urldecode( $key );
176 }
177
178 /**
179 * Send a debug message to the log
180 */
181 protected function debugLog( $text ) {
182 if ( substr( $text, -1 ) !== "\n" ) {
183 $text .= "\n";
184 }
185 wfDebugLog( 'memcached', $text );
186 }
187 }