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