objectcache: Add changeTTL() method
[lhc/web/wiklou.git] / includes / libs / objectcache / MemcachedBagOStuff.php
1 <?php
2 /**
3 * Base class for memcached clients.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Base class for memcached clients.
26 *
27 * @ingroup Cache
28 */
29 class MemcachedBagOStuff extends BagOStuff {
30 /** @var MemcachedClient|Memcached */
31 protected $client;
32
33 /**
34 * Fill in some defaults for missing keys in $params.
35 *
36 * @param array $params
37 * @return array
38 */
39 protected function applyDefaultParams( $params ) {
40 if ( !isset( $params['compress_threshold'] ) ) {
41 $params['compress_threshold'] = 1500;
42 }
43 if ( !isset( $params['connect_timeout'] ) ) {
44 $params['connect_timeout'] = 0.5;
45 }
46 return $params;
47 }
48
49 protected function doGet( $key, $flags = 0 ) {
50 $casToken = null;
51
52 return $this->getWithToken( $key, $casToken, $flags );
53 }
54
55 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
56 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
57 }
58
59 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
60 return $this->client->set( $this->validateKeyEncoding( $key ), $value,
61 $this->fixExpiry( $exptime ) );
62 }
63
64 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
65 return $this->client->cas( $casToken, $this->validateKeyEncoding( $key ),
66 $value, $this->fixExpiry( $exptime ) );
67 }
68
69 public function delete( $key ) {
70 return $this->client->delete( $this->validateKeyEncoding( $key ) );
71 }
72
73 public function add( $key, $value, $exptime = 0 ) {
74 return $this->client->add( $this->validateKeyEncoding( $key ), $value,
75 $this->fixExpiry( $exptime ) );
76 }
77
78 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
79 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
80 }
81
82 public function changeTTL( $key, $exptime = 0 ) {
83 return $this->client->touch( $this->validateKeyEncoding( $key ),
84 $this->fixExpiry( $exptime ) );
85 }
86
87 /**
88 * Get the underlying client object. This is provided for debugging
89 * purposes.
90 * @return MemcachedClient|Memcached
91 */
92 public function getClient() {
93 return $this->client;
94 }
95
96 /**
97 * Construct a cache key.
98 *
99 * @since 1.27
100 * @param string $keyspace
101 * @param array $args
102 * @return string
103 */
104 public function makeKeyInternal( $keyspace, $args ) {
105 // Memcached keys have a maximum length of 255 characters. From that,
106 // subtract the number of characters we need for the keyspace and for
107 // the separator character needed for each argument. To handle some
108 // custom prefixes used by thing like WANObjectCache, limit to 205.
109 $charsLeft = 205 - strlen( $keyspace ) - count( $args );
110
111 $args = array_map(
112 function ( $arg ) use ( &$charsLeft ) {
113 $arg = strtr( $arg, ' ', '_' );
114
115 // Make sure %, #, and non-ASCII chars are escaped
116 $arg = preg_replace_callback(
117 '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
118 function ( $m ) {
119 return rawurlencode( $m[0] );
120 },
121 $arg
122 );
123
124 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
125 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
126 $arg = '#' . md5( $arg );
127 }
128
129 $charsLeft -= strlen( $arg );
130 return $arg;
131 },
132 $args
133 );
134
135 if ( $charsLeft < 0 ) {
136 return $keyspace . ':##' . md5( implode( ':', $args ) );
137 }
138
139 return $keyspace . ':' . implode( ':', $args );
140 }
141
142 /**
143 * Ensure that a key is safe to use (contains no control characters and no
144 * characters above the ASCII range.)
145 *
146 * @param string $key
147 * @return string
148 * @throws Exception
149 */
150 public function validateKeyEncoding( $key ) {
151 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
152 throw new Exception( "Key contains invalid characters: $key" );
153 }
154 return $key;
155 }
156
157 /**
158 * TTLs higher than 30 days will be detected as absolute TTLs
159 * (UNIX timestamps), and will result in the cache entry being
160 * discarded immediately because the expiry is in the past.
161 * Clamp expires >30d at 30d, unless they're >=1e9 in which
162 * case they are likely to really be absolute (1e9 = 2011-09-09)
163 * @param int $expiry
164 * @return int
165 */
166 function fixExpiry( $expiry ) {
167 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
168 $expiry = 2592000;
169 }
170 return (int)$expiry;
171 }
172
173 /**
174 * Send a debug message to the log
175 * @param string $text
176 */
177 protected function debugLog( $text ) {
178 $this->logger->debug( $text );
179 }
180
181 public function modifySimpleRelayEvent( array $event ) {
182 if ( array_key_exists( 'val', $event ) ) {
183 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
184 }
185
186 return $event;
187 }
188 }