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
25 * Base class for memcached clients.
29 class MemcachedBagOStuff
extends BagOStuff
{
30 /** @var MWMemcached|Memcached */
34 * Fill in the defaults for any parameters missing from $params, using the
35 * backwards-compatible global variables
36 * @param array $params
39 protected function applyDefaultParams( $params ) {
40 if ( !isset( $params['servers'] ) ) {
41 $params['servers'] = $GLOBALS['wgMemCachedServers'];
43 if ( !isset( $params['debug'] ) ) {
44 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
46 if ( !isset( $params['persistent'] ) ) {
47 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
49 if ( !isset( $params['compress_threshold'] ) ) {
50 $params['compress_threshold'] = 1500;
52 if ( !isset( $params['timeout'] ) ) {
53 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
55 if ( !isset( $params['connect_timeout'] ) ) {
56 $params['connect_timeout'] = 0.5;
61 protected function doGet( $key, $flags = 0 ) {
64 return $this->getWithToken( $key, $casToken, $flags );
67 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
68 return $this->client
->get( $this->encodeKey( $key ), $casToken );
71 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
72 return $this->client
->set( $this->encodeKey( $key ), $value,
73 $this->fixExpiry( $exptime ) );
76 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
77 return $this->client
->cas( $casToken, $this->encodeKey( $key ),
78 $value, $this->fixExpiry( $exptime ) );
81 public function delete( $key ) {
82 return $this->client
->delete( $this->encodeKey( $key ) );
85 public function add( $key, $value, $exptime = 0 ) {
86 return $this->client
->add( $this->encodeKey( $key ), $value,
87 $this->fixExpiry( $exptime ) );
90 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
91 if ( !is_callable( $callback ) ) {
92 throw new Exception( "Got invalid callback." );
95 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
99 * Get the underlying client object. This is provided for debugging
103 public function getClient() {
104 return $this->client
;
108 * Encode a key for use on the wire inside the memcached protocol.
110 * We encode spaces and line breaks to avoid protocol errors. We encode
111 * the other control characters for compatibility with libmemcached
112 * verify_key. We leave other punctuation alone, to maximise backwards
117 public function encodeKey( $key ) {
118 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
119 array( $this, 'encodeKeyCallback' ), $key );
126 protected function encodeKeyCallback( $m ) {
127 return rawurlencode( $m[0] );
131 * TTLs higher than 30 days will be detected as absolute TTLs
132 * (UNIX timestamps), and will result in the cache entry being
133 * discarded immediately because the expiry is in the past.
134 * Clamp expires >30d at 30d, unless they're >=1e9 in which
135 * case they are likely to really be absolute (1e9 = 2011-09-09)
139 function fixExpiry( $expiry ) {
140 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
147 * Decode a key encoded with encodeKey(). This is provided as a convenience
148 * function for debugging.
154 public function decodeKey( $key ) {
155 // matches %00-%20, %25, %7F (=decoded alternatives for those encoded in encodeKey)
156 return preg_replace_callback( '/%([0-1][0-9]|20|25|7F)/i', function ( $match ) {
157 return urldecode( $match[0] );
162 * Send a debug message to the log
163 * @param string $text
165 protected function debugLog( $text ) {
166 $this->logger
->debug( $text );
169 public function modifySimpleRelayEvent( array $event ) {
170 if ( array_key_exists( 'val', $event ) ) {
171 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)