Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / objectcache / MemcachedBagOStuff.php
blobac34570136ae61ab1dcce26f3b27d4ecfbb3ccaa
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
35 * @param array $params
36 * @return array
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;
57 return $params;
60 /**
61 * @param string $key
62 * @param mixed $casToken [optional]
63 * @return mixed
65 public function get( $key, &$casToken = null ) {
66 return $this->client->get( $this->encodeKey( $key ), $casToken );
69 /**
70 * @param string $key
71 * @param mixed $value
72 * @param int $exptime
73 * @return bool
75 public function set( $key, $value, $exptime = 0 ) {
76 return $this->client->set( $this->encodeKey( $key ), $value,
77 $this->fixExpiry( $exptime ) );
80 /**
81 * @param mixed $casToken
82 * @param string $key
83 * @param mixed $value
84 * @param int $exptime
85 * @return bool
87 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
88 return $this->client->cas( $casToken, $this->encodeKey( $key ),
89 $value, $this->fixExpiry( $exptime ) );
92 /**
93 * @param string $key
94 * @return bool
96 public function delete( $key ) {
97 return $this->client->delete( $this->encodeKey( $key ) );
101 * @param string $key
102 * @param int $value
103 * @param int $exptime (default 0)
104 * @return mixed
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
113 * purposes.
114 * @return BagOStuff
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
126 * compatibility.
127 * @param string $key
128 * @return string
130 public function encodeKey( $key ) {
131 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
132 array( $this, 'encodeKeyCallback' ), $key );
136 * @param array $m
137 * @return string
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)
149 * @param int $expiry
150 * @return int
152 function fixExpiry( $expiry ) {
153 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
154 $expiry = 2592000;
156 return (int)$expiry;
160 * Decode a key encoded with encodeKey(). This is provided as a convenience
161 * function for debugging.
163 * @param string $key
165 * @return string
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 );