49 parent::__construct( $params );
51 $this->backend = $backend;
53 $this->attrMap = $backend->attrMap;
57 parent::setDebug( $enabled );
58 $this->backend->setDebug( $enabled );
61 public function get( $key, $flags = 0 ) {
62 $value = $this->procCache->get( $key, $flags );
63 if ( $value ===
false && !$this->procCache->hasKey( $key ) ) {
64 $value = $this->backend->get( $key, $flags );
65 $this->
set( $key, $value, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
72 $valuesByKeyCached = [];
75 foreach (
$keys as $key ) {
76 $value = $this->procCache->get( $key, $flags );
77 if ( $value ===
false && !$this->procCache->hasKey( $key ) ) {
78 $keysMissing[] = $key;
80 $valuesByKeyCached[$key] = $value;
84 $valuesByKeyFetched = $this->backend->getMulti( $keysMissing, $flags );
85 $this->
setMulti( $valuesByKeyFetched, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
87 return $valuesByKeyCached + $valuesByKeyFetched;
90 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
91 $this->procCache->set( $key, $value, $exptime, $flags );
93 if ( !$this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
94 $this->backend->set( $key, $value, $exptime, $flags );
100 public function delete( $key, $flags = 0 ) {
101 $this->procCache->delete( $key, $flags );
103 if ( !$this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
104 $this->backend->delete( $key, $flags );
110 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
111 if ( $this->
get( $key ) ===
false ) {
112 return $this->
set( $key, $value, $exptime, $flags );
121 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
122 $this->procCache->delete( $key );
124 return $this->backend->merge( $key, $callback, $exptime, $attempts, $flags );
127 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
128 $this->procCache->delete( $key );
130 return $this->backend->changeTTL( $key, $exptime, $flags );
133 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
134 return $this->backend->lock( $key, $timeout, $expiry, $rclass );
138 return $this->backend->unlock( $key );
143 callable $progress =
null,
146 $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
148 return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
152 return $this->backend->makeKeyInternal( $keyspace,
$args );
155 public function makeKey( $class, ...$components ) {
156 return $this->backend->makeKey( $class, ...$components );
160 return $this->backend->makeGlobalKey( $class, ...$components );
164 return $this->backend->getLastError();
168 return $this->backend->clearLastError();
171 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
172 $this->procCache->setMulti( $data, $exptime, $flags );
174 if ( !$this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
175 return $this->backend->setMulti( $data, $exptime, $flags );
182 $this->procCache->deleteMulti(
$keys, $flags );
184 if ( !$this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
185 return $this->backend->deleteMulti(
$keys, $flags );
192 $this->procCache->changeTTLMulti(
$keys, $exptime, $flags );
194 if ( !$this->
fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
195 return $this->backend->changeTTLMulti(
$keys, $exptime, $flags );
201 public function incr( $key, $value = 1, $flags = 0 ) {
202 $this->procCache->delete( $key );
204 return $this->backend->incr( $key, $value, $flags );
207 public function decr( $key, $value = 1, $flags = 0 ) {
208 $this->procCache->delete( $key );
210 return $this->backend->decr( $key, $value, $flags );
213 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
214 $this->procCache->delete( $key );
216 return $this->backend->incrWithInit( $key, $exptime, $value, $init, $flags );
220 $this->backend->addBusyCallback( $workCallback );
224 return $this->backend->setNewPreparedValues( $valueByKey );
228 parent::setMockTime( $time );
229 $this->procCache->setMockTime( $time );
230 $this->backend->setMockTime( $time );
Class representing a cache/ephemeral data store.
fieldHasFlags( $field, $flags)
Wrapper around a BagOStuff that caches data in memory.
__construct(BagOStuff $backend, $params=[])
Stable to call.
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( $class,... $components)
Make a global cache key.
unlock( $key)
Release an advisory lock on a key string.
deleteMulti(array $keys, $flags=0)
Batch deletion.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
clearLastError()
Clear the "last error" registry.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
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.
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
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)
makeKey( $class,... $components)
Make a cache key, scoped to this instance's keyspace.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Simple store for keeping values in an associative array for the current process.