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
35 * @param array $params
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;
62 * @param mixed $casToken [optional]
65 public function get( $key, &$casToken = null ) {
66 return $this->client
->get( $this->encodeKey( $key ), $casToken );
75 public function set( $key, $value, $exptime = 0 ) {
76 return $this->client
->set( $this->encodeKey( $key ), $value,
77 $this->fixExpiry( $exptime ) );
81 * @param mixed $casToken
87 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
88 return $this->client
->cas( $casToken, $this->encodeKey( $key ),
89 $value, $this->fixExpiry( $exptime ) );
96 public function delete( $key ) {
97 return $this->client
->delete( $this->encodeKey( $key ) );
103 * @param int $exptime (default 0)
106 public function add( $key, $value, $exptime = 0 ) {
107 return $this->client
->add( $this->encodeKey( $key ), $value,
108 $this->fixExpiry( $exptime ) );
112 * Get the underlying client object. This is provided for debugging
116 public function getClient() {
117 return $this->client
;
121 * Encode a key for use on the wire inside the memcached protocol.
123 * We encode spaces and line breaks to avoid protocol errors. We encode
124 * the other control characters for compatibility with libmemcached
125 * verify_key. We leave other punctuation alone, to maximise backwards
130 public function encodeKey( $key ) {
131 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
132 array( $this, 'encodeKeyCallback' ), $key );
139 protected function encodeKeyCallback( $m ) {
140 return rawurlencode( $m[0] );
144 * TTLs higher than 30 days will be detected as absolute TTLs
145 * (UNIX timestamps), and will result in the cache entry being
146 * discarded immediately because the expiry is in the past.
147 * Clamp expires >30d at 30d, unless they're >=1e9 in which
148 * case they are likely to really be absolute (1e9 = 2011-09-09)
152 function fixExpiry( $expiry ) {
153 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
160 * Decode a key encoded with encodeKey(). This is provided as a convenience
161 * function for debugging.
167 public function decodeKey( $key ) {
168 return urldecode( $key );
172 * Send a debug message to the log
173 * @param string $text
175 protected function debugLog( $text ) {
176 $this->logger
->debug( $text );