Merge "Remove extra unneeded whitespace"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedBagOStuff.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Cache
20 */
21
22 /**
23 * Base class for memcached clients.
24 *
25 * @ingroup Cache
26 */
27 class MemcachedBagOStuff extends BagOStuff {
28 protected $client;
29
30 /**
31 * Fill in the defaults for any parameters missing from $params, using the
32 * backwards-compatible global variables
33 */
34 protected function applyDefaultParams( $params ) {
35 if ( !isset( $params['servers'] ) ) {
36 $params['servers'] = $GLOBALS['wgMemCachedServers'];
37 }
38 if ( !isset( $params['debug'] ) ) {
39 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
40 }
41 if ( !isset( $params['persistent'] ) ) {
42 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
43 }
44 if ( !isset( $params['compress_threshold'] ) ) {
45 $params['compress_threshold'] = 1500;
46 }
47 if ( !isset( $params['timeout'] ) ) {
48 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
49 }
50 if ( !isset( $params['connect_timeout'] ) ) {
51 $params['connect_timeout'] = 0.1;
52 }
53 return $params;
54 }
55
56 /**
57 * @param $key string
58 * @return Mixed
59 */
60 public function get( $key ) {
61 return $this->client->get( $this->encodeKey( $key ) );
62 }
63
64 /**
65 * @param $key string
66 * @param $value
67 * @param $exptime int
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 $key string
77 * @param $time int
78 * @return bool
79 */
80 public function delete( $key, $time = 0 ) {
81 return $this->client->delete( $this->encodeKey( $key ), $time );
82 }
83
84 /**
85 * @param $key string
86 * @param $value int
87 * @return Mixed
88 */
89 public function add( $key, $value, $exptime = 0 ) {
90 return $this->client->add( $this->encodeKey( $key ), $value,
91 $this->fixExpiry( $exptime ) );
92 }
93
94 /**
95 * @param $key string
96 * @param $value int
97 * @param $exptime
98 * @return Mixed
99 */
100 public function replace( $key, $value, $exptime = 0 ) {
101 return $this->client->replace( $this->encodeKey( $key ), $value,
102 $this->fixExpiry( $exptime ) );
103 }
104
105 /**
106 * Get the underlying client object. This is provided for debugging
107 * purposes.
108 */
109 public function getClient() {
110 return $this->client;
111 }
112
113 /**
114 * Encode a key for use on the wire inside the memcached protocol.
115 *
116 * We encode spaces and line breaks to avoid protocol errors. We encode
117 * the other control characters for compatibility with libmemcached
118 * verify_key. We leave other punctuation alone, to maximise backwards
119 * compatibility.
120 * @return string
121 */
122 public function encodeKey( $key ) {
123 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
124 array( $this, 'encodeKeyCallback' ), $key );
125 }
126
127 protected function encodeKeyCallback( $m ) {
128 return rawurlencode( $m[0] );
129 }
130
131 /**
132 * TTLs higher than 30 days will be detected as absolute TTLs
133 * (UNIX timestamps), and will result in the cache entry being
134 * discarded immediately because the expiry is in the past.
135 * Clamp expiries >30d at 30d, unless they're >=1e9 in which
136 * case they are likely to really be absolute (1e9 = 2011-09-09)
137 */
138 function fixExpiry( $expiry ) {
139 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
140 $expiry = 2592000;
141 }
142 return $expiry;
143 }
144
145 /**
146 * Decode a key encoded with encodeKey(). This is provided as a convenience
147 * function for debugging.
148 *
149 * @param $key string
150 *
151 * @return string
152 */
153 public function decodeKey( $key ) {
154 return urldecode( $key );
155 }
156
157 /**
158 * Send a debug message to the log
159 */
160 protected function debugLog( $text ) {
161 global $wgDebugLogGroups;
162 if( !isset( $wgDebugLogGroups['memcached'] ) ) {
163 # Prefix message since it will end up in main debug log file
164 $text = "memcached: $text";
165 }
166 if ( substr( $text, -1 ) !== "\n" ) {
167 $text .= "\n";
168 }
169 wfDebugLog( 'memcached', $text );
170 }
171 }
172