MediaWiki  master
CachedBagOStuff.php
Go to the documentation of this file.
1 <?php
37 class CachedBagOStuff extends BagOStuff {
39  protected $store;
41  protected $procCache;
42 
48  public function __construct( BagOStuff $backend, $params = [] ) {
49  $params['keyspace'] = $backend->keyspace;
50  parent::__construct( $params );
51 
52  $this->store = $backend;
53  $this->procCache = new HashBagOStuff( $params );
54  $this->attrMap = $backend->attrMap;
55  }
56 
57  public function get( $key, $flags = 0 ) {
58  $value = $this->procCache->get( $key, $flags );
59  if ( $value !== false || $this->procCache->hasKey( $key ) ) {
60  return $value;
61  }
62 
63  $value = $this->store->proxyCall(
64  __FUNCTION__,
65  self::ARG0_KEY,
66  self::RES_NONKEY,
67  func_get_args()
68  );
69  $this->set( $key, $value, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
70 
71  return $value;
72  }
73 
74  public function getMulti( array $keys, $flags = 0 ) {
75  $valueByKeyCached = [];
76 
77  $keysFetch = [];
78  foreach ( $keys as $key ) {
79  $value = $this->procCache->get( $key, $flags );
80  if ( $value === false && !$this->procCache->hasKey( $key ) ) {
81  $keysFetch[] = $key;
82  } else {
83  $valueByKeyCached[$key] = $value;
84  }
85  }
86 
87  $valueByKeyFetched = $this->store->proxyCall(
88  __FUNCTION__,
89  self::ARG0_KEYARR,
90  self::RES_KEYMAP,
91  [ $keysFetch, $flags ]
92  );
93  $this->setMulti( $valueByKeyFetched, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
94 
95  return $valueByKeyCached + $valueByKeyFetched;
96  }
97 
98  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
99  $this->procCache->set( $key, $value, $exptime, $flags );
100 
101  if ( $this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
102  return true;
103  }
104 
105  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
106  }
107 
108  public function delete( $key, $flags = 0 ) {
109  $this->procCache->delete( $key, $flags );
110 
111  if ( $this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
112  return true;
113  }
114 
115  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
116  }
117 
118  public function add( $key, $value, $exptime = 0, $flags = 0 ) {
119  if ( $this->get( $key ) === false ) {
120  return $this->set( $key, $value, $exptime, $flags );
121  }
122 
123  return false; // key already set
124  }
125 
126  // These just call the backend (tested elsewhere)
127  // @codeCoverageIgnoreStart
128 
129  public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
130  $this->procCache->delete( $key );
131 
132  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
133  }
134 
135  public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
136  $this->procCache->delete( $key );
137 
138  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
139  }
140 
141  public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
142  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
143  }
144 
145  public function unlock( $key ) {
146  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
147  }
148 
150  $timestamp,
151  callable $progress = null,
152  $limit = INF
153  ) {
154  $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
155 
156  return $this->store->proxyCall( __FUNCTION__, self::ARG0_NONKEY, self::RES_NONKEY, func_get_args() );
157  }
158 
159  public function makeKeyInternal( $keyspace, $components ) {
160  return $this->genericKeyFromComponents( $keyspace, ...$components );
161  }
162 
163  public function makeKey( $collection, ...$components ) {
164  return $this->genericKeyFromComponents( $this->keyspace, $collection, ...$components );
165  }
166 
167  public function makeGlobalKey( $collection, ...$components ) {
168  return $this->genericKeyFromComponents( self::GLOBAL_KEYSPACE, $collection, ...$components );
169  }
170 
171  protected function convertGenericKey( $key ) {
172  return $key; // short-circuit; already uses "generic" keys
173  }
174 
175  public function getLastError() {
176  return $this->store->getLastError();
177  }
178 
179  public function clearLastError() {
180  return $this->store->clearLastError();
181  }
182 
183  public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
184  $this->procCache->setMulti( $valueByKey, $exptime, $flags );
185 
186  if ( $this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
187  return true;
188  }
189 
190  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYMAP, self::RES_NONKEY, func_get_args() );
191  }
192 
193  public function deleteMulti( array $keys, $flags = 0 ) {
194  $this->procCache->deleteMulti( $keys, $flags );
195 
196  if ( $this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
197  return true;
198  }
199 
200  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYARR, self::RES_NONKEY, func_get_args() );
201  }
202 
203  public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
204  $this->procCache->changeTTLMulti( $keys, $exptime, $flags );
205 
206  if ( $this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
207  return true;
208  }
209 
210  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYARR, self::RES_NONKEY, func_get_args() );
211  }
212 
213  public function incr( $key, $value = 1, $flags = 0 ) {
214  $this->procCache->delete( $key );
215 
216  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
217  }
218 
219  public function decr( $key, $value = 1, $flags = 0 ) {
220  $this->procCache->delete( $key );
221 
222  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
223  }
224 
225  public function incrWithInit( $key, $exptime, $value = 1, $init = null, $flags = 0 ) {
226  $this->procCache->delete( $key );
227 
228  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
229  }
230 
231  public function addBusyCallback( callable $workCallback ) {
232  $this->store->addBusyCallback( $workCallback );
233  }
234 
235  public function setNewPreparedValues( array $valueByKey ) {
236  return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYMAP, self::RES_NONKEY, func_get_args() );
237  }
238 
239  public function setMockTime( &$time ) {
240  parent::setMockTime( $time );
241  $this->procCache->setMockTime( $time );
242  $this->store->setMockTime( $time );
243  }
244 
245  // @codeCoverageIgnoreEnd
246 }
CachedBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: CachedBagOStuff.php:213
CachedBagOStuff\clearLastError
clearLastError()
Clear the "last error" registry.
Definition: CachedBagOStuff.php:179
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:32
CachedBagOStuff\unlock
unlock( $key)
Release an advisory lock on a key string.
Definition: CachedBagOStuff.php:145
CachedBagOStuff\getLastError
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
Definition: CachedBagOStuff.php:175
CachedBagOStuff\makeGlobalKey
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
Definition: CachedBagOStuff.php:167
CachedBagOStuff\setNewPreparedValues
setNewPreparedValues(array $valueByKey)
Make a "generic" reversible cache key from the given components.
Definition: CachedBagOStuff.php:235
CachedBagOStuff\convertGenericKey
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
Definition: CachedBagOStuff.php:171
CachedBagOStuff\$store
BagOStuff $store
Definition: CachedBagOStuff.php:39
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
CachedBagOStuff\__construct
__construct(BagOStuff $backend, $params=[])
Definition: CachedBagOStuff.php:48
BagOStuff\genericKeyFromComponents
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
Definition: BagOStuff.php:685
CachedBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: CachedBagOStuff.php:219
CachedBagOStuff\changeTTLMulti
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
Definition: CachedBagOStuff.php:203
CachedBagOStuff\getMulti
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
Definition: CachedBagOStuff.php:74
CachedBagOStuff\addBusyCallback
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
Definition: CachedBagOStuff.php:231
BagOStuff\get
get( $key, $flags=0)
Get an item with the given key.
CachedBagOStuff\changeTTL
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
Definition: CachedBagOStuff.php:135
CachedBagOStuff\deleteObjectsExpiringBefore
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
Definition: CachedBagOStuff.php:149
CachedBagOStuff\setMockTime
setMockTime(&$time)
Definition: CachedBagOStuff.php:239
CachedBagOStuff\add
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: CachedBagOStuff.php:118
CachedBagOStuff\incrWithInit
incrWithInit( $key, $exptime, $value=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise.
Definition: CachedBagOStuff.php:225
CachedBagOStuff\setMulti
setMulti(array $valueByKey, $exptime=0, $flags=0)
Batch insertion/replace.
Definition: CachedBagOStuff.php:183
CachedBagOStuff\deleteMulti
deleteMulti(array $keys, $flags=0)
Batch deletion.
Definition: CachedBagOStuff.php:193
CachedBagOStuff
Wrapper around a BagOStuff that caches data in memory.
Definition: CachedBagOStuff.php:37
CachedBagOStuff\$procCache
HashBagOStuff $procCache
Definition: CachedBagOStuff.php:41
CachedBagOStuff\makeKey
makeKey( $collection,... $components)
Make a cache key for the global keyspace and given components.
Definition: CachedBagOStuff.php:163
CachedBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
Definition: CachedBagOStuff.php:159
CachedBagOStuff\merge
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
Definition: CachedBagOStuff.php:129
BagOStuff\fieldHasFlags
fieldHasFlags( $field, $flags)
Definition: BagOStuff.php:593
$keys
$keys
Definition: testCompression.php:72
BagOStuff\$keyspace
string $keyspace
Default keyspace; used by makeKey()
Definition: BagOStuff.php:103
CachedBagOStuff\lock
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
Definition: CachedBagOStuff.php:141
BagOStuff\WRITE_CACHE_ONLY
const WRITE_CACHE_ONLY
Definition: BagOStuff.php:119