MediaWiki REL1_33
|
Multi-datacenter aware caching interface. More...
Public Member Functions | |
__construct (array $params) | |
adaptiveTTL ( $mtime, $maxTTL, $minTTL=30, $factor=0.2) | |
Get a TTL that is higher for objects that have not changed recently. | |
clearLastError () | |
Clear the "last error" registry. | |
clearProcessCache () | |
Clear the in-process caches; useful for testing. | |
delete ( $key, $ttl=self::HOLDOFF_TTL) | |
Purge a key from all datacenters. | |
get ( $key, &$curTTL=null, array $checkKeys=[], &$info=null) | |
Fetch the value of a key from cache. | |
getCheckKeyTime ( $key) | |
Fetch the value of a timestamp "check" key. | |
getLastError () | |
Get the "last error" registered; clearLastError() should be called manually. | |
getMulti (array $keys, &$curTTLs=[], array $checkKeys=[], &$info=null) | |
Fetch the value of several keys from cache. | |
getMultiCheckKeyTime (array $keys) | |
Fetch the values of each timestamp "check" key. | |
getMultiWithSetCallback (ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[]) | |
Method to fetch multiple cache keys at once with regeneration. | |
getMultiWithUnionSetCallback (ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[]) | |
Method to fetch/regenerate multiple cache keys at once. | |
getQoS ( $flag) | |
getWarmupKeyMisses () | |
getWithSetCallback ( $key, $ttl, $callback, array $opts=[]) | |
Method to fetch/regenerate cache keys. | |
makeGlobalKey ( $class, $component=null) | |
makeKey ( $class, $component=null) | |
makeMultiKeys (array $entities, callable $keyFunc) | |
reap ( $key, $purgeTimestamp, &$isStale=false) | |
Set a key to soon expire in the local cluster if it pre-dates $purgeTimestamp. | |
reapCheckKey ( $key, $purgeTimestamp, &$isStale=false) | |
Set a "check" key to soon expire in the local cluster if it pre-dates $purgeTimestamp. | |
resetCheckKey ( $key) | |
Delete a "check" key from all datacenters, invalidating keys that use it. | |
set ( $key, $value, $ttl=self::TTL_INDEFINITE, array $opts=[]) | |
Set the value of a key in cache. | |
setLogger (LoggerInterface $logger) | |
setMockTime (&$time) | |
touchCheckKey ( $key, $holdoff=self::HOLDOFF_TTL) | |
Purge a "check" key from all datacenters, invalidating keys that use it. | |
useInterimHoldOffCaching ( $enabled) | |
Enable or disable the use of brief caching for tombstoned keys. | |
Static Public Member Functions | |
static | newEmpty () |
Get an instance that wraps EmptyBagOStuff. | |
Public Attributes | |
const | AGE_NEW = 60 |
Never consider performing "popularity" refreshes until a key reaches this age. | |
const | CHECK_KEY_TTL = self::TTL_YEAR |
Seconds to keep dependency purge keys around. | |
const | COOLOFF_KEY_PREFIX = 'WANCache:c:' |
const | COOLOFF_TTL = 1 |
Seconds to no-op key set() calls to avoid large blob I/O stampedes. | |
const | FLD_FLAGS = 4 |
const | FLD_HOLDOFF = 5 |
const | FLD_TIME = 3 |
const | FLD_TTL = 2 |
const | FLD_VALUE = 1 |
const | FLD_VERSION = 0 |
const | GRACE_TTL_NONE = 0 |
Idiom for set()/getWithSetCallback() for "no post-expired grace period". | |
const | HIT_RATE_HIGH = 1 |
Hits/second for a refresh to be expected within the "popularity" window. | |
const | HOLDOFF_NONE = 0 |
Idiom for delete() for "no hold-off". | |
const | HOLDOFF_TTL = 11 |
Seconds to tombstone keys on delete() | |
const | HOT_TTR = 900 |
The time length of the "popularity" refresh window for hot keys. | |
const | INTERIM_KEY_PREFIX = 'WANCache:i:' |
const | INTERIM_KEY_TTL = 1 |
Seconds to keep interim value keys for tombstoned keys around. | |
const | LOCK_TTL = 10 |
Seconds to keep lock keys around. | |
const | LOW_TTL = 30 |
Default remaining TTL at which to consider pre-emptive regeneration. | |
const | MAX_COMMIT_DELAY = 3 |
Max time expected to pass between delete() and DB commit finishing. | |
const | MAX_READ_LAG = 7 |
Max replication+snapshot lag before applying TTL_LAGGED or disallowing set() | |
const | MIN_TIMESTAMP_NONE = 0.0 |
Idiom for getWithSetCallback() for "no minimum required as-of timestamp". | |
const | MUTEX_KEY_PREFIX = 'WANCache:m:' |
const | PASS_BY_REF = -1 |
Parameter to get()/getMulti() to return extra information by reference. | |
const | PC_PRIMARY = 'primary:1000' |
const | PURGE_VAL_PREFIX = 'PURGED:' |
const | RAMPUP_TTL = 30 |
Seconds to ramp up to the "popularity" refresh chance after a key is no longer new. | |
const | RECENT_SET_HIGH_MS = 100 |
Max millisecond set() backoff for keys in hold-off (far less than INTERIM_KEY_TTL) | |
const | RECENT_SET_LOW_MS = 50 |
Min millisecond set() backoff for keys in hold-off (far less than INTERIM_KEY_TTL) | |
const | SET_DELAY_HIGH_MS = 50 |
Milliseconds of delay after get() where set() storms are a consideration with 'lockTSE'. | |
const | STALE_TTL_NONE = 0 |
Idiom for set()/getWithSetCallback() for "do not augment the storage medium TTL". | |
const | TIME_KEY_PREFIX = 'WANCache:t:' |
const | TINY_NEGATIVE = -0.000001 |
Tiny negative float to use when CTL comes up >= 0 due to clock skew. | |
const | TINY_POSTIVE = 0.000001 |
Tiny positive float to use when using "minTime" to assert an inequality. | |
const | TSE_NONE = -1 |
Idiom for getWithSetCallback() callbacks to 'lockTSE' logic. | |
const | TTL_LAGGED = 30 |
Max TTL to store keys when a data sourced is lagged. | |
const | TTL_UNCACHEABLE = -1 |
Idiom for getWithSetCallback() callbacks to avoid calling set() | |
const | VALUE_KEY_PREFIX = 'WANCache:v:' |
const | VERSION = 1 |
Cache format version number. | |
const | VFLD_DATA = 'WOC:d' |
const | VFLD_VERSION = 'WOC:v' |
Public Attributes inherited from IExpiringStore | |
const | ATTR_EMULATION = 1 |
const | ATTR_SYNCWRITES = 2 |
const | ERR_NO_RESPONSE = 1 |
const | ERR_NONE = 0 |
const | ERR_UNEXPECTED = 3 |
const | ERR_UNREACHABLE = 2 |
const | QOS_EMULATION_SQL = 1 |
const | QOS_SYNCWRITES_BE = 2 |
const | QOS_SYNCWRITES_NONE = 1 |
const | QOS_SYNCWRITES_QC = 3 |
const | QOS_SYNCWRITES_SS = 4 |
const | QOS_UNKNOWN = INF |
const | TTL_DAY = 86400 |
const | TTL_HOUR = 3600 |
const | TTL_INDEFINITE = 0 |
const | TTL_MINUTE = 60 |
const | TTL_MONTH = 2592000 |
const | TTL_PROC_LONG = 30 |
const | TTL_PROC_SHORT = 3 |
const | TTL_SECOND = 1 |
const | TTL_WEEK = 604800 |
const | TTL_YEAR = 31536000 |
Protected Member Functions | |
determineKeyClassForStats ( $key) | |
doGetWithSetCallback ( $key, $ttl, $callback, array $opts, &$asOf=null) | |
Do the actual I/O for getWithSetCallback() when needed. | |
getCurrentTime () | |
getInterimValue ( $key, $versioned, $minTime) | |
getProcessCache ( $group) | |
isAliveOrInGracePeriod ( $curTTL, $graceTTL) | |
Check if a key is fresh or in the grace window and thus due for randomized reuse. | |
isValid ( $value, $versioned, $asOf, $minTime, $purgeTime=null) | |
Check if $value is not false, versioned (if needed), and not older than $minTime (if set) | |
makePurgeValue ( $timestamp, $holdoff) | |
parsePurgeValue ( $value) | |
relayDelete ( $key) | |
Do the actual async bus delete of a key. | |
relayPurge ( $key, $ttl, $holdoff) | |
Do the actual async bus purge of a key. | |
resolveCTL ( $value, $curTTL, $curInfo, $touchedCallback) | |
resolveTouched ( $value, $lastPurge, $touchedCallback) | |
setInterimValue ( $key, $value, $tempTTL, $newAsOf) | |
unwrap ( $wrapped, $now) | |
Do not use this method outside WANObjectCache. | |
worthRefreshExpiring ( $curTTL, $lowTTL) | |
Check if a key is nearing expiration and thus due for randomized regeneration. | |
worthRefreshPopular ( $asOf, $ageNew, $timeTillRefresh, $now) | |
Check if a key is due for randomized regeneration due to its popularity. | |
wrap ( $value, $ttl, $now) | |
Do not use this method outside WANObjectCache. | |
Static Protected Member Functions | |
static | prefixCacheKeys (array $keys, $prefix) |
Protected Attributes | |
callable null | $asyncHandler |
Function that takes a WAN cache callback and runs it later. | |
BagOStuff | $cache |
The local datacenter cache. | |
string | $cluster |
Cache cluster name for mcrouter use. | |
float | $epoch |
Unix timestamp of the oldest possible valid values. | |
LoggerInterface | $logger |
$mcrouterAware | |
@bar bool Whether to use mcrouter key prefixing for routing | |
MapCacheLRU[] | $processCaches = [] |
Map of group PHP instance caches. | |
string | $region |
Physical region for mcrouter use. | |
StatsdDataFactoryInterface | $stats |
bool | $useInterimHoldOffCaching = true |
Whether to use "interim" caching while keys are tombstoned. | |
Private Member Functions | |
checkAndSetCooloff ( $key, $kClass, $elapsed, $lockTSE, $hasLock) | |
getNonProcessCachedKeys (array $keys, array $opts, $pcTTL) | |
getRawKeysForWarmup (array $keys, array $checkKeys) | |
isVolatileValueAgeNegligible ( $age) | |
processCheckKeys (array $timeKeys, array $wrappedValues, $now) | |
scheduleAsyncRefresh ( $key, $ttl, $callback, $opts) | |
Private Attributes | |
int | $callbackDepth = 0 |
Callback stack depth for getWithSetCallback() | |
float null | $wallClockOverride |
mixed[] | $warmupCache = [] |
Temporary warm-up cache. | |
int | $warmupKeyMisses = 0 |
Key fetched. | |
Multi-datacenter aware caching interface.
All operations go to the local datacenter cache, except for delete(), touchCheckKey(), and resetCheckKey(), which broadcast to all datacenters.
This class is intended for caching data from primary stores. If the get() method does not return a value, then the caller should query the new value and backfill the cache using set(). The preferred way to do this logic is through getWithSetCallback(). When querying the store on cache miss, the closest DB replica should be used. Try to avoid heavyweight DB master or quorum reads.
To ensure consumers of the cache see new values in a timely manner, you either need to follow either the validation strategy, or the purge strategy.
The validation strategy refers to the natural avoidance of stale data by one of the following means:
The purge strategy refers to the the approach whereby your application knows that source data has changed and can react by purging the relevant cache keys. As purges are expensive, this strategy should be avoided if possible. The simplest purge method is delete().
No matter which strategy you choose, callers must not rely on updates or purges being immediately visible to other servers. It should be treated similarly as one would a database replica.
The need for immediate updates should be avoided. If needed, solutions must be sought outside WANObjectCache.
There are two supported ways to set up broadcasted operations:
true
. Configure mcrouter as follows:Broadcasted operations like delete() and touchCheckKey() are intended to run immediately in the local datacenter and asynchronously in remote datacenters.
This means that callers in all datacenters may see older values for however many milliseconds that the purge took to reach that datacenter. As with any cache, this should not be relied on for cases where reads are used to determine writes to source (e.g. non-cache) data stores, except when reading immutable data.
All values are wrapped in metadata arrays. Keys use a "WANCache:" prefix to avoid collisions with keys that are not wrapped as metadata arrays. The prefixes are as follows:
Definition at line 116 of file WANObjectCache.php.
WANObjectCache::__construct | ( | array | $params | ) |
array | $params |
|
Definition at line 254 of file WANObjectCache.php.
References $params, cache, and setLogger().
WANObjectCache::adaptiveTTL | ( | $mtime, | |
$maxTTL, | |||
$minTTL = 30 , |
|||
$factor = 0.2 |
|||
) |
Get a TTL that is higher for objects that have not changed recently.
This is useful for keys that get explicit purges and DB or purge relay lag is a potential concern (especially how it interacts with CDN cache)
Example usage:
Another use case is when there are no applicable "last modified" fields in the DB, and there are too many dependencies for explicit purges to be viable, and the rate of change to relevant content is unstable, and it is highly valued to have the cached value be as up-to-date as possible.
Example usage:
int | float | $mtime | UNIX timestamp |
int | $maxTTL | Maximum TTL (seconds) |
int | $minTTL | Minimum TTL (seconds); Default: 30 |
float | $factor | Value in the range (0,1); Default: .2 |
Definition at line 2033 of file WANObjectCache.php.
References getCurrentTime().
|
private |
string | $key | |
string | $kClass | |
float | $elapsed | Seconds spent regenerating the value |
float | $lockTSE | |
$hasLock | bool |
Definition at line 1470 of file WANObjectCache.php.
References cache, and IExpiringStore\ERR_NONE.
Referenced by doGetWithSetCallback().
|
final |
Clear the "last error" registry.
Definition at line 1924 of file WANObjectCache.php.
References cache.
WANObjectCache::clearProcessCache | ( | ) |
Clear the in-process caches; useful for testing.
Definition at line 1933 of file WANObjectCache.php.
|
final |
Purge a key from all datacenters.
This should only be called when the underlying data (being cached) changes in a significant way. This deletes the key and starts a hold-off period where the key cannot be written to for a few seconds (HOLDOFF_TTL). This is done to avoid the following race condition:
This is implemented by storing a special "tombstone" value at the cache key that this class recognizes; get() calls will return false for the key and any set() calls will refuse to replace tombstone values at the key. For this to always avoid stale value writes, the following must hold:
Note that set() can also be lag-aware and lower the TTL if it's high.
Be aware that this does not clear the process cache. Even if it did, callbacks used by getWithSetCallback() might still return stale data in the case of either uncommitted or not-yet-replicated changes (callback generally use replica DBs).
When using potentially long-running ACID transactions, a good pattern is to use a pre-commit hook to issue the delete. This means that immediately after commit, callers will see the tombstone in cache upon purge relay. It also avoids the following race condition:
Example usage:
The $ttl parameter can be used when purging values that have not actually changed recently. For example, a cleanup script to purge cache entries does not really need a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge. Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback().
If called twice on the same key, then the last hold-off TTL takes precedence. For idempotence, the $ttl should not vary for different delete() calls on the same key.
string | $key | Cache key |
int | $ttl | Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL |
Definition at line 703 of file WANObjectCache.php.
References determineKeyClassForStats(), relayDelete(), and relayPurge().
Referenced by WANObjectCacheTest\testBusyValue(), WANObjectCacheTest\testGetMultiWithSetCallback(), WANObjectCacheTest\testGetMultiWithUnionSetCallback(), WANObjectCacheTest\testGetWithSetCallback(), WANObjectCacheTest\testGetWithSetCallback_versions(), WANObjectCacheTest\testInterimHoldOffCaching(), and WANObjectCacheTest\testLockTSE().
|
protected |
string | $key | String of the format <scope>:<class>[:<class or variable>]... |
Definition at line 2327 of file WANObjectCache.php.
Referenced by delete(), doGetWithSetCallback(), resetCheckKey(), and touchCheckKey().
|
protected |
Do the actual I/O for getWithSetCallback() when needed.
string | $key | |
int | $ttl | |
callable | $callback | |
array | $opts | Options map for getWithSetCallback() |
float | null | &$asOf | Cache generation timestamp of returned value [returned] |
Definition at line 1300 of file WANObjectCache.php.
References $value, cache, checkAndSetCooloff(), determineKeyClassForStats(), false, getCurrentTime(), getInterimValue(), isAliveOrInGracePeriod(), isValid(), isVolatileValueAgeNegligible(), list, null, resolveCTL(), resolveTouched(), scheduleAsyncRefresh(), setInterimValue(), worthRefreshExpiring(), and worthRefreshPopular().
Referenced by getWithSetCallback(), and scheduleAsyncRefresh().
Fetch the value of a key from cache.
If supplied, $curTTL is set to the remaining TTL (current time left):
If a key is tombstoned, $curTTL will reflect the time since delete().
The timestamp of $key will be checked against the last-purge timestamp of each of $checkKeys. Those $checkKeys not in cache will have the last-purge initialized to the current timestamp. If any of $checkKeys have a timestamp greater than that of $key, then $curTTL will reflect how long ago $key became invalid. Callers can use $curTTL to know when the value is stale. The $checkKeys parameter allow mass invalidations by updating a single key:
Source data entities might exists in a DB that uses snapshot isolation (e.g. the default REPEATABLE-READ in innoDB). Even for mutable data, that isolation can largely be maintained by doing the following:
However, pre-snapshot values might still be seen if an update was made in a remote datacenter but the purge from delete() didn't relay yet.
Consider using getWithSetCallback() instead of get() and set() cycles. That method has cache slam avoiding features for hot/expensive keys.
Pass $info as WANObjectCache::PASS_BY_REF to transform it into a cache key info map. This map includes the following metadata:
Otherwise, $info will transform into the cached value timestamp.
string | $key | Cache key made from makeKey() or makeGlobalKey() |
mixed | null | &$curTTL | Approximate TTL left on the key if present/tombstoned [returned] |
array | $checkKeys | List of "check" keys |
mixed | null | &$info | Key info if WANObjectCache::PASS_BY_REF [returned] |
Definition at line 331 of file WANObjectCache.php.
References getMulti(), and null.
Referenced by JobQueueDB\doDeduplicateRootJob(), WANObjectCacheTest\testCheckKeyInitHoldoff(), WANObjectCacheTest\testEpoch(), WANObjectCacheTest\testGetMultiWithSetCallback(), WANObjectCacheTest\testGetMultiWithUnionSetCallback(), WANObjectCacheTest\testGetWithSetCallback(), and WANObjectCacheTest\testLockTSESlow().
|
final |
Fetch the value of a timestamp "check" key.
The key will be initialized to the current time if not set, so only call this method if this behavior is actually desired
The timestamp can be used to check whether a cached value is valid. Callers should not assume that this returns the same timestamp in all datacenters due to relay delays.
The level of staleness can roughly be estimated from this key, but if the key was evicted from cache, such calculations may show the time since expiry as ~0 seconds.
Note that "check" keys won't collide with other regular keys.
string | $key |
Definition at line 737 of file WANObjectCache.php.
References getMultiCheckKeyTime().
Referenced by WANObjectCacheTest\testEpoch(), WANObjectCacheTest\testGetMulti(), WANObjectCacheTest\testGetMultiWithSetCallback(), WANObjectCacheTest\testGetMultiWithUnionSetCallback(), WANObjectCacheTest\testGetWithSetCallback(), and WANObjectCacheTest\testTouchKeys().
|
protected |
Definition at line 2448 of file WANObjectCache.php.
Referenced by adaptiveTTL(), doGetWithSetCallback(), getInterimValue(), getMulti(), getMultiCheckKeyTime(), relayPurge(), and set().
|
protected |
string | $key | |
bool | $versioned | |
float | $minTime |
Definition at line 1547 of file WANObjectCache.php.
References $value, cache, getCurrentTime(), isValid(), list, null, unwrap(), and useInterimHoldOffCaching().
Referenced by doGetWithSetCallback().
|
final |
Get the "last error" registered; clearLastError() should be called manually.
Definition at line 1907 of file WANObjectCache.php.
References $code, cache, IExpiringStore\ERR_NO_RESPONSE, IExpiringStore\ERR_NONE, and IExpiringStore\ERR_UNREACHABLE.
|
final |
Fetch the value of several keys from cache.
Pass $info as WANObjectCache::PASS_BY_REF to transform it into a map of cache keys to cache key info maps, each having the same style as those of WANObjectCache::get(). All the cache keys listed in $keys will have an entry.
Othwerwise, $info will transform into a map of (cache key => cached value timestamp). Only the cache keys listed in $keys that exists or are tombstoned will have an entry.
array | $keys | List of cache keys made from makeKey() or makeGlobalKey() |
mixed | null | &$curTTLs | Map of (key => TTL left) for existing/tombstoned keys [returned] |
array | $checkKeys | List of check keys to apply to all $keys. May also apply "check" keys to specific cache keys only by using cache keys as keys in the $checkKeys array. |
mixed | null | &$info | Map of (key => info) if WANObjectCache::PASS_BY_REF [returned] |
Definition at line 370 of file WANObjectCache.php.
References $keys, $value, array(), as, cache, getCurrentTime(), list, null, processCheckKeys(), and unwrap().
Referenced by get(), getMultiWithUnionSetCallback(), WANObjectCacheTest\testGetMulti(), and WANObjectCacheTest\testGetMultiCheckKeys().
|
final |
Fetch the values of each timestamp "check" key.
This works like getCheckKeyTime() except it takes a list of keys and returns a map of timestamps instead of just that of one key
This might be useful if both:
The later entities can each use a "check" key to invalidate their dependee entities. However, it is expensive for the former entities to verify against all of the relevant "check" keys during each getWithSetCallback() call. A less expensive approach is to do these verifications only after a "time-till-verify" (TTV) has passed. This is a middle ground between using blind TTLs and using constant verification. The adaptiveTTL() method can be used to dynamically adjust the TTV. Also, the initial TTV can make use of the last-modified times of the dependant entities (either from the DB or the "check" keys).
Example usage:
array | $keys |
Definition at line 802 of file WANObjectCache.php.
References $keys, $time, as, cache, getCurrentTime(), makePurgeValue(), parsePurgeValue(), and string.
Referenced by getCheckKeyTime().
|
final |
Method to fetch multiple cache keys at once with regeneration.
This works the same as getWithSetCallback() except:
Example usage:
ArrayIterator | $keyedIds | Result of WANObjectCache::makeMultiKeys() |
int | $ttl | Seconds to live for key updates |
callable | $callback | Callback the yields entity regeneration callbacks |
array | $opts | Options map |
Definition at line 1647 of file WANObjectCache.php.
References as, getNonProcessCachedKeys(), getRawKeysForWarmup(), getWithSetCallback(), and use.
Referenced by WANObjectCacheTest\testGetMultiWithSetCallback().
|
final |
Method to fetch/regenerate multiple cache keys at once.
This works the same as getWithSetCallback() except:
Example usage:
ArrayIterator | $keyedIds | Result of WANObjectCache::makeMultiKeys() |
int | $ttl | Seconds to live for key updates |
callable | $callback | Callback the yields entity regeneration callbacks |
array | $opts | Options map |
Definition at line 1742 of file WANObjectCache.php.
References as, getMulti(), getNonProcessCachedKeys(), getRawKeysForWarmup(), getWithSetCallback(), and use.
Referenced by WANObjectCacheTest\testGetMultiWithUnionSetCallback().
array | $keys | |
array | $opts | |
int | $pcTTL |
Definition at line 2394 of file WANObjectCache.php.
References $keys, as, and getProcessCache().
Referenced by getMultiWithSetCallback(), and getMultiWithUnionSetCallback().
|
protected |
string | $group |
Definition at line 2379 of file WANObjectCache.php.
References list.
Referenced by getNonProcessCachedKeys(), and getWithSetCallback().
WANObjectCache::getQoS | ( | $flag | ) |
int | $flag | ATTR_* class constant |
Definition at line 1966 of file WANObjectCache.php.
References cache.
Referenced by MWLBFactory\applyDefaultCaching().
array | $keys | |
array | $checkKeys |
Definition at line 2414 of file WANObjectCache.php.
References $keys, $warmupCache, as, and cache.
Referenced by getMultiWithSetCallback(), and getMultiWithUnionSetCallback().
|
final |
Definition at line 2051 of file WANObjectCache.php.
Referenced by WANObjectCacheTest\testGetMultiWithSetCallback(), and WANObjectCacheTest\testGetMultiWithUnionSetCallback().
|
final |
Method to fetch/regenerate cache keys.
On cache miss, the key will be set to the callback result via set() (unless the callback returns false) and that result will be returned. The arguments supplied to the callback are:
It is strongly recommended to set the 'lag' and 'since' fields to avoid race conditions that can cause stale values to get stuck at keys. Usually, callbacks ignore the current value, but it can be used to maintain "most recent X" values that come from time or sequence based source data, provided that the "as of" id/time is tracked. Note that preemptive regeneration and $checkKeys can result in a non-false current value.
Usage of $checkKeys is similar to get() and getMulti(). However, rather than the caller having to inspect a "current time left" variable (e.g. $curTTL, $curTTLs), a cache regeneration will automatically be triggered using the callback.
The $ttl argument and "hotTTR" option (in $opts) use time-dependant randomization to avoid stampedes. Keys that are slow to regenerate and either heavily used or subject to explicit (unpredictable) purges, may need additional mechanisms. The simplest way to avoid stampedes for such keys is to use 'lockTSE' (in $opts). If explicit purges are needed, also:
Example usage (typical key):
Example usage (key that is expensive and hot):
Example usage (key with dynamic dependencies):
Example usage (key that is expensive with too many DB dependencies for "check keys"):
Example usage (hot key holding most recent 100 events):
Example usage (key holding an LRU subkey:value map; this can avoid flooding cache with keys for an unlimited set of (constraint,situation) pairs, thereby avoiding elevated cache evictions and wasted memory):
string | $key | Cache key made from makeKey() or makeGlobalKey() |
int | $ttl | Seconds to live for key updates. Special values are:
|
callable | $callback | Value generation function |
array | $opts | Options map:
|
Definition at line 1215 of file WANObjectCache.php.
References $value, doGetWithSetCallback(), getProcessCache(), makeGlobalKey(), and use.
Referenced by getMultiWithSetCallback(), getMultiWithUnionSetCallback(), WANObjectCacheTest\testBusyValue(), WANObjectCacheTest\testGetWithSetCallback(), WANObjectCacheTest\testGetWithSetcallback_touched(), WANObjectCacheTest\testGetWithSetCallback_versions(), WANObjectCacheTest\testInterimHoldOffCaching(), WANObjectCacheTest\testLockTSE(), WANObjectCacheTest\testLockTSESlow(), and WANObjectCacheTest\testPreemtiveRefresh().
|
protected |
Check if a key is fresh or in the grace window and thus due for randomized reuse.
If $curTTL > 0 (e.g. not expired) this returns true. Otherwise, the chance of returning true decrease steadily from 100% to 0% as the |$curTTL| moves from 0 to $graceTTL seconds. This handles widely varying levels of cache access traffic.
If $curTTL <= -$graceTTL (e.g. already expired), then this returns false.
float | $curTTL | Approximate TTL left on the key if present |
int | $graceTTL | Consider using stale values if $curTTL is greater than this |
Definition at line 2140 of file WANObjectCache.php.
References worthRefreshExpiring().
Referenced by doGetWithSetCallback().
|
protected |
Check if $value is not false, versioned (if needed), and not older than $minTime (if set)
array | bool | $value | |
bool | $versioned | |
float | $asOf | The time $value was generated |
float | $minTime | The last time the main value was generated (0.0 if unknown) |
float | null | $purgeTime | The last time the value was invalidated |
Definition at line 2235 of file WANObjectCache.php.
References $value.
Referenced by doGetWithSetCallback(), and getInterimValue().
|
private |
float | $age | Age of volatile/interim key in seconds |
Definition at line 1458 of file WANObjectCache.php.
Referenced by doGetWithSetCallback().
WANObjectCache::makeGlobalKey | ( | $class, | |
$component = null |
|||
) |
string | $class | Key class |
string | null | $component | [optional] Key component (starting with a key collection name) |
Definition at line 1884 of file WANObjectCache.php.
References cache.
Referenced by getWithSetCallback(), and WANObjectCacheTest\testEpoch().
WANObjectCache::makeKey | ( | $class, | |
$component = null |
|||
) |
string | $class | Key class |
string | null | $component | [optional] Key component (starting with a key collection name) |
Definition at line 1873 of file WANObjectCache.php.
References cache.
Referenced by WANObjectCacheTest\testGetWithSetCallback().
|
final |
array | $entities | List of entity IDs |
callable | $keyFunc | Callback yielding a key from (entity ID, this WANObjectCache) |
Definition at line 1894 of file WANObjectCache.php.
References as.
Referenced by WANObjectCacheTest\testGetMultiWithSetCallback(), and WANObjectCacheTest\testGetMultiWithUnionSetCallback().
|
protected |
float | $timestamp | |
int | $holdoff | In seconds |
Definition at line 2371 of file WANObjectCache.php.
Referenced by getMultiCheckKeyTime(), processCheckKeys(), and relayPurge().
|
static |
Get an instance that wraps EmptyBagOStuff.
Definition at line 278 of file WANObjectCache.php.
|
protected |
string | array | bool | $value | Possible string of the form "PURGED:<timestamp>:<holdoff>" |
Definition at line 2338 of file WANObjectCache.php.
References $value.
Referenced by getMultiCheckKeyTime(), processCheckKeys(), reapCheckKey(), and unwrap().
|
staticprotected |
array | $keys | |
string | $prefix |
Definition at line 2314 of file WANObjectCache.php.
array | $timeKeys | List of prefixed time check keys |
array | $wrappedValues | |
float | $now |
Definition at line 469 of file WANObjectCache.php.
References as, cache, makePurgeValue(), and parsePurgeValue().
Referenced by getMulti().
|
final |
Set a key to soon expire in the local cluster if it pre-dates $purgeTimestamp.
This sets stale keys' time-to-live at HOLDOFF_TTL seconds, which both avoids broadcasting in mcrouter setups and also avoids races with new tombstones.
string | $key | Cache key |
int | $purgeTimestamp | UNIX timestamp of purge |
bool | &$isStale | Whether the key is stale |
Definition at line 1819 of file WANObjectCache.php.
References cache.
|
final |
Set a "check" key to soon expire in the local cluster if it pre-dates $purgeTimestamp.
string | $key | Cache key |
int | $purgeTimestamp | UNIX timestamp of purge |
bool | &$isStale | Whether the key is stale |
Definition at line 1848 of file WANObjectCache.php.
References cache, and parsePurgeValue().
|
protected |
Do the actual async bus delete of a key.
string | $key | Cache key |
Definition at line 2092 of file WANObjectCache.php.
References cache.
Referenced by delete(), and resetCheckKey().
|
protected |
Do the actual async bus purge of a key.
This must set the key to "PURGED:<UNIX timestamp>:<holdoff>"
string | $key | Cache key |
int | $ttl | How long to keep the tombstone [seconds] |
int | $holdoff | HOLDOFF_* constant controlling how long to ignore sets for this key |
Definition at line 2065 of file WANObjectCache.php.
References cache, getCurrentTime(), and makePurgeValue().
Referenced by delete(), and touchCheckKey().
|
final |
Delete a "check" key from all datacenters, invalidating keys that use it.
This is similar to touchCheckKey() in that keys using it via get(), getMulti(), or getWithSetCallback() will be invalidated. The differences are:
The advantage here is that the "check" keys, which have high TTLs, will only be created when a get*() method actually uses that key. This is better when a large number of "check" keys are invalided in a short period of time.
Note that "check" keys won't collide with other regular keys.
string | $key | Cache key |
Definition at line 904 of file WANObjectCache.php.
References determineKeyClassForStats(), and relayDelete().
Referenced by WANObjectCacheTest\testTouchKeys().
|
protected |
mixed | $value | |
float | null | $curTTL | |
array | $curInfo | |
callable | null | $touchedCallback |
Definition at line 1505 of file WANObjectCache.php.
References $value.
Referenced by doGetWithSetCallback().
|
protected |
mixed | $value | |
float | null | $lastPurge | |
callable | null | $touchedCallback |
Definition at line 1529 of file WANObjectCache.php.
References $value.
Referenced by doGetWithSetCallback().
|
private |
string | $key | |
int | $ttl | |
callable | $callback | |
array | $opts |
Definition at line 2112 of file WANObjectCache.php.
References doGetWithSetCallback(), and use.
Referenced by doGetWithSetCallback().
|
final |
Set the value of a key in cache.
Simply calling this method when source data changes is not valid because the changes do not replicate to the other WAN sites. In that case, delete() should be used instead. This method is intended for use on cache misses.
If the data was read from a snapshot-isolated transactions (e.g. the default REPEATABLE-READ in innoDB), use 'since' to avoid the following race condition:
Setting 'lag' and 'since' help avoids keys getting stuck in stale states.
Be aware that this does not update the process cache for getWithSetCallback() callers. Keys accessed via that method are not generally meant to also be set using this primitive method.
Do not use this method on versioned keys accessed via getWithSetCallback().
Example usage:
string | $key | Cache key |
mixed | $value | |
int | $ttl | Seconds to live. Special values are:
|
array | $opts | Options map:
|
Definition at line 554 of file WANObjectCache.php.
References $cache, $value, cache, false, getCurrentTime(), use, and wrap().
Referenced by JobQueueDB\doDeduplicateRootJob(), MessageBlobStore\recacheMessageBlob(), WANObjectCacheTest\testCheckKeyInitHoldoff(), WANObjectCacheTest\testEpoch(), WANObjectCacheTest\testGetMulti(), and WANObjectCacheTest\testGetMultiCheckKeys().
|
protected |
string | $key | |
mixed | $value | |
int | $tempTTL | |
float | $newAsOf |
Definition at line 1568 of file WANObjectCache.php.
References $value, cache, use, and wrap().
Referenced by doGetWithSetCallback().
WANObjectCache::setLogger | ( | LoggerInterface | $logger | ) |
LoggerInterface | $logger |
Definition at line 269 of file WANObjectCache.php.
Referenced by __construct().
WANObjectCache::setMockTime | ( | & | $time | ) |
float | null | &$time | Mock UNIX timestamp for testing |
Definition at line 2466 of file WANObjectCache.php.
Referenced by WANObjectCacheTest\testBusyValue(), WANObjectCacheTest\testEpoch(), WANObjectCacheTest\testGetMulti(), WANObjectCacheTest\testGetMultiCheckKeys(), WANObjectCacheTest\testGetMultiWithSetCallback(), WANObjectCacheTest\testGetMultiWithUnionSetCallback(), WANObjectCacheTest\testGetWithSetCallback(), WANObjectCacheTest\testGetWithSetcallback_touched(), WANObjectCacheTest\testInterimHoldOffCaching(), WANObjectCacheTest\testLockTSE(), WANObjectCacheTest\testLockTSESlow(), WANObjectCacheTest\testPreemtiveRefresh(), and WANObjectCacheTest\testTouchKeys().
|
final |
Purge a "check" key from all datacenters, invalidating keys that use it.
This should only be called when the underlying data (being cached) changes in a significant way, and it is impractical to call delete() on all keys that should be changed. When get() is called on those keys, the relevant "check" keys must be supplied for this to work.
The "check" key essentially represents a last-modified time of an entity. When the key is touched, the timestamp will be updated to the current time. Keys using the "check" key via get(), getMulti(), or getWithSetCallback() will be invalidated. This approach is useful if many keys depend on a single entity.
The timestamp of the "check" key is treated as being HOLDOFF_TTL seconds in the future by get*() methods in order to avoid race conditions where keys are updated with stale values (e.g. from a lagged replica DB). A high TTL is set on the "check" key, making it possible to know the timestamp of the last change to the corresponding entities in most cases. This might use more cache space than resetCheckKey().
When a few important keys get a large number of hits, a high cache time is usually desired as well as "lockTSE" logic. The resetCheckKey() method is less appropriate in such cases since the "time since expiry" cannot be inferred, causing any get() after the reset to treat the key as being "hot", resulting in more stale value usage.
Note that "check" keys won't collide with other regular keys.
string | $key | Cache key |
int | $holdoff | HOLDOFF_TTL or HOLDOFF_NONE constant |
Definition at line 867 of file WANObjectCache.php.
References determineKeyClassForStats(), and relayPurge().
Referenced by MessageBlobStore\clear(), WANObjectCacheTest\testEpoch(), WANObjectCacheTest\testGetMultiCheckKeys(), WANObjectCacheTest\testGetWithSetCallback(), and WANObjectCacheTest\testTouchKeys().
|
protected |
Do not use this method outside WANObjectCache.
The cached value will be false if absent/tombstoned/malformed
array | string | bool | $wrapped | |
float | $now | Unix Current timestamp (preferrably pre-query) |
Definition at line 2276 of file WANObjectCache.php.
References null, and parsePurgeValue().
Referenced by getInterimValue(), and getMulti().
|
final |
Enable or disable the use of brief caching for tombstoned keys.
When a key is purged via delete(), there normally is a period where caching is hold-off limited to an extremely short time. This method will disable that caching, forcing the callback to run for any of:
This is useful when both:
bool | $enabled | Whether to enable interim caching |
Definition at line 1957 of file WANObjectCache.php.
References useInterimHoldOffCaching().
Referenced by getInterimValue(), WANObjectCacheTest\testInterimHoldOffCaching(), and useInterimHoldOffCaching().
|
protected |
Check if a key is nearing expiration and thus due for randomized regeneration.
This returns false if $curTTL >= $lowTTL. Otherwise, the chance of returning true increases steadily from 0% to 100% as the $curTTL moves from $lowTTL to 0 seconds. This handles widely varying levels of cache access traffic.
If $curTTL <= 0 (e.g. already expired), then this returns false.
float | $curTTL | Approximate TTL left on the key if present |
float | $lowTTL | Consider a refresh when $curTTL is less than this |
Reimplemented in NearExpiringWANObjectCache.
Definition at line 2170 of file WANObjectCache.php.
Referenced by doGetWithSetCallback(), and isAliveOrInGracePeriod().
|
protected |
Check if a key is due for randomized regeneration due to its popularity.
This is used so that popular keys can preemptively refresh themselves for higher consistency (especially in the case of purge loss/delay). Unpopular keys can remain in cache with their high nominal TTL. This means popular keys keep good consistency, whether the data changes frequently or not, and long-tail keys get to stay in cache and get hits too. Similar to worthRefreshExpiring(), randomization is used.
float | $asOf | UNIX timestamp of the value |
int | $ageNew | Age of key when this might recommend refreshing (seconds) |
int | $timeTillRefresh | Age of key when it should be refreshed if popular (seconds) |
float | $now | The current UNIX timestamp |
Reimplemented in PopularityRefreshingWANObjectCache.
Definition at line 2199 of file WANObjectCache.php.
Referenced by doGetWithSetCallback().
|
protected |
Do not use this method outside WANObjectCache.
mixed | $value | |
int | $ttl | [0=forever] |
float | $now | Unix Current timestamp just before calling set() |
Definition at line 2258 of file WANObjectCache.php.
References $value.
Referenced by set(), and setInterimValue().
|
protected |
Function that takes a WAN cache callback and runs it later.
Definition at line 134 of file WANObjectCache.php.
|
protected |
The local datacenter cache.
Definition at line 118 of file WANObjectCache.php.
|
private |
Callback stack depth for getWithSetCallback()
Definition at line 139 of file WANObjectCache.php.
|
protected |
Cache cluster name for mcrouter use.
Definition at line 126 of file WANObjectCache.php.
|
protected |
Unix timestamp of the oldest possible valid values.
Definition at line 136 of file WANObjectCache.php.
|
protected |
Definition at line 128 of file WANObjectCache.php.
|
protected |
@bar bool Whether to use mcrouter key prefixing for routing
Definition at line 122 of file WANObjectCache.php.
|
protected |
Map of group PHP instance caches.
Definition at line 120 of file WANObjectCache.php.
|
protected |
Physical region for mcrouter use.
Definition at line 124 of file WANObjectCache.php.
|
protected |
Definition at line 130 of file WANObjectCache.php.
|
protected |
Whether to use "interim" caching while keys are tombstoned.
Definition at line 132 of file WANObjectCache.php.
|
private |
Definition at line 146 of file WANObjectCache.php.
|
private |
Temporary warm-up cache.
Definition at line 141 of file WANObjectCache.php.
Referenced by getRawKeysForWarmup().
|
private |
Key fetched.
Definition at line 143 of file WANObjectCache.php.
const WANObjectCache::AGE_NEW = 60 |
Never consider performing "popularity" refreshes until a key reaches this age.
Definition at line 168 of file WANObjectCache.php.
const WANObjectCache::CHECK_KEY_TTL = self::TTL_YEAR |
Seconds to keep dependency purge keys around.
Definition at line 156 of file WANObjectCache.php.
const WANObjectCache::COOLOFF_KEY_PREFIX = 'WANCache:c:' |
Definition at line 221 of file WANObjectCache.php.
const WANObjectCache::COOLOFF_TTL = 1 |
Seconds to no-op key set() calls to avoid large blob I/O stampedes.
Definition at line 163 of file WANObjectCache.php.
const WANObjectCache::FLD_FLAGS = 4 |
Definition at line 214 of file WANObjectCache.php.
const WANObjectCache::FLD_HOLDOFF = 5 |
Definition at line 215 of file WANObjectCache.php.
const WANObjectCache::FLD_TIME = 3 |
Definition at line 213 of file WANObjectCache.php.
const WANObjectCache::FLD_TTL = 2 |
Definition at line 212 of file WANObjectCache.php.
const WANObjectCache::FLD_VALUE = 1 |
Definition at line 211 of file WANObjectCache.php.
const WANObjectCache::FLD_VERSION = 0 |
Definition at line 210 of file WANObjectCache.php.
const WANObjectCache::GRACE_TTL_NONE = 0 |
Idiom for set()/getWithSetCallback() for "no post-expired grace period".
Definition at line 187 of file WANObjectCache.php.
const WANObjectCache::HIT_RATE_HIGH = 1 |
Hits/second for a refresh to be expected within the "popularity" window.
Definition at line 172 of file WANObjectCache.php.
const WANObjectCache::HOLDOFF_NONE = 0 |
Idiom for delete() for "no hold-off".
Definition at line 183 of file WANObjectCache.php.
const WANObjectCache::HOLDOFF_TTL = 11 |
Seconds to tombstone keys on delete()
Definition at line 153 of file WANObjectCache.php.
const WANObjectCache::HOT_TTR = 900 |
The time length of the "popularity" refresh window for hot keys.
Definition at line 170 of file WANObjectCache.php.
const WANObjectCache::INTERIM_KEY_PREFIX = 'WANCache:i:' |
Definition at line 218 of file WANObjectCache.php.
const WANObjectCache::INTERIM_KEY_TTL = 1 |
Seconds to keep interim value keys for tombstoned keys around.
Definition at line 158 of file WANObjectCache.php.
const WANObjectCache::LOCK_TTL = 10 |
Seconds to keep lock keys around.
Definition at line 161 of file WANObjectCache.php.
const WANObjectCache::LOW_TTL = 30 |
Default remaining TTL at which to consider pre-emptive regeneration.
Definition at line 165 of file WANObjectCache.php.
const WANObjectCache::MAX_COMMIT_DELAY = 3 |
Max time expected to pass between delete() and DB commit finishing.
Definition at line 149 of file WANObjectCache.php.
const WANObjectCache::MAX_READ_LAG = 7 |
Max replication+snapshot lag before applying TTL_LAGGED or disallowing set()
Definition at line 151 of file WANObjectCache.php.
const WANObjectCache::MIN_TIMESTAMP_NONE = 0.0 |
Idiom for getWithSetCallback() for "no minimum required as-of timestamp".
Definition at line 190 of file WANObjectCache.php.
const WANObjectCache::MUTEX_KEY_PREFIX = 'WANCache:m:' |
Definition at line 220 of file WANObjectCache.php.
const WANObjectCache::PASS_BY_REF = -1 |
Parameter to get()/getMulti() to return extra information by reference.
Definition at line 205 of file WANObjectCache.php.
const WANObjectCache::PC_PRIMARY = 'primary:1000' |
Definition at line 228 of file WANObjectCache.php.
const WANObjectCache::PURGE_VAL_PREFIX = 'PURGED:' |
Definition at line 223 of file WANObjectCache.php.
const WANObjectCache::RAMPUP_TTL = 30 |
Seconds to ramp up to the "popularity" refresh chance after a key is no longer new.
Definition at line 174 of file WANObjectCache.php.
const WANObjectCache::RECENT_SET_HIGH_MS = 100 |
Max millisecond set() backoff for keys in hold-off (far less than INTERIM_KEY_TTL)
Definition at line 202 of file WANObjectCache.php.
const WANObjectCache::RECENT_SET_LOW_MS = 50 |
Min millisecond set() backoff for keys in hold-off (far less than INTERIM_KEY_TTL)
Definition at line 200 of file WANObjectCache.php.
const WANObjectCache::SET_DELAY_HIGH_MS = 50 |
Milliseconds of delay after get() where set() storms are a consideration with 'lockTSE'.
Definition at line 198 of file WANObjectCache.php.
const WANObjectCache::STALE_TTL_NONE = 0 |
Idiom for set()/getWithSetCallback() for "do not augment the storage medium TTL".
Definition at line 185 of file WANObjectCache.php.
const WANObjectCache::TIME_KEY_PREFIX = 'WANCache:t:' |
Definition at line 219 of file WANObjectCache.php.
const WANObjectCache::TINY_NEGATIVE = -0.000001 |
Tiny negative float to use when CTL comes up >= 0 due to clock skew.
Definition at line 193 of file WANObjectCache.php.
const WANObjectCache::TINY_POSTIVE = 0.000001 |
Tiny positive float to use when using "minTime" to assert an inequality.
Definition at line 195 of file WANObjectCache.php.
const WANObjectCache::TSE_NONE = -1 |
Idiom for getWithSetCallback() callbacks to 'lockTSE' logic.
Definition at line 179 of file WANObjectCache.php.
const WANObjectCache::TTL_LAGGED = 30 |
Max TTL to store keys when a data sourced is lagged.
Definition at line 181 of file WANObjectCache.php.
const WANObjectCache::TTL_UNCACHEABLE = -1 |
Idiom for getWithSetCallback() callbacks to avoid calling set()
Definition at line 177 of file WANObjectCache.php.
const WANObjectCache::VALUE_KEY_PREFIX = 'WANCache:v:' |
Definition at line 217 of file WANObjectCache.php.
const WANObjectCache::VERSION = 1 |
Cache format version number.
Definition at line 208 of file WANObjectCache.php.
const WANObjectCache::VFLD_DATA = 'WOC:d' |
Definition at line 225 of file WANObjectCache.php.
const WANObjectCache::VFLD_VERSION = 'WOC:v' |
Definition at line 226 of file WANObjectCache.php.