MediaWiki REL1_31
MemcachedBagOStuff.php
Go to the documentation of this file.
1<?php
31 protected $client;
32
33 function __construct( array $params ) {
34 parent::__construct( $params );
35
36 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_BE; // unreliable
37 }
38
45 protected function applyDefaultParams( $params ) {
46 return $params + [
47 'compress_threshold' => 1500,
48 'connect_timeout' => 0.5,
49 'debug' => false
50 ];
51 }
52
53 protected function doGet( $key, $flags = 0 ) {
54 $casToken = null;
55
56 return $this->getWithToken( $key, $casToken, $flags );
57 }
58
59 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
60 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
61 }
62
63 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
64 return $this->client->set( $this->validateKeyEncoding( $key ), $value,
65 $this->fixExpiry( $exptime ) );
66 }
67
68 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
69 return $this->client->cas( $casToken, $this->validateKeyEncoding( $key ),
70 $value, $this->fixExpiry( $exptime ) );
71 }
72
73 public function delete( $key ) {
74 return $this->client->delete( $this->validateKeyEncoding( $key ) );
75 }
76
77 public function add( $key, $value, $exptime = 0 ) {
78 return $this->client->add( $this->validateKeyEncoding( $key ), $value,
79 $this->fixExpiry( $exptime ) );
80 }
81
82 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
83 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
84 }
85
86 public function changeTTL( $key, $exptime = 0 ) {
87 return $this->client->touch( $this->validateKeyEncoding( $key ),
88 $this->fixExpiry( $exptime ) );
89 }
90
96 public function getClient() {
97 return $this->client;
98 }
99
108 public function makeKeyInternal( $keyspace, $args ) {
109 // Memcached keys have a maximum length of 255 characters. From that,
110 // subtract the number of characters we need for the keyspace and for
111 // the separator character needed for each argument. To handle some
112 // custom prefixes used by thing like WANObjectCache, limit to 205.
113 $charsLeft = 205 - strlen( $keyspace ) - count( $args );
114
115 $args = array_map(
116 function ( $arg ) use ( &$charsLeft ) {
117 $arg = strtr( $arg, ' ', '_' );
118
119 // Make sure %, #, and non-ASCII chars are escaped
120 $arg = preg_replace_callback(
121 '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
122 function ( $m ) {
123 return rawurlencode( $m[0] );
124 },
125 $arg
126 );
127
128 // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
129 if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
130 $arg = '#' . md5( $arg );
131 }
132
133 $charsLeft -= strlen( $arg );
134 return $arg;
135 },
136 $args
137 );
138
139 if ( $charsLeft < 0 ) {
140 return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
141 }
142
143 return $keyspace . ':' . implode( ':', $args );
144 }
145
154 public function validateKeyEncoding( $key ) {
155 if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
156 throw new Exception( "Key contains invalid characters: $key" );
157 }
158 return $key;
159 }
160
170 function fixExpiry( $expiry ) {
171 if ( $expiry > 2592000 && $expiry < 1000000000 ) {
172 $expiry = 2592000;
173 }
174 return (int)$expiry;
175 }
176
181 protected function debugLog( $text ) {
182 $this->logger->debug( $text );
183 }
184
185 public function modifySimpleRelayEvent( array $event ) {
186 if ( array_key_exists( 'val', $event ) ) {
187 $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
188 }
189
190 return $event;
191 }
192}
if( $line===false) $args
Definition cdb.php:64
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
string $keyspace
Definition BagOStuff.php:53
mergeViaCas( $key, $callback, $exptime=0, $attempts=10)
Base class for memcached clients.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
modifySimpleRelayEvent(array $event)
Modify a cache update operation array for EventRelayer::notify()
doGet( $key, $flags=0)
getClient()
Get the underlying client object.
validateKeyEncoding( $key)
Ensure that a key is safe to use (contains no control characters and no characters above the ASCII ra...
MemcachedClient Memcached $client
getWithToken( $key, &$casToken, $flags=0)
debugLog( $text)
Send a debug message to the log.
applyDefaultParams( $params)
Fill in some defaults for missing keys in $params.
add( $key, $value, $exptime=0)
__construct(array $params)
$params include:
changeTTL( $key, $exptime=0)
Reset the TTL on a key if it exists.
fixExpiry( $expiry)
TTLs higher than 30 days will be detected as absolute TTLs (UNIX timestamps), and will result in the ...
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
cas( $casToken, $key, $value, $exptime=0)
Check and set an item.
memcached client class implemented using (p)fsockopen()
The phpstorm stubs package includes the Memcached class with two parameters and docs saying that they...
Definition memcached.php:11
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
$params