MWException: Don't send headers multiple times
[mediawiki.git] / includes / objectcache / MemcachedBagOStuff.php
blob79a684137e1d5b96ffc3d54ea534cdc23feda61d
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 string $key
82 * @param mixed $casToken
83 * @param mixed $value
84 * @param int $exptime
85 * @return bool
87 public 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 * @param int $time
95 * @return bool
97 public function delete( $key, $time = 0 ) {
98 return $this->client->delete( $this->encodeKey( $key ), $time );
102 * @param string $key
103 * @param int $value
104 * @param int $exptime (default 0)
105 * @return mixed
107 public function add( $key, $value, $exptime = 0 ) {
108 return $this->client->add( $this->encodeKey( $key ), $value,
109 $this->fixExpiry( $exptime ) );
113 * Get the underlying client object. This is provided for debugging
114 * purposes.
115 * @return BagOStuff
117 public function getClient() {
118 return $this->client;
122 * Encode a key for use on the wire inside the memcached protocol.
124 * We encode spaces and line breaks to avoid protocol errors. We encode
125 * the other control characters for compatibility with libmemcached
126 * verify_key. We leave other punctuation alone, to maximise backwards
127 * compatibility.
128 * @param string $key
129 * @return string
131 public function encodeKey( $key ) {
132 return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
133 array( $this, 'encodeKeyCallback' ), $key );
137 * @param array $m
138 * @return string
140 protected function encodeKeyCallback( $m ) {
141 return rawurlencode( $m[0] );
145 * TTLs higher than 30 days will be detected as absolute TTLs
146 * (UNIX timestamps), and will result in the cache entry being
147 * discarded immediately because the expiry is in the past.
148 * Clamp expiries >30d at 30d, unless they're >=1e9 in which
149 * case they are likely to really be absolute (1e9 = 2011-09-09)
150 * @param int $expiry
151 * @return int
153 function fixExpiry( $expiry ) {
154 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
155 $expiry = 2592000;
157 return $expiry;
161 * Decode a key encoded with encodeKey(). This is provided as a convenience
162 * function for debugging.
164 * @param string $key
166 * @return string
168 public function decodeKey( $key ) {
169 return urldecode( $key );
173 * Send a debug message to the log
174 * @param string $text
176 protected function debugLog( $text ) {
177 wfDebugLog( 'memcached', $text );