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.
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.
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
23 * Base class for memcached clients.
27 class MemcachedBagOStuff
extends BagOStuff
{
31 * Fill in the defaults for any parameters missing from $params, using the
32 * backwards-compatible global variables
34 protected function applyDefaultParams( $params ) {
35 if ( !isset( $params['servers'] ) ) {
36 $params['servers'] = $GLOBALS['wgMemCachedServers'];
38 if ( !isset( $params['debug'] ) ) {
39 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
41 if ( !isset( $params['persistent'] ) ) {
42 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
44 if ( !isset( $params['compress_threshold'] ) ) {
45 $params['compress_threshold'] = 1500;
47 if ( !isset( $params['timeout'] ) ) {
48 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
50 if ( !isset( $params['connect_timeout'] ) ) {
51 $params['connect_timeout'] = 0.1;
60 public function get( $key ) {
61 return $this->client
->get( $this->encodeKey( $key ) );
70 public function set( $key, $value, $exptime = 0 ) {
71 return $this->client
->set( $this->encodeKey( $key ), $value,
72 $this->fixExpiry( $exptime ) );
80 public function delete( $key, $time = 0 ) {
81 return $this->client
->delete( $this->encodeKey( $key ), $time );
89 public function add( $key, $value, $exptime = 0 ) {
90 return $this->client
->add( $this->encodeKey( $key ), $value,
91 $this->fixExpiry( $exptime ) );
100 public function replace( $key, $value, $exptime = 0 ) {
101 return $this->client
->replace( $this->encodeKey( $key ), $value,
102 $this->fixExpiry( $exptime ) );
106 * Get the underlying client object. This is provided for debugging
109 public function getClient() {
110 return $this->client
;
114 * Encode a key for use on the wire inside the memcached protocol.
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
122 public function encodeKey( $key ) {
123 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
124 array( $this, 'encodeKeyCallback' ), $key );
127 protected function encodeKeyCallback( $m ) {
128 return rawurlencode( $m[0] );
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)
138 function fixExpiry( $expiry ) {
139 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
146 * Decode a key encoded with encodeKey(). This is provided as a convenience
147 * function for debugging.
153 public function decodeKey( $key ) {
154 return urldecode( $key );
158 * Send a debug message to the log
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";
166 if ( substr( $text, -1 ) !== "\n" ) {
169 wfDebugLog( 'memcached', $text );