MediaWiki  1.27.1
MemcachedBagOStuff.php
Go to the documentation of this file.
1 <?php
31  protected $client;
32 
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, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
79  if ( !is_callable( $callback ) ) {
80  throw new Exception( "Got invalid callback." );
81  }
82 
83  return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
84  }
85 
91  public function getClient() {
92  return $this->client;
93  }
94 
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. To handle some
107  // custom prefixes used by thing like WANObjectCache, limit to 205.
108  $charsLeft = 205 - strlen( $keyspace ) - count( $args );
109 
110  $args = array_map(
111  function ( $arg ) use ( &$charsLeft ) {
112  $arg = strtr( $arg, ' ', '_' );
113 
114  // Make sure %, #, and non-ASCII chars are escaped
115  $arg = preg_replace_callback(
116  '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
117  function ( $m ) {
118  return rawurlencode( $m[0] );
119  },
120  $arg
121  );
122 
123  // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
124  if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
125  $arg = '#' . md5( $arg );
126  }
127 
128  $charsLeft -= strlen( $arg );
129  return $arg;
130  },
131  $args
132  );
133 
134  if ( $charsLeft < 0 ) {
135  return $keyspace . ':##' . md5( implode( ':', $args ) );
136  }
137 
138  return $keyspace . ':' . implode( ':', $args );
139  }
140 
149  public function validateKeyEncoding( $key ) {
150  if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
151  throw new Exception( "Key contains invalid characters: $key" );
152  }
153  return $key;
154  }
155 
165  function fixExpiry( $expiry ) {
166  if ( $expiry > 2592000 && $expiry < 1000000000 ) {
167  $expiry = 2592000;
168  }
169  return (int)$expiry;
170  }
171 
176  protected function debugLog( $text ) {
177  $this->logger->debug( $text );
178  }
179 
180  public function modifySimpleRelayEvent( array $event ) {
181  if ( array_key_exists( 'val', $event ) ) {
182  $event['flg'] = 0; // data is not serialized nor gzipped (for memcached driver)
183  }
184 
185  return $event;
186  }
187 }
MemcachedClient Memcached $client
merge($key, $callback, $exptime=0, $attempts=10, $flags=0)
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$value
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2548
fixExpiry($expiry)
TTLs higher than 30 days will be detected as absolute TTLs (UNIX timestamps), and will result in the ...
string $keyspace
Definition: BagOStuff.php:53
if($line===false) $args
Definition: cdb.php:64
mergeViaCas($key, $callback, $exptime=0, $attempts=10)
Definition: BagOStuff.php:285
makeKeyInternal($keyspace, $args)
Construct a cache key.
$params
validateKeyEncoding($key)
Ensure that a key is safe to use (contains no control characters and no characters above the ASCII ra...
set($key, $value, $exptime=0, $flags=0)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
modifySimpleRelayEvent(array $event)
Base class for memcached clients.
doGet($key, $flags=0)
add($key, $value, $exptime=0)
getClient()
Get the underlying client object.
debugLog($text)
Send a debug message to the log.
applyDefaultParams($params)
Fill in some defaults for missing keys in $params.
getWithToken($key, &$casToken, $flags=0)
cas($casToken, $key, $value, $exptime=0)