Merge "Add 'mw-anonuserlink' class for anonymous users"
[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 * Get the underlying client object. This is provided for debugging
112 * purposes.
113 */
114 public function getClient() {
115 return $this->client;
116 }
117
118 /**
119 * Encode a key for use on the wire inside the memcached protocol.
120 *
121 * We encode spaces and line breaks to avoid protocol errors. We encode
122 * the other control characters for compatibility with libmemcached
123 * verify_key. We leave other punctuation alone, to maximise backwards
124 * compatibility.
125 * @param $key string
126 * @return string
127 */
128 public function encodeKey( $key ) {
129 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
130 array( $this, 'encodeKeyCallback' ), $key );
131 }
132
133 /**
134 * @param $m array
135 * @return string
136 */
137 protected function encodeKeyCallback( $m ) {
138 return rawurlencode( $m[0] );
139 }
140
141 /**
142 * TTLs higher than 30 days will be detected as absolute TTLs
143 * (UNIX timestamps), and will result in the cache entry being
144 * discarded immediately because the expiry is in the past.
145 * Clamp expiries >30d at 30d, unless they're >=1e9 in which
146 * case they are likely to really be absolute (1e9 = 2011-09-09)
147 */
148 function fixExpiry( $expiry ) {
149 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
150 $expiry = 2592000;
151 }
152 return $expiry;
153 }
154
155 /**
156 * Decode a key encoded with encodeKey(). This is provided as a convenience
157 * function for debugging.
158 *
159 * @param $key string
160 *
161 * @return string
162 */
163 public function decodeKey( $key ) {
164 return urldecode( $key );
165 }
166
167 /**
168 * Send a debug message to the log
169 */
170 protected function debugLog( $text ) {
171 wfDebugLog( 'memcached', $text );
172 }
173 }