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.
Автор: Matthias Mullie ([email protected])
Наследование: implements MatthiasMullie\Scrapbook\KeyValueStore
Показать файл Открыть проект

Защищенные свойства (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

add() публичный Метод

public add ( $key, $value, $expire )

cas() публичный Метод

public cas ( $token, $key, $value, $expire )

decrement() публичный Метод

public decrement ( $key, $offset = 1, $initial, $expire )

delete() публичный Метод

public delete ( $key )

deleteMulti() публичный Метод

public deleteMulti ( array $keys )
$keys array

flush() публичный Метод

public flush ( )

get() публичный Метод

public get ( $key, &$token = null )

getMulti() публичный Метод

public getMulti ( array $keys, array &$tokens = null )
$keys array
$tokens array

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 )

set() публичный Метод

public set ( $key, $value, $expire )

setMulti() публичный Метод

public setMulti ( array $items, $expire )
$items array

sleep() защищенный Метод

When waiting for stampede-protected keys, we'll just sleep, not using much resources.
protected sleep ( ) : boolean
Результат boolean

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.
protected stampedeKey ( string $key ) : string
$key string
Результат string

touch() публичный Метод

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

$sla защищенное свойство

Amount of time, in milliseconds, this class guarantees protection.
protected int $sla
Результат integer