PHP Класс MatthiasMullie\Scrapbook\Scale\StampedeProtector
A cache stampede happens when there are a lot of requests for data that is
not currently in cache. Examples could be:
* cache expires for something that is often under very heavy load
* sudden unexpected high load on something that is likely to not be in cache
In those cases, this huge amount of requests for data that is not at that
time in cache, causes that expensive operation to be executed a lot of times,
all at once.
This class is designed to counteract that: if a value can't be found in cache
we'll write something else to cache for a short period of time, to indicate
that another process has already requested this same key (and is probably
already performing that complex operation that will result in the key being
filled)
All of the follow-up requests (that find that the "stampede indicator" has
already been set) will just wait (usleep): instead of crippling the servers
by all having to execute the same operation, these processes will just idle
to give the first process the chance to fill in the cache. Periodically,
these processes will poll the cache to see if the value has already been
stored in the meantime.
The stampede protection will only be temporary, for $sla milliseconds. We
need to limit it because the first process (tasked with filling the cache
after executing the expensive operation) may fail/crash/... If the expensive
operation fails to conclude in < $sla milliseconds.
This class guarantees that the stampede will hold off for $sla amount of time
but after that, all follow-up requests will go through without cached values
and cause a stampede after all, if the initial process fails to complete
within that time.
Показать файл
Открыть проект
Защищенные свойства (Protected)
Свойство |
Тип |
Описание |
|
$attempts |
integer |
Amount of times every process will poll within $sla time. |
|
$cache |
MatthiasMullie\Scrapbook\KeyValueStore |
|
|
$sla |
integer |
Amount of time, in milliseconds, this class guarantees protection. |
|
Открытые методы
Метод |
Описание |
|
__construct ( MatthiasMullie\Scrapbook\KeyValueStore $cache, integer $sla = 1000 ) |
|
|
add ( $key, $value, $expire ) |
|
|
cas ( $token, $key, $value, $expire ) |
|
|
decrement ( $key, $offset = 1, $initial, $expire ) |
|
|
delete ( $key ) |
|
|
deleteMulti ( array $keys ) |
|
|
flush ( ) |
|
|
get ( $key, &$token = null ) |
|
|
getMulti ( array $keys, array &$tokens = null ) |
|
|
increment ( $key, $offset = 1, $initial, $expire ) |
|
|
replace ( $key, $value, $expire ) |
|
|
set ( $key, $value, $expire ) |
|
|
setMulti ( array $items, $expire ) |
|
|
touch ( $key, $expire ) |
|
|
Защищенные методы
Метод |
Описание |
|
protect ( array $keys ) : string[] |
As soon as a key turns up empty (doesn't yet exist in cache), we'll
"protect" it for some time. This will be done by writing to a key similar
to the original key name. If this key is present (which it will only be
for a short amount of time) we'll know it's protected. |
|
sleep ( ) : boolean |
When waiting for stampede-protected keys, we'll just sleep, not using
much resources. |
|
stampedeKey ( string $key ) : string |
To figure out if something has recently been requested already (and is
likely in the process of being recalculated), we'll temporarily write to
another key, so follow-up requests know another process is likely already
re-processing the value. |
|
Описание методов
__construct()
публичный Метод
public __construct ( MatthiasMullie\Scrapbook\KeyValueStore $cache, integer $sla = 1000 ) |
$cache |
MatthiasMullie\Scrapbook\KeyValueStore |
The real cache we'll buffer for |
$sla |
integer |
Stampede protection time, in milliseconds |
public add ( $key, $value, $expire ) |
public cas ( $token, $key, $value, $expire ) |
decrement()
публичный Метод
public decrement ( $key, $offset = 1, $initial, $expire ) |
deleteMulti()
публичный Метод
public get ( $key, &$token = null ) |
getMulti()
публичный Метод
increment()
публичный Метод
public increment ( $key, $offset = 1, $initial, $expire ) |
protect()
защищенный Метод
As soon as a key turns up empty (doesn't yet exist in cache), we'll
"protect" it for some time. This will be done by writing to a key similar
to the original key name. If this key is present (which it will only be
for a short amount of time) we'll know it's protected.
protected protect ( array $keys ) : string[] |
$keys |
array |
|
Результат |
string[] |
Array of keys that were successfully protected |
replace()
публичный Метод
public replace ( $key, $value, $expire ) |
public set ( $key, $value, $expire ) |
setMulti()
публичный Метод
When waiting for stampede-protected keys, we'll just sleep, not using
much resources.
stampedeKey()
защищенный Метод
To figure out if something has recently been requested already (and is
likely in the process of being recalculated), we'll temporarily write to
another key, so follow-up requests know another process is likely already
re-processing the value.
public touch ( $key, $expire ) |
Описание свойств
$attempts защищенное свойство
Amount of times every process will poll within $sla time.
protected int $attempts |
Результат |
integer |
|
$cache защищенное свойство
protected KeyValueStore,MatthiasMullie\Scrapbook $cache |
Результат |
MatthiasMullie\Scrapbook\KeyValueStore |
|
Amount of time, in milliseconds, this class guarantees protection.
protected int $sla |
Результат |
integer |
|