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
{
30 /** @var MWMemcached|Memcached */
34 * Fill in some defaults for missing keys in $params.
36 * @param array $params
39 protected function applyDefaultParams( $params ) {
40 if ( !isset( $params['compress_threshold'] ) ) {
41 $params['compress_threshold'] = 1500;
43 if ( !isset( $params['connect_timeout'] ) ) {
44 $params['connect_timeout'] = 0.5;
49 protected function doGet( $key, $flags = 0 ) {
52 return $this->getWithToken( $key, $casToken, $flags );
55 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
56 return $this->client
->get( $this->validateKeyEncoding( $key ), $casToken );
59 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
60 return $this->client
->set( $this->validateKeyEncoding( $key ), $value,
61 $this->fixExpiry( $exptime ) );
64 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
65 return $this->client
->cas( $casToken, $this->validateKeyEncoding( $key ),
66 $value, $this->fixExpiry( $exptime ) );
69 public function delete( $key ) {
70 return $this->client
->delete( $this->validateKeyEncoding( $key ) );
73 public function add( $key, $value, $exptime = 0 ) {
74 return $this->client
->add( $this->validateKeyEncoding( $key ), $value,
75 $this->fixExpiry( $exptime ) );
78 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
79 if ( !is_callable( $callback ) ) {
80 throw new Exception( "Got invalid callback." );
83 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
87 * Get the underlying client object. This is provided for debugging
91 public function getClient() {
96 * Construct a cache key.
99 * @param string $keyspace
103 public function makeKeyInternal( $keyspace, $args ) {
104 // Memcached keys have a maximum length of 255 characters. From that,
105 // subtract the number of characters we need for the keyspace and for
106 // the separator character needed for each argument.
107 $charsLeft = 255 - strlen( $keyspace ) - count( $args );
110 function ( $arg ) use ( &$charsLeft ) {
111 $arg = strtr( $arg, ' ', '_' );
113 // Make sure %, #, and non-ASCII chars are escaped
114 $arg = preg_replace_callback(
115 '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
117 return rawurlencode( $m[0] );
122 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
123 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
124 $arg = '#' . md5( $arg );
127 $charsLeft -= strlen( $arg );
133 if ( $charsLeft < 0 ) {
134 return $keyspace . ':##' . md5( implode( ':', $args ) );
137 return $keyspace . ':' . implode( ':', $args );
141 * Ensure that a key is safe to use (contains no control characters and no
142 * characters above the ASCII range.)
148 public function validateKeyEncoding( $key ) {
149 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
150 throw new Exception( "Key contains invalid characters: $key" );
156 * TTLs higher than 30 days will be detected as absolute TTLs
157 * (UNIX timestamps), and will result in the cache entry being
158 * discarded immediately because the expiry is in the past.
159 * Clamp expires >30d at 30d, unless they're >=1e9 in which
160 * case they are likely to really be absolute (1e9 = 2011-09-09)
164 function fixExpiry( $expiry ) {
165 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
172 * Send a debug message to the log
173 * @param string $text
175 protected function debugLog( $text ) {
176 $this->logger
->debug( $text );
179 public function modifySimpleRelayEvent( array $event ) {
180 if ( array_key_exists( 'val', $event ) ) {
181 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)