21use Wikimedia\ObjectFactory;
46 private const MAX_WRITE_DELAY = 5;
61 parent::__construct( $params );
63 if ( !isset( $params[
'writeFactory'] ) ) {
64 throw new InvalidArgumentException(
65 __METHOD__ .
': the "writeFactory" parameter is required' );
66 } elseif ( !isset( $params[
'readFactory'] ) ) {
67 throw new InvalidArgumentException(
68 __METHOD__ .
': the "readFactory" parameter is required' );
71 $this->consistencyWindow = $params[
'sessionConsistencyWindow'] ?? self::MAX_WRITE_DELAY;
72 $this->writeStore = ( $params[
'writeFactory'] instanceof
BagOStuff )
73 ? $params[
'writeFactory']
74 : ObjectFactory::getObjectFromSpec( $params[
'writeFactory'] );
75 $this->readStore = ( $params[
'readFactory'] instanceof
BagOStuff )
76 ? $params[
'readFactory']
77 : ObjectFactory::getObjectFromSpec( $params[
'readFactory'] );
78 $this->attrMap = $this->
mergeFlagMaps( [ $this->readStore, $this->writeStore ] );
81 public function get( $key, $flags = 0 ) {
91 return $store->
proxyCall( __FUNCTION__, self::ARG0_KEY, self::RES_NONKEY, func_get_args() );
94 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
97 return $this->writeStore->proxyCall(
105 public function delete( $key, $flags = 0 ) {
108 return $this->writeStore->proxyCall(
116 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
119 return $this->writeStore->proxyCall(
127 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
130 return $this->writeStore->proxyCall(
138 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
141 return $this->writeStore->proxyCall(
149 public function lock( $key, $timeout = 6, $exptime = 6, $rclass =
'' ) {
150 return $this->writeStore->proxyCall(
159 return $this->writeStore->proxyCall(
169 callable $progress =
null,
173 return $this->writeStore->proxyCall(
191 return $store->
proxyCall( __FUNCTION__, self::ARG0_KEYARR, self::RES_KEYMAP, func_get_args() );
194 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
197 return $this->writeStore->proxyCall(
208 return $this->writeStore->proxyCall(
219 return $this->writeStore->proxyCall(
227 public function incr( $key, $value = 1, $flags = 0 ) {
230 return $this->writeStore->proxyCall(
238 public function decr( $key, $value = 1, $flags = 0 ) {
241 return $this->writeStore->proxyCall(
249 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
252 return $this->writeStore->proxyCall(
261 return ( $this->writeStore->getLastError() !== self::ERR_NONE )
262 ? $this->writeStore->getLastError()
263 : $this->readStore->getLastError();
267 $this->writeStore->clearLastError();
268 $this->readStore->clearLastError();
275 public function makeKey( $collection, ...$components ) {
288 return $this->writeStore->addBusyCallback( $workCallback );
292 return $this->writeStore->proxyCall(
301 parent::setMockTime( $time );
302 $this->writeStore->setMockTime( $time );
303 $this->readStore->setMockTime( $time );
312 foreach ( $keys as $key ) {
313 $ts = $this->lastKeyWrites[$key] ?? 0;
314 if ( $ts && ( $now - $ts ) <= $this->consistencyWindow ) {
327 foreach ( $keys as $key ) {
328 unset( $this->lastKeyWrites[$key] );
329 $this->lastKeyWrites[$key] = $now;
332 if ( ( $now - reset( $this->lastKeyWrites ) ) > $this->consistencyWindow ) {
333 $this->lastKeyWrites = array_filter(
334 $this->lastKeyWrites,
335 function ( $timestamp ) use ( $now ) {
336 return ( ( $now - $timestamp ) <= $this->consistencyWindow );
Class representing a cache/ephemeral data store.
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
proxyCall( $method, $arg0Sig, $resSig, array $genericArgs)
Call a method on behalf of wrapper BagOStuff instance that uses "generic" keys.
string $keyspace
Default keyspace; used by makeKey()
fieldHasFlags( $field, $flags)
A cache class that directs writes to one set of servers and reads to another.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF, string $tag=null)
Delete all objects expiring before a certain date.
__construct( $params)
Constructor.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
clearLastError()
Clear the "last error" registry.
lock( $key, $timeout=6, $exptime=6, $rclass='')
Acquire an advisory lock on a key string, exclusive to the caller.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
deleteMulti(array $keys, $flags=0)
Batch deletion.
remarkRecentSessionWrite(array $keys)
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
setNewPreparedValues(array $valueByKey)
Make a "generic" reversible cache key from the given components.
setMulti(array $valueByKey, $exptime=0, $flags=0)
Batch insertion/replace.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
hadRecentSessionWrite(array $keys)
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
int $consistencyWindow
Seconds to read from the master source for a key after writing to it.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
float[] $lastKeyWrites
Map of (key => UNIX timestamp)
unlock( $key)
Release an advisory lock on a key string.
makeKey( $collection,... $components)
Make a cache key for the global keyspace and given components.
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.