PHP Class 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.
Author: Matthias Mullie ([email protected])
Inheritance: implements MatthiasMullie\Scrapbook\KeyValueStore
Afficher le fichier Open project: matthiasmullie/scrapbook

Protected Properties

Свойство Type Description
$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.

Méthodes publiques

Méthode Description
__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 )

Méthodes protégées

Méthode Description
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.

Method Details

__construct() public méthode

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 méthode

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

cas() public méthode

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

decrement() public méthode

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

delete() public méthode

public delete ( $key )

deleteMulti() public méthode

public deleteMulti ( array $keys )
$keys array

flush() public méthode

public flush ( )

get() public méthode

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

getMulti() public méthode

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

increment() public méthode

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

protect() protected méthode

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
Résultat string[] Array of keys that were successfully protected

replace() public méthode

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

set() public méthode

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

setMulti() public méthode

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

sleep() protected méthode

When waiting for stampede-protected keys, we'll just sleep, not using much resources.
protected sleep ( ) : boolean
Résultat boolean

stampedeKey() protected méthode

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
Résultat string

touch() public méthode

public touch ( $key, $expire )

Property Details

$attempts protected_oe property

Amount of times every process will poll within $sla time.
protected int $attempts
Résultat integer

$cache protected_oe property

protected KeyValueStore,MatthiasMullie\Scrapbook $cache
Résultat MatthiasMullie\Scrapbook\KeyValueStore

$sla protected_oe property

Amount of time, in milliseconds, this class guarantees protection.
protected int $sla
Résultat integer