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