wiki.techinc.nl/includes/Settings/Cache/CacheableSource.php
Dan Duvall d83a7bcd09 Cache loading of SettingsBuilder sources
The `SettingsBuilder` now accepts a PSR-16 cache interface with which to
store and query settings before attempting to load from each source. By
default, no cache is used, but any object that implements the
`Psr\SimpleCache\CacheInterface` may be provided to the constructor.

An explicit dependency on "psr/simple-cache" has been added to
`composer.json`. Note that this dependency already existed in vendor
albeit it as a transitive one.

An APCu based `SharedMemoryCache` adapter is provided as a canonical
PSR-16 compliant interface for production use.

Sources are now queued by the `SettingsBuilder` when calling `load()`.
If a cache interface has been provided, and the source is considered
cacheable (implements `CacheableSource`), then it is wrapped as a
`CachedSource` which will query the cache first before loading from the
wrapped source.

Cache stampedes are mitigated using probabilistic early expiry. The
implementation for this was partially based on symfony/cache-contract
source code but also from the Wikipedia article and paper referenced
therein.

See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration

Bug: T294748
Change-Id: I52ab3899731546876ee58265bd4a1927886746dc
2021-11-29 12:54:59 -08:00

52 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MediaWiki\Settings\Cache;
use MediaWiki\Settings\Source\SettingsSource;
/**
* A {@link SettingsSource} that can be cached. It must return a unique
* (enough) and deterministic hash key for cache indexing.
*
* @since 1.38
* @todo mark as stable before the 1.38 release
*/
interface CacheableSource extends SettingsSource {
/**
* Coefficient used in determining early expiration of cached settings to
* avoid stampedes.
*
* Increasing this value will cause the random early election to happen by
* a larger margin of lead time before normal expiry, relative to the
* cache value's generation duration. Conversely, returning a lesser value
* will narrow the margin of lead time, making the cache hold items for
* slightly longer but with more likelihood that concurrent regenerations
* and set overwrites will occur. Returning <code>0</code> will
* effectively disable early expiration, and by extension disable stampede
* mitigation altogether.
*
* A greater value may be suitable if a source has a highly variable
* generation duration, but most implementations should simply return
* <code>1.0</code>.
*
* @link https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
* @link https://cseweb.ucsd.edu/~avattani/papers/cache_stampede.pdf
*
* Optimal Probabilistic Cache Stampede Prevention
* Vattani, A.; Chierichetti, F.; Lowenstein, K. (2015), "Optimal
* Probabilistic Cache Stampede Prevention" (PDF), Proceedings of the VLDB
* Endowment, VLDB, 8 (8): 886897, doi:10.14778/2757807.2757813, ISSN
* 2150-8097 https://cseweb.ucsd.edu/~avattani/papers/cache_stampede.pdf
*
* @return float
*/
public function getExpiryWeight(): float;
/**
* Returns a deterministically computed key for use in caching settings
* from this source.
*
* @return string
*/
public function getHashKey(): string;
}