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
파일 보기 프로젝트 열기: matthiasmullie/scrapbook

보호된 프로퍼티들

프로퍼티 타입 설명
$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