Merge "[JobQueue] Update "empty" cache key when jobs are recycled."
[mediawiki.git] / includes / objectcache / MemcachedBagOStuff.php
blob4f8209d9f8631fa771be5d10a855c03fb8f5b26a
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
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;
55 return $params;
58 /**
59 * @param $key string
60 * @param $casToken[optional] mixed
61 * @return Mixed
63 public function get( $key, &$casToken = null ) {
64 return $this->client->get( $this->encodeKey( $key ), $casToken );
67 /**
68 * @param $key string
69 * @param $value
70 * @param $exptime int
71 * @return bool
73 public function set( $key, $value, $exptime = 0 ) {
74 return $this->client->set( $this->encodeKey( $key ), $value,
75 $this->fixExpiry( $exptime ) );
78 /**
79 * @param $key string
80 * @param $casToken mixed
81 * @param $value
82 * @param $exptime int
83 * @return bool
85 public function cas( $casToken, $key, $value, $exptime = 0 ) {
86 return $this->client->cas( $casToken, $this->encodeKey( $key ),
87 $value, $this->fixExpiry( $exptime ) );
90 /**
91 * @param $key string
92 * @param $time int
93 * @return bool
95 public function delete( $key, $time = 0 ) {
96 return $this->client->delete( $this->encodeKey( $key ), $time );
99 /**
100 * @param $key string
101 * @param $value int
102 * @param $exptime int (default 0)
103 * @return Mixed
105 public function add( $key, $value, $exptime = 0 ) {
106 return $this->client->add( $this->encodeKey( $key ), $value,
107 $this->fixExpiry( $exptime ) );
111 * @param $key string
112 * @param $value int
113 * @param $exptime
114 * @return Mixed
116 public function replace( $key, $value, $exptime = 0 ) {
117 return $this->client->replace( $this->encodeKey( $key ), $value,
118 $this->fixExpiry( $exptime ) );
122 * Get the underlying client object. This is provided for debugging
123 * purposes.
125 public function getClient() {
126 return $this->client;
130 * Encode a key for use on the wire inside the memcached protocol.
132 * We encode spaces and line breaks to avoid protocol errors. We encode
133 * the other control characters for compatibility with libmemcached
134 * verify_key. We leave other punctuation alone, to maximise backwards
135 * compatibility.
136 * @param $key string
137 * @return string
139 public function encodeKey( $key ) {
140 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
141 array( $this, 'encodeKeyCallback' ), $key );
145 * @param $m array
146 * @return string
148 protected function encodeKeyCallback( $m ) {
149 return rawurlencode( $m[0] );
153 * TTLs higher than 30 days will be detected as absolute TTLs
154 * (UNIX timestamps), and will result in the cache entry being
155 * discarded immediately because the expiry is in the past.
156 * Clamp expiries >30d at 30d, unless they're >=1e9 in which
157 * case they are likely to really be absolute (1e9 = 2011-09-09)
159 function fixExpiry( $expiry ) {
160 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
161 $expiry = 2592000;
163 return $expiry;
167 * Decode a key encoded with encodeKey(). This is provided as a convenience
168 * function for debugging.
170 * @param $key string
172 * @return string
174 public function decodeKey( $key ) {
175 return urldecode( $key );
179 * Send a debug message to the log
181 protected function debugLog( $text ) {
182 if ( substr( $text, -1 ) !== "\n" ) {
183 $text .= "\n";
185 wfDebugLog( 'memcached', $text );