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
{
33 * Fill in the defaults for any parameters missing from $params, using the
34 * backwards-compatible global variables
36 protected function applyDefaultParams( $params ) {
37 if ( !isset( $params['servers'] ) ) {
38 $params['servers'] = $GLOBALS['wgMemCachedServers'];
40 if ( !isset( $params['debug'] ) ) {
41 $params['debug'] = $GLOBALS['wgMemCachedDebug'];
43 if ( !isset( $params['persistent'] ) ) {
44 $params['persistent'] = $GLOBALS['wgMemCachedPersistent'];
46 if ( !isset( $params['compress_threshold'] ) ) {
47 $params['compress_threshold'] = 1500;
49 if ( !isset( $params['timeout'] ) ) {
50 $params['timeout'] = $GLOBALS['wgMemCachedTimeout'];
52 if ( !isset( $params['connect_timeout'] ) ) {
53 $params['connect_timeout'] = 0.5;
62 public function get( $key ) {
63 return $this->client
->get( $this->encodeKey( $key ) );
72 public function set( $key, $value, $exptime = 0 ) {
73 return $this->client
->set( $this->encodeKey( $key ), $value,
74 $this->fixExpiry( $exptime ) );
82 public function delete( $key, $time = 0 ) {
83 return $this->client
->delete( $this->encodeKey( $key ), $time );
89 * @param $exptime int (default 0)
92 public function add( $key, $value, $exptime = 0 ) {
93 return $this->client
->add( $this->encodeKey( $key ), $value,
94 $this->fixExpiry( $exptime ) );
103 public function replace( $key, $value, $exptime = 0 ) {
104 return $this->client
->replace( $this->encodeKey( $key ), $value,
105 $this->fixExpiry( $exptime ) );
109 * Get the underlying client object. This is provided for debugging
112 public function getClient() {
113 return $this->client
;
117 * Encode a key for use on the wire inside the memcached protocol.
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
126 public function encodeKey( $key ) {
127 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
128 array( $this, 'encodeKeyCallback' ), $key );
135 protected function encodeKeyCallback( $m ) {
136 return rawurlencode( $m[0] );
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)
146 function fixExpiry( $expiry ) {
147 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
154 * Decode a key encoded with encodeKey(). This is provided as a convenience
155 * function for debugging.
161 public function decodeKey( $key ) {
162 return urldecode( $key );
166 * Send a debug message to the log
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";
174 if ( substr( $text, -1 ) !== "\n" ) {
177 wfDebugLog( 'memcached', $text );