Merge "(bug 36819) Lowercase Kazakh and Uzbek"
[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.1;
54 }
55 return $params;
56 }
57
58 /**
59 * @param $key string
60 * @return Mixed
61 */
62 public function get( $key ) {
63 return $this->client->get( $this->encodeKey( $key ) );
64 }
65
66 /**
67 * @param $key string
68 * @param $value
69 * @param $exptime int
70 * @return bool
71 */
72 public function set( $key, $value, $exptime = 0 ) {
73 return $this->client->set( $this->encodeKey( $key ), $value,
74 $this->fixExpiry( $exptime ) );
75 }
76
77 /**
78 * @param $key string
79 * @param $time int
80 * @return bool
81 */
82 public function delete( $key, $time = 0 ) {
83 return $this->client->delete( $this->encodeKey( $key ), $time );
84 }
85
86 /**
87 * @param $key string
88 * @param $value int
89 * @return Mixed
90 */
91 public function add( $key, $value, $exptime = 0 ) {
92 return $this->client->add( $this->encodeKey( $key ), $value,
93 $this->fixExpiry( $exptime ) );
94 }
95
96 /**
97 * @param $key string
98 * @param $value int
99 * @param $exptime
100 * @return Mixed
101 */
102 public function replace( $key, $value, $exptime = 0 ) {
103 return $this->client->replace( $this->encodeKey( $key ), $value,
104 $this->fixExpiry( $exptime ) );
105 }
106
107 /**
108 * Get the underlying client object. This is provided for debugging
109 * purposes.
110 */
111 public function getClient() {
112 return $this->client;
113 }
114
115 /**
116 * Encode a key for use on the wire inside the memcached protocol.
117 *
118 * We encode spaces and line breaks to avoid protocol errors. We encode
119 * the other control characters for compatibility with libmemcached
120 * verify_key. We leave other punctuation alone, to maximise backwards
121 * compatibility.
122 * @param $key string
123 * @return string
124 */
125 public function encodeKey( $key ) {
126 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
127 array( $this, 'encodeKeyCallback' ), $key );
128 }
129
130 /**
131 * @param $m array
132 * @return string
133 */
134 protected function encodeKeyCallback( $m ) {
135 return rawurlencode( $m[0] );
136 }
137
138 /**
139 * TTLs higher than 30 days will be detected as absolute TTLs
140 * (UNIX timestamps), and will result in the cache entry being
141 * discarded immediately because the expiry is in the past.
142 * Clamp expiries >30d at 30d, unless they're >=1e9 in which
143 * case they are likely to really be absolute (1e9 = 2011-09-09)
144 */
145 function fixExpiry( $expiry ) {
146 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
147 $expiry = 2592000;
148 }
149 return $expiry;
150 }
151
152 /**
153 * Decode a key encoded with encodeKey(). This is provided as a convenience
154 * function for debugging.
155 *
156 * @param $key string
157 *
158 * @return string
159 */
160 public function decodeKey( $key ) {
161 return urldecode( $key );
162 }
163
164 /**
165 * Send a debug message to the log
166 */
167 protected function debugLog( $text ) {
168 global $wgDebugLogGroups;
169 if( !isset( $wgDebugLogGroups['memcached'] ) ) {
170 # Prefix message since it will end up in main debug log file
171 $text = "memcached: $text";
172 }
173 if ( substr( $text, -1 ) !== "\n" ) {
174 $text .= "\n";
175 }
176 wfDebugLog( 'memcached', $text );
177 }
178 }
179