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 ] );
82 parent::setDebug( $enabled );
83 $this->writeStore->setDebug( $enabled );
84 $this->readStore->setDebug( $enabled );
87 public function get( $key, $flags = 0 ) {
92 ? $this->writeStore->get( $key, $flags )
93 : $this->readStore->get( $key, $flags );
96 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
99 return $this->writeStore->set( $key, $value, $exptime, $flags );
102 public function delete( $key, $flags = 0 ) {
105 return $this->writeStore->delete( $key, $flags );
108 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
111 return $this->writeStore->add( $key, $value, $exptime, $flags );
114 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
117 return $this->writeStore->merge( $key, $callback, $exptime, $attempts, $flags );
120 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
123 return $this->writeStore->changeTTL( $key, $exptime, $flags );
126 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
127 return $this->writeStore->lock( $key, $timeout, $expiry, $rclass );
131 return $this->writeStore->unlock( $key );
136 callable $progress =
null,
139 return $this->writeStore->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
147 ? $this->writeStore->getMulti(
$keys, $flags )
148 : $this->readStore->getMulti(
$keys, $flags );
151 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
154 return $this->writeStore->setMulti( $data, $exptime, $flags );
160 return $this->writeStore->deleteMulti(
$keys, $flags );
166 return $this->writeStore->changeTTLMulti(
$keys, $exptime, $flags );
169 public function incr( $key, $value = 1, $flags = 0 ) {
172 return $this->writeStore->incr( $key, $value, $flags );
175 public function decr( $key, $value = 1, $flags = 0 ) {
178 return $this->writeStore->decr( $key, $value, $flags );
181 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
184 return $this->writeStore->incrWithInit( $key, $exptime, $value, $init, $flags );
188 return ( $this->writeStore->getLastError() !== self::ERR_NONE )
189 ? $this->writeStore->getLastError()
190 : $this->readStore->getLastError();
194 $this->writeStore->clearLastError();
195 $this->readStore->clearLastError();
199 return $this->writeStore->makeKeyInternal( ...func_get_args() );
202 public function makeKey( $class, ...$components ) {
203 return $this->writeStore->makeKey( ...func_get_args() );
207 return $this->writeStore->makeGlobalKey( ...func_get_args() );
211 $this->writeStore->addBusyCallback( $workCallback );
215 return $this->writeStore->setNewPreparedValues( $valueByKey );
219 parent::setMockTime( $time );
220 $this->writeStore->setMockTime( $time );
221 $this->readStore->setMockTime( $time );
230 foreach ( $keys as $key ) {
231 $ts = $this->lastKeyWrites[$key] ?? 0;
232 if ( $ts && ( $now - $ts ) <= $this->consistencyWindow ) {
245 foreach ( $keys as $key ) {
246 unset( $this->lastKeyWrites[$key] );
247 $this->lastKeyWrites[$key] = $now;
250 if ( ( $now - reset( $this->lastKeyWrites ) ) > $this->consistencyWindow ) {
251 $this->lastKeyWrites = array_filter(
252 $this->lastKeyWrites,
253 function ( $timestamp ) use ( $now ) {
254 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.
fieldHasFlags( $field, $flags)
A cache class that directs writes to one set of servers and reads to another.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
__construct( $params)
Constructor.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
clearLastError()
Clear the "last error" registry.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
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)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
hadRecentSessionWrite(array $keys)
makeGlobalKey( $class,... $components)
Make a global cache key.
int $consistencyWindow
Seconds to read from the master source for a key after writing to it.
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
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.
float[] $lastKeyWrites
Map of (key => UNIX timestamp)
unlock( $key)
Release an advisory lock on a key string.
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.