MediaWiki  master
ReplicatedBagOStuff Class Reference

A cache class that directs writes to one set of servers and reads to another. More...

Inheritance diagram for ReplicatedBagOStuff:
Collaboration diagram for ReplicatedBagOStuff:

Public Member Functions

 __construct ( $params)
 Constructor. More...
 
 add ( $key, $value, $exptime=0, $flags=0)
 Insert an item if it does not already exist. More...
 
 addBusyCallback (callable $workCallback)
 Let a callback be run to avoid wasting time on special blocking calls. More...
 
 changeTTL ( $key, $exptime=0, $flags=0)
 Change the expiration on a key if it exists. More...
 
 changeTTLMulti (array $keys, $exptime, $flags=0)
 Change the expiration of multiple keys that exist. More...
 
 clearLastError ()
 Clear the "last error" registry. More...
 
 decr ( $key, $value=1, $flags=0)
 Decrease stored value of $key by $value while preserving its TTL. More...
 
 delete ( $key, $flags=0)
 Delete an item. More...
 
 deleteMulti (array $keys, $flags=0)
 Batch deletion. More...
 
 deleteObjectsExpiringBefore ( $timestamp, callable $progress=null, $limit=INF, string $tag=null)
 Delete all objects expiring before a certain date. More...
 
 get ( $key, $flags=0)
 Get an item with the given key. More...
 
 getLastError ()
 Get the "last error" registered; clearLastError() should be called manually. More...
 
 getMulti (array $keys, $flags=0)
 Get an associative array containing the item for each of the keys that have items. More...
 
 incr ( $key, $value=1, $flags=0)
 Increase stored value of $key by $value while preserving its TTL. More...
 
 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. More...
 
 lock ( $key, $timeout=6, $exptime=6, $rclass='')
 Acquire an advisory lock on a key string, exclusive to the caller. More...
 
 makeGlobalKey ( $collection,... $components)
 Make a cache key for the default keyspace and given components. More...
 
 makeKey ( $collection,... $components)
 Make a cache key for the global keyspace and given components. More...
 
 makeKeyInternal ( $keyspace, $components)
 Make a cache key for the given keyspace and components. More...
 
 merge ( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
 Merge changes into the existing cache value (possibly creating a new one) More...
 
 set ( $key, $value, $exptime=0, $flags=0)
 Set an item. More...
 
 setMockTime (&$time)
 
 setMulti (array $valueByKey, $exptime=0, $flags=0)
 Batch insertion/replace. More...
 
 setNewPreparedValues (array $valueByKey)
 Make a "generic" reversible cache key from the given components. More...
 
 unlock ( $key)
 Release an advisory lock on a key string. More...
 
- Public Member Functions inherited from BagOStuff
 __construct (array $params=[])
 Parameters include: More...
 
 getCurrentTime ()
 
 getLogger ()
 
 getQoS ( $flag)
 
 getScopedLock ( $key, $timeout=6, $expiry=30, $rclass='')
 Get a lightweight exclusive self-unlocking lock. More...
 
 getSegmentationSize ()
 
 getSegmentedValueMaxSize ()
 
 getWithSetCallback ( $key, $exptime, $callback, $flags=0)
 Get an item with the given key, regenerating and setting it if not found. More...
 
 isKeyGlobal ( $key)
 Check whether a cache key is in the global keyspace. More...
 
 registerWrapperInfoForStats (string $prefixComponent, string $statsGroup, callable $collectionCallback)
 Register info about a caching layer class that uses BagOStuff as a backing store. More...
 
 setLogger (LoggerInterface $logger)
 

Protected Member Functions

 convertGenericKey ( $key)
 Convert a "generic" reversible cache key into one for this cache. More...
 
- Protected Member Functions inherited from BagOStuff
 componentsFromGenericKey ( $key)
 Extract the components from a "generic" reversible cache key. More...
 
 determineKeyPrefixForStats ( $key)
 
 fieldHasFlags ( $field, $flags)
 
 genericKeyFromComponents (... $components)
 At a minimum, there must be a keyspace and collection name component. More...
 
 mergeFlagMaps (array $bags)
 Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map. More...
 
 proxyCall ( $method, $arg0Sig, $resSig, array $genericArgs)
 Call a method on behalf of wrapper BagOStuff instance that uses "generic" keys. More...
 

Private Member Functions

 hadRecentSessionWrite (array $keys)
 
 remarkRecentSessionWrite (array $keys)
 

Private Attributes

int $consistencyWindow
 Seconds to read from the master source for a key after writing to it. More...
 
float[] $lastKeyWrites = []
 Map of (key => UNIX timestamp) More...
 
BagOStuff $readStore
 
BagOStuff $writeStore
 

Additional Inherited Members

- Public Attributes inherited from BagOStuff
const READ_LATEST = 1
 Bitfield constants for get()/getMulti(); these are only advisory. More...
 
const READ_VERIFIED = 2
 
const WRITE_ALLOW_SEGMENTS = 16
 
const WRITE_BACKGROUND = 64
 
const WRITE_CACHE_ONLY = 8
 
const WRITE_PRUNE_SEGMENTS = 32
 
const WRITE_SYNC = 4
 Bitfield constants for set()/merge(); these are only advisory. More...
 
- Public Attributes inherited from Wikimedia\LightweightObjectStore\StorageAwareness
const ATTR_DURABILITY = 2
 Durability of writes; see QOS_DURABILITY_* (higher means stronger) More...
 
const ATTR_EMULATION = 1
 Emulation/fallback mode; see QOS_EMULATION_*; higher is better. More...
 
const ERR_NO_RESPONSE = 1
 Storage medium failed to yield a response. More...
 
const ERR_NONE = 0
 No storage medium error. More...
 
const ERR_UNEXPECTED = 3
 Storage medium operation failed due to usage limitations or an I/O error. More...
 
const ERR_UNREACHABLE = 2
 Storage medium could not be reached. More...
 
const QOS_DURABILITY_DISK = 4
 Data is saved to disk and writes do not usually block on fsync() More...
 
const QOS_DURABILITY_NONE = 1
 Data is never saved to begin with (blackhole store) More...
 
const QOS_DURABILITY_RDBMS = 5
 Data is saved to disk and writes usually block on fsync(), like a standard RDBMS. More...
 
const QOS_DURABILITY_SCRIPT = 2
 Data is lost at the end of the current web request or CLI script. More...
 
const QOS_DURABILITY_SERVICE = 3
 Data is lost once the service storing the data restarts. More...
 
const QOS_EMULATION_SQL = 1
 Fallback disk-based SQL store. More...
 
const QOS_UNKNOWN = INF
 Generic "unknown" value; useful for comparisons (always "good enough") More...
 
- Protected Attributes inherited from BagOStuff
callable null $asyncHandler
 
int[] $attrMap = []
 Map of (ATTR_* class constant => QOS_* class constant) More...
 
string $keyspace
 Default keyspace; used by makeKey() More...
 
LoggerInterface $logger
 
StatsdDataFactoryInterface $stats
 
array< string, array > $wrapperInfoByPrefix = []
 Cache key processing callbacks and info for metrics. More...
 

Detailed Description

A cache class that directs writes to one set of servers and reads to another.

This assumes that the servers used for reads are setup to replica DB those that writes go to. This can easily be used with redis for example.

In the WAN scenario (e.g. multi-datacenter case), this is useful when writes are rare or they usually take place in the primary datacenter.

Since
1.26

Definition at line 34 of file ReplicatedBagOStuff.php.

Constructor & Destructor Documentation

◆ __construct()

ReplicatedBagOStuff::__construct (   $params)

Constructor.

Parameters are:

  • writeFactory: ObjectFactory::getObjectFromSpec array yielding BagOStuff. This object will be used for writes (e.g. the primary DB).
  • readFactory: ObjectFactory::getObjectFromSpec array yielding BagOStuff. This object will be used for reads (e.g. a replica DB).
  • sessionConsistencyWindow: Seconds to read from the master source for a key after writing to it. [Default: ReplicatedBagOStuff::MAX_WRITE_DELAY]
Parameters
array$params
Exceptions
InvalidArgumentException

Definition at line 60 of file ReplicatedBagOStuff.php.

References BagOStuff\mergeFlagMaps().

Member Function Documentation

◆ add()

ReplicatedBagOStuff::add (   $key,
  $value,
  $exptime = 0,
  $flags = 0 
)

Insert an item if it does not already exist.

Parameters
string$key
mixed$value
int$exptime
int$flagsBitfield of BagOStuff::WRITE_* constants (since 1.33)
Returns
bool Success

Reimplemented from BagOStuff.

Definition at line 116 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ addBusyCallback()

ReplicatedBagOStuff::addBusyCallback ( callable  $workCallback)

Let a callback be run to avoid wasting time on special blocking calls.

The callbacks may or may not be called ever, in any particular order. They are likely to be invoked when something WRITE_SYNC is used used. They should follow a caching pattern as shown below, so that any code using the work will get it's result no matter what happens.

$result = null;
$workCallback = function () use ( &$result ) {
if ( !$result ) {
$result = ....
}
return $result;
}
Parameters
callable$workCallback
Since
1.28

Reimplemented from BagOStuff.

Definition at line 287 of file ReplicatedBagOStuff.php.

◆ changeTTL()

ReplicatedBagOStuff::changeTTL (   $key,
  $exptime = 0,
  $flags = 0 
)

Change the expiration on a key if it exists.

If an expiry in the past is given then the key will immediately be expired

For large values written using WRITE_ALLOW_SEGMENTS, this only changes the TTL of the main segment list key. While lowering the TTL of the segment list key has the effect of functionally lowering the TTL of the key, it might leave unused blobs in cache for longer. Raising the TTL of such keys is not effective, since the expiration of a single segment key effectively expires the entire value.

Parameters
string$key
int$exptimeTTL or UNIX timestamp
int$flagsBitfield of BagOStuff::WRITE_* constants (since 1.33)
Returns
bool Success Returns false on failure or if the item does not exist
Since
1.28

Reimplemented from BagOStuff.

Definition at line 138 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ changeTTLMulti()

ReplicatedBagOStuff::changeTTLMulti ( array  $keys,
  $exptime,
  $flags = 0 
)

Change the expiration of multiple keys that exist.

See also
BagOStuff::changeTTL()
Parameters
string[]$keysList of keys
int$exptimeTTL or UNIX timestamp
int$flagsBitfield of BagOStuff::WRITE_* constants (since 1.33)
Returns
bool Success
Since
1.34

Reimplemented from BagOStuff.

Definition at line 216 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ clearLastError()

ReplicatedBagOStuff::clearLastError ( )

Clear the "last error" registry.

Since
1.23

Reimplemented from BagOStuff.

Definition at line 266 of file ReplicatedBagOStuff.php.

◆ convertGenericKey()

ReplicatedBagOStuff::convertGenericKey (   $key)
protected

Convert a "generic" reversible cache key into one for this cache.

See also
BagOStuff::genericKeyFromComponents()
Parameters
string$keyKeyspace-prepended list of encoded components as a colon-separated value
Returns
string Keyspace-prepended list of encoded components as a colon-separated value

Reimplemented from BagOStuff.

Definition at line 283 of file ReplicatedBagOStuff.php.

◆ decr()

ReplicatedBagOStuff::decr (   $key,
  $value = 1,
  $flags = 0 
)

Decrease stored value of $key by $value while preserving its TTL.

Parameters
string$key
int$valueValue to subtract from $key (default: 1) [optional]
int$flagsBit field of class WRITE_* constants [optional]
Returns
int|bool New value or false on failure

Reimplemented from BagOStuff.

Definition at line 238 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ delete()

ReplicatedBagOStuff::delete (   $key,
  $flags = 0 
)

Delete an item.

For large values written using WRITE_ALLOW_SEGMENTS, this only deletes the main segment list key unless WRITE_PRUNE_SEGMENTS is in the flags. While deleting the segment list key has the effect of functionally deleting the key, it leaves unused blobs in cache.

Parameters
string$key
Returns
bool True if the item was deleted or not found, false on failure
Parameters
int$flagsBitfield of BagOStuff::WRITE_* constants

Reimplemented from BagOStuff.

Definition at line 105 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ deleteMulti()

ReplicatedBagOStuff::deleteMulti ( array  $keys,
  $flags = 0 
)

Batch deletion.

This does not support WRITE_ALLOW_SEGMENTS to avoid excessive read I/O

WRITE_BACKGROUND can be used for bulk deletion where the response is not vital

Parameters
string[]$keysList of keys
int$flagsBitfield of BagOStuff::WRITE_* constants
Returns
bool Success
Since
1.33

Reimplemented from BagOStuff.

Definition at line 205 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ deleteObjectsExpiringBefore()

ReplicatedBagOStuff::deleteObjectsExpiringBefore (   $timestamp,
callable  $progress = null,
  $limit = INF,
string  $tag = null 
)

Delete all objects expiring before a certain date.

Parameters
string | int$timestampThe reference date in MW or TS_UNIX format
callable | null$progressOptional, a function which will be called regularly during long-running operations with the percentage progress as the first parameter. [optional]
int$limitMaximum number of keys to delete [default: INF]
string | null$tagTag to purge a single shard only. This is only supported when server tags are used in configuration.
Returns
bool Success; false if unimplemented

Reimplemented from BagOStuff.

Definition at line 167 of file ReplicatedBagOStuff.php.

◆ get()

ReplicatedBagOStuff::get (   $key,
  $flags = 0 
)

Get an item with the given key.

If the key includes a deterministic input hash (e.g. the key can only have the correct value) or complete staleness checks are handled by the caller (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set. This lets tiered backends know they can safely upgrade a cached value to higher tiers using standard TTLs.

Parameters
string$key
int$flagsBitfield of BagOStuff::READ_* constants [optional]
Returns
mixed Returns false on failure or if the item does not exist

Reimplemented from BagOStuff.

Definition at line 81 of file ReplicatedBagOStuff.php.

References BagOStuff\fieldHasFlags(), and hadRecentSessionWrite().

◆ getLastError()

ReplicatedBagOStuff::getLastError ( )

Get the "last error" registered; clearLastError() should be called manually.

Returns
int ERR_* constant for the "last error" registry
Since
1.23

Reimplemented from BagOStuff.

Definition at line 260 of file ReplicatedBagOStuff.php.

◆ getMulti()

ReplicatedBagOStuff::getMulti ( array  $keys,
  $flags = 0 
)

Get an associative array containing the item for each of the keys that have items.

Parameters
string[]$keysList of keys
int$flagsBitfield; supports READ_LATEST [optional]
Returns
mixed[] Map of (key => value) for existing keys

Reimplemented from BagOStuff.

Definition at line 181 of file ReplicatedBagOStuff.php.

References BagOStuff\fieldHasFlags(), and hadRecentSessionWrite().

◆ hadRecentSessionWrite()

ReplicatedBagOStuff::hadRecentSessionWrite ( array  $keys)
private
Parameters
string[]$keys
Returns
bool

Definition at line 310 of file ReplicatedBagOStuff.php.

References BagOStuff\getCurrentTime().

Referenced by get(), and getMulti().

◆ incr()

ReplicatedBagOStuff::incr (   $key,
  $value = 1,
  $flags = 0 
)

Increase stored value of $key by $value while preserving its TTL.

Parameters
string$keyKey to increase
int$valueValue to add to $key (default: 1) [optional]
int$flagsBit field of class WRITE_* constants [optional]
Returns
int|bool New value or false on failure

Reimplemented from BagOStuff.

Definition at line 227 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ incrWithInit()

ReplicatedBagOStuff::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.

This will create the key with the value $init and TTL $exptime instead if not present. Callers should make sure that both ($init - $value) and $exptime are invariants for all operations to any given key. The value of $init should be at least that of $value.

Parameters
string$keyKey built via makeKey() or makeGlobalKey()
int$exptimeTime-to-live (in seconds) or a UNIX timestamp expiration
int$valueAmount to increase the key value by [default: 1]
int | null$initValue to initialize the key to if it does not exist [default: $value]
int$flagsBit field of class WRITE_* constants [optional]
Returns
int|bool New value or false on failure
Since
1.24

Reimplemented from BagOStuff.

Definition at line 249 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ lock()

ReplicatedBagOStuff::lock (   $key,
  $timeout = 6,
  $exptime = 6,
  $rclass = '' 
)

Acquire an advisory lock on a key string, exclusive to the caller.

Parameters
string$key
int$timeoutLock wait timeout; 0 for non-blocking [optional]
int$exptimeLock time-to-live in seconds; 1 day maximum [optional]
string$rclassIf this thread already holds the lock, and the lock was acquired using the same value for this parameter, then return true and use reference counting so that only the unlock() call from the outermost lock() caller actually releases the lock (note that only the outermost time-to-live is used) [optional]
Returns
bool Success

Reimplemented from BagOStuff.

Definition at line 149 of file ReplicatedBagOStuff.php.

◆ makeGlobalKey()

ReplicatedBagOStuff::makeGlobalKey (   $collection,
  $components 
)

Make a cache key for the default keyspace and given components.

See also
IStoreKeyEncoder::makeGlobalKey()
Parameters
string$collectionKey collection name component
string|int...$components Additional, ordered, key components for entity IDs
Returns
string Colon-separated, keyspace-prepended, ordered list of encoded components
Since
1.27

Reimplemented from BagOStuff.

Definition at line 279 of file ReplicatedBagOStuff.php.

References BagOStuff\genericKeyFromComponents().

◆ makeKey()

ReplicatedBagOStuff::makeKey (   $collection,
  $components 
)

Make a cache key for the global keyspace and given components.

See also
IStoreKeyEncoder::makeKey()
Parameters
string$collectionKey collection name component
string|int...$components Additional, ordered, key components for entity IDs
Returns
string Colon-separated, keyspace-prepended, ordered list of encoded components
Since
1.27

Reimplemented from BagOStuff.

Definition at line 275 of file ReplicatedBagOStuff.php.

References BagOStuff\genericKeyFromComponents().

◆ makeKeyInternal()

ReplicatedBagOStuff::makeKeyInternal (   $keyspace,
  $components 
)

Make a cache key for the given keyspace and components.

Long components might be converted to respective hashes due to size constraints. In extreme cases, all of them might be combined into a single hash component.

Access: internal
This method should not be used outside of BagOStuff (since 1.36)
Parameters
string$keyspaceKeyspace component
string[] | int[]$componentsKey components (key collection name first)
Returns
string Keyspace-prepended list of encoded components as a colon-separated value
Since
1.27

Reimplemented from BagOStuff.

Definition at line 271 of file ReplicatedBagOStuff.php.

References BagOStuff\$keyspace, and BagOStuff\genericKeyFromComponents().

◆ merge()

ReplicatedBagOStuff::merge (   $key,
callable  $callback,
  $exptime = 0,
  $attempts = 10,
  $flags = 0 
)

Merge changes into the existing cache value (possibly creating a new one)

The callback function returns the new value given the current value (which will be false if not present), and takes the arguments: (this BagOStuff, cache key, current value, TTL). The TTL parameter is reference set to $exptime. It can be overriden in the callback. Nothing is stored nor deleted if the callback returns false.

Parameters
string$key
callable$callbackCallback method to be executed
int$exptimeEither an interval in seconds or a unix timestamp for expiry
int$attemptsThe amount of times to attempt a merge in case of failure
int$flagsBitfield of BagOStuff::WRITE_* constants
Returns
bool Success
Exceptions
InvalidArgumentException

Reimplemented from BagOStuff.

Definition at line 127 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ remarkRecentSessionWrite()

ReplicatedBagOStuff::remarkRecentSessionWrite ( array  $keys)
private
Parameters
string[]$keys

Definition at line 325 of file ReplicatedBagOStuff.php.

References BagOStuff\getCurrentTime().

Referenced by add(), changeTTL(), changeTTLMulti(), decr(), delete(), deleteMulti(), incr(), incrWithInit(), merge(), set(), and setMulti().

◆ set()

ReplicatedBagOStuff::set (   $key,
  $value,
  $exptime = 0,
  $flags = 0 
)

Set an item.

Parameters
string$key
mixed$value
int$exptimeEither an interval in seconds or a unix timestamp for expiry
int$flagsBitfield of BagOStuff::WRITE_* constants
Returns
bool Success

Reimplemented from BagOStuff.

Definition at line 94 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ setMockTime()

ReplicatedBagOStuff::setMockTime ( $time)
Access: internal
For testing only
Parameters
float | null&$timeMock UNIX timestamp

Reimplemented from BagOStuff.

Definition at line 300 of file ReplicatedBagOStuff.php.

◆ setMulti()

ReplicatedBagOStuff::setMulti ( array  $valueByKey,
  $exptime = 0,
  $flags = 0 
)

Batch insertion/replace.

This does not support WRITE_ALLOW_SEGMENTS to avoid excessive read I/O

WRITE_BACKGROUND can be used for bulk insertion where the response is not vital

Parameters
mixed[]$valueByKeyMap of (key => value)
int$exptimeEither an interval in seconds or a unix timestamp for expiry
int$flagsBitfield of BagOStuff::WRITE_* constants (since 1.33)
Returns
bool Success
Since
1.24

Reimplemented from BagOStuff.

Definition at line 194 of file ReplicatedBagOStuff.php.

References remarkRecentSessionWrite().

◆ setNewPreparedValues()

ReplicatedBagOStuff::setNewPreparedValues ( array  $valueByKey)

Make a "generic" reversible cache key from the given components.

All previously prepared values will be cleared. Each of the new prepared values will be individually cleared as they get used by write operations for that key. This is done to avoid unchecked growth in PHP memory usage.

Example usage:

$valueByKey = [ $key1 => $value1, $key2 => $value2, $key3 => $value3 ];
$cache->setNewPreparedValues( $valueByKey );
$cache->set( $key1, $value1, $cache::TTL_HOUR );
$cache->setMulti( [ $key2 => $value2, $key3 => $value3 ], $cache::TTL_HOUR );

This is only useful if the caller needs an estimate of the serialized object sizes. The caller cannot know the serialization format and even if it did, it could be expensive to serialize complex values twice just to get the size information before writing them to cache. This method solves both problems by making the cache instance do the serialization and having it reuse the result when the cache write happens.

Parameters
array$valueByKeyMap of (cache key => PHP variable value to serialize)
Returns
int[]|null[] Corresponding list of size estimates (null for invalid values)
Since
1.35

Reimplemented from BagOStuff.

Definition at line 291 of file ReplicatedBagOStuff.php.

◆ unlock()

ReplicatedBagOStuff::unlock (   $key)

Release an advisory lock on a key string.

Parameters
string$key
Returns
bool Success

Reimplemented from BagOStuff.

Definition at line 158 of file ReplicatedBagOStuff.php.

Member Data Documentation

◆ $consistencyWindow

int ReplicatedBagOStuff::$consistencyWindow
private

Seconds to read from the master source for a key after writing to it.

Definition at line 41 of file ReplicatedBagOStuff.php.

◆ $lastKeyWrites

float [] ReplicatedBagOStuff::$lastKeyWrites = []
private

Map of (key => UNIX timestamp)

Definition at line 43 of file ReplicatedBagOStuff.php.

◆ $readStore

BagOStuff ReplicatedBagOStuff::$readStore
private

Definition at line 38 of file ReplicatedBagOStuff.php.

◆ $writeStore

BagOStuff ReplicatedBagOStuff::$writeStore
private

Definition at line 36 of file ReplicatedBagOStuff.php.


The documentation for this class was generated from the following file:
$cache
$cache
Definition: mcc.php:33