49 $params[
'keyspace'] = $backend->keyspace;
50 parent::__construct( $params );
52 $this->store = $backend;
55 $this->attrMap = $backend->attrMap;
58 public function get( $key, $flags = 0 ) {
59 $value = $this->procCache->
get( $key, $flags );
60 if ( $value !==
false || $this->procCache->hasKey( $key ) ) {
64 $value = $this->store->proxyCall(
70 $this->
set( $key, $value, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
76 $valueByKeyCached = [];
79 foreach (
$keys as $key ) {
80 $value = $this->procCache->get( $key, $flags );
81 if ( $value ===
false && !$this->procCache->hasKey( $key ) ) {
84 $valueByKeyCached[$key] = $value;
88 $valueByKeyFetched = $this->store->proxyCall(
92 [ $keysFetch, $flags ]
94 $this->
setMulti( $valueByKeyFetched, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
96 return $valueByKeyCached + $valueByKeyFetched;
99 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
100 $this->procCache->set( $key, $value, $exptime, $flags );
102 if ( $this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
106 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
109 public function delete( $key, $flags = 0 ) {
110 $this->procCache->delete( $key, $flags );
112 if ( $this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
116 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
119 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
120 if ( $this->
get( $key ) ===
false ) {
121 return $this->
set( $key, $value, $exptime, $flags );
130 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
131 $this->procCache->delete( $key );
133 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
136 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
137 $this->procCache->delete( $key );
139 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
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() );
147 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
152 callable $progress =
null,
156 $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag );
158 return $this->store->proxyCall( __FUNCTION__, self::ARG0_NONKEY, self::RES_NONKEY, func_get_args() );
165 public function makeKey( $collection, ...$components ) {
178 return $this->store->getLastError();
182 return $this->store->clearLastError();
185 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
186 $this->procCache->setMulti( $valueByKey, $exptime, $flags );
188 if ( $this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
192 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYMAP, self::RES_NONKEY, func_get_args() );
196 $this->procCache->deleteMulti(
$keys, $flags );
198 if ( $this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
202 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYARR, self::RES_NONKEY, func_get_args() );
206 $this->procCache->changeTTLMulti(
$keys, $exptime, $flags );
208 if ( $this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
212 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYARR, self::RES_NONKEY, func_get_args() );
215 public function incr( $key, $value = 1, $flags = 0 ) {
216 $this->procCache->delete( $key );
218 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
221 public function decr( $key, $value = 1, $flags = 0 ) {
222 $this->procCache->delete( $key );
224 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
227 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
228 $this->procCache->delete( $key );
230 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
234 $this->store->addBusyCallback( $workCallback );
238 return $this->store->proxyCall( __FUNCTION__, self::ARG0_KEYMAP, self::RES_NONKEY, func_get_args() );
242 parent::setMockTime( $time );
243 $this->procCache->setMockTime( $time );
244 $this->store->setMockTime( $time );
Class representing a cache/ephemeral data store.
get( $key, $flags=0)
Get an item with the given key.
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
fieldHasFlags( $field, $flags)
Wrapper around a BagOStuff that caches data in memory.
__construct(BagOStuff $backend, $params=[])
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
unlock( $key)
Release an advisory lock on a key string.
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
deleteMulti(array $keys, $flags=0)
Batch deletion.
makeKey( $collection,... $components)
Make a cache key for the global keyspace and given components.
clearLastError()
Clear the "last error" registry.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF, string $tag=null)
Delete all objects expiring before a certain date.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
setNewPreparedValues(array $valueByKey)
Make a "generic" reversible cache key from the given components.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
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.
lock( $key, $timeout=6, $exptime=6, $rclass='')
Acquire an advisory lock on a key string, exclusive to the caller.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
setMulti(array $valueByKey, $exptime=0, $flags=0)
Batch insertion/replace.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
Simple store for keeping values in an associative array for the current process.