29use Psr\Log\LoggerAwareInterface;
30use Psr\Log\LoggerInterface;
31use Psr\Log\NullLogger;
32use Wikimedia\ScopedCallback;
97 $this->
setLogger( $params[
'logger'] ??
new NullLogger() );
98 $this->asyncHandler = $params[
'asyncHandler'] ??
null;
106 $this->logger = $logger;
113 $this->debugMode = $enabled;
130 $value = $this->
get( $key, $flags );
132 if ( $value ===
false ) {
133 $value = $callback( $ttl );
134 if ( $value !==
false && $ttl >= 0 ) {
135 $this->
set( $key, $value, $ttl, $flags );
155 abstract public function get( $key, $flags = 0 );
166 abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
179 abstract public function delete( $key, $flags = 0 );
190 abstract public function add( $key, $value, $exptime = 0, $flags = 0 );
234 abstract public function changeTTL( $key, $exptime = 0, $flags = 0 );
247 abstract public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' );
273 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass =
'' ) {
274 $expiry = min( $expiry ?: INF, self::TTL_DAY );
276 if ( !$this->
lock( $key, $timeout, $expiry, $rclass ) ) {
282 return new ScopedCallback(
function () use ( $key, $lSince, $expiry ) {
285 if ( ( $age + $latency ) >= $expiry ) {
286 $this->logger->warning(
287 "Lock for {key} held too long ({age} sec).",
288 [
'key' => $key,
'age' => $age ]
308 callable $progress =
null,
333 abstract public function setMulti( array $data, $exptime = 0, $flags = 0 );
369 abstract public function incr( $key, $value = 1, $flags = 0 );
378 abstract public function decr( $key, $value = 1, $flags = 0 );
395 abstract public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 );
460 abstract public function makeKey( $class, ...$components );
468 return $this->attrMap[$flag] ?? self::QOS_UNKNOWN;
494 return ( ( $field & $flags ) === $flags );
505 foreach ( $bags as $bag ) {
506 foreach ( $bag->attrMap as $attr => $rank ) {
507 if ( isset( $map[$attr] ) ) {
508 $map[$attr] = min( $map[$attr], $rank );
524 return $this->wallClockOverride ?: microtime(
true );
533 $this->wallClockOverride =& $time;
Class representing a cache/ephemeral data store.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
int[] $attrMap
Map of (ATTR_* class constant => QOS_* class constant)
getWithSetCallback( $key, $ttl, $callback, $flags=0)
Get an item with the given key, regenerating and setting it if not found.
__construct(array $params=[])
Parameters include:
getScopedLock( $key, $timeout=6, $expiry=30, $rclass='')
Get a lightweight exclusive self-unlocking lock.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
unlock( $key)
Release an advisory lock on a key string.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
getSegmentedValueMaxSize()
deleteMulti(array $keys, $flags=0)
Batch deletion.
const WRITE_ALLOW_SEGMENTS
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
makeKey( $class,... $components)
Make a cache key, scoped to this instance's keyspace.
float null $wallClockOverride
const READ_LATEST
Bitfield constants for get()/getMulti(); these are only advisory.
makeGlobalKey( $class,... $components)
Make a global cache key.
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.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
const WRITE_PRUNE_SEGMENTS
setLogger(LoggerInterface $logger)
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
const WRITE_SYNC
Bitfield constants for set()/merge(); these are only advisory.
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
clearLastError()
Clear the "last error" registry.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
fieldHasFlags( $field, $flags)
callable null $asyncHandler
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
Generic interface for lightweight expiring object stores.
Generic interface for object stores with key encoding methods.