MediaWiki  master
MemcachedPhpBagOStuff.php
Go to the documentation of this file.
1 <?php
31  protected $client;
32 
43  public function __construct( $params ) {
44  parent::__construct( $params );
45 
46  // Default class-specific parameters
47  $params += [
48  'compress_threshold' => 1500,
49  'connect_timeout' => 0.5,
50  'timeout' => 500000,
51  ];
52 
53  $this->client = new MemcachedClient( $params );
54  $this->client->set_servers( $params['servers'] );
55  $this->client->set_debug( true );
56  }
57 
58  protected function doGet( $key, $flags = 0, &$casToken = null ) {
59  $getToken = ( $casToken === self::PASS_BY_REF );
60  $casToken = null;
61 
62  $routeKey = $this->validateKeyAndPrependRoute( $key );
63 
64  // T257003: only require "gets" (instead of "get") when a CAS token is needed
65  return $getToken
66  // @phan-suppress-next-line PhanTypeMismatchArgument False positive
67  ? $this->client->get( $routeKey, $casToken )
68  : $this->client->get( $routeKey );
69  }
70 
71  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
72  $routeKey = $this->validateKeyAndPrependRoute( $key );
73 
74  return $this->client->set( $routeKey, $value, $this->fixExpiry( $exptime ) );
75  }
76 
77  protected function doDelete( $key, $flags = 0 ) {
78  $routeKey = $this->validateKeyAndPrependRoute( $key );
79 
80  return $this->client->delete( $routeKey );
81  }
82 
83  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
84  $routeKey = $this->validateKeyAndPrependRoute( $key );
85 
86  return $this->client->add( $routeKey, $value, $this->fixExpiry( $exptime ) );
87  }
88 
89  protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
90  $routeKey = $this->validateKeyAndPrependRoute( $key );
91 
92  return $this->client->cas( $casToken, $routeKey, $value, $this->fixExpiry( $exptime ) );
93  }
94 
95  public function incr( $key, $value = 1, $flags = 0 ) {
96  $routeKey = $this->validateKeyAndPrependRoute( $key );
97  $n = $this->client->incr( $routeKey, $value );
98 
99  return ( $n !== false && $n !== null ) ? $n : false;
100  }
101 
102  public function decr( $key, $value = 1, $flags = 0 ) {
103  $routeKey = $this->validateKeyAndPrependRoute( $key );
104  $n = $this->client->decr( $routeKey, $value );
105 
106  return ( $n !== false && $n !== null ) ? $n : false;
107  }
108 
109  protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
110  $routeKey = $this->validateKeyAndPrependRoute( $key );
111 
112  $watchPoint = $this->watchErrors();
113  $newValue = $this->client->incr( $routeKey, $step ) ?? false;
114  if ( $newValue === false && !$this->getLastError( $watchPoint ) ) {
115  // No key set; initialize
116  $success = $this->client->add( $routeKey, $init, $this->fixExpiry( $exptime ) );
117  $newValue = $success ? $init : false;
118  if ( $newValue === false && !$this->getLastError( $watchPoint ) ) {
119  // Raced out initializing; increment
120  $newValue = $this->client->incr( $routeKey, $step ) ?? false;
121  }
122  }
123 
124  return $newValue;
125  }
126 
127  protected function doChangeTTL( $key, $exptime, $flags ) {
128  $routeKey = $this->validateKeyAndPrependRoute( $key );
129 
130  return $this->client->touch( $routeKey, $this->fixExpiry( $exptime ) );
131  }
132 
133  protected function doGetMulti( array $keys, $flags = 0 ) {
134  $routeKeys = [];
135  foreach ( $keys as $key ) {
136  $routeKeys[] = $this->validateKeyAndPrependRoute( $key );
137  }
138 
139  $resByRouteKey = $this->client->get_multi( $routeKeys );
140 
141  $res = [];
142  foreach ( $resByRouteKey as $routeKey => $value ) {
143  $res[$this->stripRouteFromKey( $routeKey )] = $value;
144  }
145 
146  return $res;
147  }
148 
149  protected function serialize( $value ) {
150  return is_int( $value ) ? $value : $this->client->serialize( $value );
151  }
152 
153  protected function unserialize( $value ) {
154  return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
155  }
156 }
$success
getLastError( $watchPoint=0)
Get the "last error" registry.
Definition: BagOStuff.php:495
watchErrors()
Get a "watch point" token that can be used to get the "last error" to occur after now.
Definition: BagOStuff.php:473
isInteger( $value)
Check if a value is an integer.
Base class for memcached clients.
memcached client class implemented using (p)fsockopen()
A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doChangeTTL( $key, $exptime, $flags)
__construct( $params)
Available parameters are:
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
doGet( $key, $flags=0, &$casToken=null)
doIncrWithInit( $key, $exptime, $step, $init, $flags)
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
doDelete( $key, $flags=0)
Delete an item.