Merge "Remove return value from FormSpecialPage::checkExecutePermissions"
[mediawiki.git] / includes / objectcache / MemcachedBagOStuff.php
blob7d1274921c1c757f81f95e9336e1d28a3f310b4b
1 <?php
2 /**
3 * Base class for memcached clients.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Base class for memcached clients.
27 * @ingroup Cache
29 class MemcachedBagOStuff extends BagOStuff {
30 protected $client;
32 /**
33 * Fill in the defaults for any parameters missing from $params, using the
34 * backwards-compatible global variables
35 * @param array $params
36 * @return array
38 protected function applyDefaultParams( $params ) {
39 if ( !isset( $params['servers'] ) ) {
40 $params['servers'] = $GLOBALS['wgMemCachedServers'];
42 if ( !isset( $params['debug'] ) ) {
43 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
45 if ( !isset( $params['persistent'] ) ) {
46 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
48 if ( !isset( $params['compress_threshold'] ) ) {
49 $params['compress_threshold'] = 1500;
51 if ( !isset( $params['timeout'] ) ) {
52 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
54 if ( !isset( $params['connect_timeout'] ) ) {
55 $params['connect_timeout'] = 0.5;
57 return $params;
60 public function get( $key, &$casToken = null, $flags = 0 ) {
61 return $this->client->get( $this->encodeKey( $key ), $casToken );
64 /**
65 * @param string $key
66 * @param mixed $value
67 * @param int $exptime
68 * @return bool
70 public function set( $key, $value, $exptime = 0 ) {
71 return $this->client->set( $this->encodeKey( $key ), $value,
72 $this->fixExpiry( $exptime ) );
75 /**
76 * @param mixed $casToken
77 * @param string $key
78 * @param mixed $value
79 * @param int $exptime
80 * @return bool
82 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
83 return $this->client->cas( $casToken, $this->encodeKey( $key ),
84 $value, $this->fixExpiry( $exptime ) );
87 /**
88 * @param string $key
89 * @return bool
91 public function delete( $key ) {
92 return $this->client->delete( $this->encodeKey( $key ) );
95 /**
96 * @param string $key
97 * @param int $value
98 * @param int $exptime (default 0)
99 * @return mixed
101 public function add( $key, $value, $exptime = 0 ) {
102 return $this->client->add( $this->encodeKey( $key ), $value,
103 $this->fixExpiry( $exptime ) );
106 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
107 if ( !is_callable( $callback ) ) {
108 throw new Exception( "Got invalid callback." );
111 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
115 * Get the underlying client object. This is provided for debugging
116 * purposes.
117 * @return BagOStuff
119 public function getClient() {
120 return $this->client;
124 * Encode a key for use on the wire inside the memcached protocol.
126 * We encode spaces and line breaks to avoid protocol errors. We encode
127 * the other control characters for compatibility with libmemcached
128 * verify_key. We leave other punctuation alone, to maximise backwards
129 * compatibility.
130 * @param string $key
131 * @return string
133 public function encodeKey( $key ) {
134 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
135 array( $this, 'encodeKeyCallback' ), $key );
139 * @param array $m
140 * @return string
142 protected function encodeKeyCallback( $m ) {
143 return rawurlencode( $m[0] );
147 * TTLs higher than 30 days will be detected as absolute TTLs
148 * (UNIX timestamps), and will result in the cache entry being
149 * discarded immediately because the expiry is in the past.
150 * Clamp expires >30d at 30d, unless they're >=1e9 in which
151 * case they are likely to really be absolute (1e9 = 2011-09-09)
152 * @param int $expiry
153 * @return int
155 function fixExpiry( $expiry ) {
156 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
157 $expiry = 2592000;
159 return (int)$expiry;
163 * Decode a key encoded with encodeKey(). This is provided as a convenience
164 * function for debugging.
166 * @param string $key
168 * @return string
170 public function decodeKey( $key ) {
171 // matches %00-%20, %25, %7F (=decoded alternatives for those encoded in encodeKey)
172 return preg_replace_callback( '/%([0-1][0-9]|20|25|7F)/i', function ( $match ) {
173 return urldecode( $match[0] );
174 }, $key );
178 * Send a debug message to the log
179 * @param string $text
181 protected function debugLog( $text ) {
182 $this->logger->debug( $text );
185 public function modifySimpleRelayEvent( array $event ) {
186 if ( array_key_exists( 'val', $event ) ) {
187 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
190 return $event;