MediaWiki  1.33.0
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, &$casToken = null ) {
54  return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
55  }
56 
57  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
58  return $this->client->set( $this->validateKeyEncoding( $key ), $value,
59  $this->fixExpiry( $exptime ) );
60  }
61 
62  protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
63  return $this->client->cas( $casToken, $this->validateKeyEncoding( $key ),
64  $value, $this->fixExpiry( $exptime ) );
65  }
66 
67  public function delete( $key, $flags = 0 ) {
68  return $this->client->delete( $this->validateKeyEncoding( $key ) );
69  }
70 
71  public function add( $key, $value, $exptime = 0, $flags = 0 ) {
72  return $this->client->add( $this->validateKeyEncoding( $key ), $value,
73  $this->fixExpiry( $exptime ) );
74  }
75 
76  public function incr( $key, $value = 1 ) {
77  $n = $this->client->incr( $this->validateKeyEncoding( $key ), $value );
78 
79  return ( $n !== false && $n !== null ) ? $n : false;
80  }
81 
82  public function decr( $key, $value = 1 ) {
83  $n = $this->client->decr( $this->validateKeyEncoding( $key ), $value );
84 
85  return ( $n !== false && $n !== null ) ? $n : false;
86  }
87 
88  public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
89  return $this->client->touch( $this->validateKeyEncoding( $key ),
90  $this->fixExpiry( $exptime ) );
91  }
92 
98  public function getClient() {
99  return $this->client;
100  }
101 
110  public function makeKeyInternal( $keyspace, $args ) {
111  // Memcached keys have a maximum length of 255 characters. From that,
112  // subtract the number of characters we need for the keyspace and for
113  // the separator character needed for each argument. To handle some
114  // custom prefixes used by thing like WANObjectCache, limit to 205.
115  $charsLeft = 205 - strlen( $keyspace ) - count( $args );
116 
117  $args = array_map(
118  function ( $arg ) use ( &$charsLeft ) {
119  $arg = strtr( $arg, ' ', '_' );
120 
121  // Make sure %, #, and non-ASCII chars are escaped
122  $arg = preg_replace_callback(
123  '/[^\x21-\x22\x24\x26-\x39\x3b-\x7e]+/',
124  function ( $m ) {
125  return rawurlencode( $m[0] );
126  },
127  $arg
128  );
129 
130  // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
131  if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
132  $arg = '#' . md5( $arg );
133  }
134 
135  $charsLeft -= strlen( $arg );
136  return $arg;
137  },
138  $args
139  );
140 
141  if ( $charsLeft < 0 ) {
142  return $keyspace . ':BagOStuff-long-key:##' . md5( implode( ':', $args ) );
143  }
144 
145  return $keyspace . ':' . implode( ':', $args );
146  }
147 
156  public function validateKeyEncoding( $key ) {
157  if ( preg_match( '/[^\x21-\x7e]+/', $key ) ) {
158  throw new Exception( "Key contains invalid characters: $key" );
159  }
160  return $key;
161  }
162 
172  function fixExpiry( $expiry ) {
173  if ( $expiry > 2592000 && $expiry < 1000000000 ) {
174  $expiry = 2592000;
175  }
176  return (int)$expiry;
177  }
178 
183  protected function debugLog( $text ) {
184  $this->logger->debug( $text );
185  }
186 }
MemcachedBagOStuff\fixExpiry
fixExpiry( $expiry)
TTLs higher than 30 days will be detected as absolute TTLs (UNIX timestamps), and will result in the ...
Definition: MemcachedBagOStuff.php:172
MemcachedBagOStuff\incr
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
Definition: MemcachedBagOStuff.php:76
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MemcachedBagOStuff
Base class for memcached clients.
Definition: MemcachedBagOStuff.php:29
captcha-old.count
count
Definition: captcha-old.py:249
MemcachedBagOStuff\applyDefaultParams
applyDefaultParams( $params)
Fill in some defaults for missing keys in $params.
Definition: MemcachedBagOStuff.php:45
$params
$params
Definition: styleTest.css.php:44
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:58
php
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
MemcachedBagOStuff\debugLog
debugLog( $text)
Send a debug message to the log.
Definition: MemcachedBagOStuff.php:183
MemcachedClient
memcached client class implemented using (p)fsockopen()
Definition: MemcachedClient.php:79
IExpiringStore\QOS_SYNCWRITES_BE
const QOS_SYNCWRITES_BE
Definition: IExpiringStore.php:56
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MemcachedBagOStuff\changeTTL
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
Definition: MemcachedBagOStuff.php:88
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$value
$value
Definition: styleTest.css.php:49
IExpiringStore\ATTR_SYNCWRITES
const ATTR_SYNCWRITES
Definition: IExpiringStore.php:53
MemcachedBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Definition: MemcachedBagOStuff.php:53
MemcachedBagOStuff\cas
cas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
Definition: MemcachedBagOStuff.php:62
MemcachedBagOStuff\decr
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
Definition: MemcachedBagOStuff.php:82
MemcachedBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $args)
Construct a cache key.
Definition: MemcachedBagOStuff.php:110
$args
if( $line===false) $args
Definition: cdb.php:64
MemcachedBagOStuff\getClient
getClient()
Get the underlying client object.
Definition: MemcachedBagOStuff.php:98
BagOStuff\$keyspace
string $keyspace
Definition: BagOStuff.php:64
MemcachedBagOStuff\validateKeyEncoding
validateKeyEncoding( $key)
Ensure that a key is safe to use (contains no control characters and no characters above the ASCII ra...
Definition: MemcachedBagOStuff.php:156
MemcachedBagOStuff\__construct
__construct(array $params)
$params include:
Definition: MemcachedBagOStuff.php:33
MemcachedBagOStuff\add
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: MemcachedBagOStuff.php:71
MemcachedBagOStuff\$client
MemcachedClient Memcached $client
Definition: MemcachedBagOStuff.php:31