PHP Класс MatthiasMullie\Scrapbook\Buffered\Utils\Defer

Optimizations will be: * multiple set() values (with the same expiration) will be applied in a single setMulti() * for a set() followed by another set() on the same key, only the latter one will be applied * same for an replace() followed by an increment(), or whatever operation happens on the same key: if we can pre-calculate the end result, we'll only execute 1 operation with the end result * operations before a flush() will not be executed, they'll just be lost Rollback strategy includes: * fetching the original value of operations prone to fail (add, replace & cas) prior to executing them * executing said operations before the others, to minimize changes of interfering concurrent writes * if the commit fails, said original values will be restored in case the new value had already been stored This class must never receive invalid data. E.g. a "replace" can never follow a "delete" of the same key. This should be guaranteed by whatever uses this class: there is no point in re-implementing these checks here. The only acceptable conflicts are when cache values have changed outside, from another process. Those will be handled by this class.
Автор: Matthias Mullie ([email protected])
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$cache MatthiasMullie\Scrapbook\KeyValueStore Cache to write to.
$flush boolean Flush is special - it's not specific to (a) key(s), so we can't store it to $keys.
$keys array[] E.g. 2 sets, the later will override the former. E.g. set + increment, might as well set incremented value immediately. This is going to be an array that holds horrible arrays of update data, being: * 0: the operation name (set, add, ...) so we're able to sort them * 1: a callable, to apply the update to cache * 2: the array of data to supply to the callable

Открытые методы

Метод Описание
__construct ( MatthiasMullie\Scrapbook\KeyValueStore $cache )
__destruct ( )
add ( string $key, mixed $value, integer $expire )
cas ( mixed $originalValue, string $key, mixed $value, integer $expire )
clear ( ) Clears all scheduled writes.
commit ( ) : boolean Commit all deferred writes to cache.
decrement ( string $key, integer $offset, integer $initial, integer $expire )
delete ( string $key )
deleteMulti ( array $keys )
flush ( )
increment ( string $key, integer $offset, integer $initial, integer $expire )
replace ( string $key, mixed $value, integer $expire )
set ( string $key, mixed $value, integer $expire )
setMulti ( array $items, integer $expire )
touch ( string $key, integer $expire )

Защищенные методы

Метод Описание
combineUpdates ( array $updates ) : array We may have multiple sets & deletes, which can be combined into a single setMulti or deleteMulti operation.
doIncrement ( string $operation, string $key, integer $offset, integer $initial, integer $expire )
generateRollback ( ) : array[] Since we can't perform true atomic transactions, we'll fake it.
generateUpdates ( ) : array By storing all updates by key, we've already made sure we don't perform redundant operations on a per-key basis. Now we'll turn those into actual updates.
rollback ( array $old, array $new ) Roll the cache back to pre-transaction state by comparing the current cache values with what we planned to set them to.
sortUpdates ( array $a, array $b ) : integer Change the order of the updates in this transaction to ensure we have those most likely to fail first. That'll decrease odds of having to roll back, and make rolling back easier.

Описание методов

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

public __construct ( MatthiasMullie\Scrapbook\KeyValueStore $cache )
$cache MatthiasMullie\Scrapbook\KeyValueStore

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

public __destruct ( )

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

public add ( string $key, mixed $value, integer $expire )
$key string
$value mixed
$expire integer

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

public cas ( mixed $originalValue, string $key, mixed $value, integer $expire )
$originalValue mixed No real CAS token, but the original value for this key
$key string
$value mixed
$expire integer

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

Clears all scheduled writes.
public clear ( )

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

We may have multiple sets & deletes, which can be combined into a single setMulti or deleteMulti operation.
protected combineUpdates ( array $updates ) : array
$updates array
Результат array

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

When the commit fails, no changes in this transaction will be applied (and those that had already been applied will be undone). False will be returned in that case.
public commit ( ) : boolean
Результат boolean

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

public decrement ( string $key, integer $offset, integer $initial, integer $expire )
$key string
$offset integer
$initial integer
$expire integer

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

public delete ( string $key )
$key string

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

public deleteMulti ( array $keys )
$keys array

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

protected doIncrement ( string $operation, string $key, integer $offset, integer $initial, integer $expire )
$operation string
$key string
$offset integer
$initial integer
$expire integer

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

public flush ( )

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

Most of the operations (set, touch, ...) can't fail. We'll do those last. We'll first schedule the operations that can fail (cas, replace, add) to minimize chances of another process overwriting those values in the meantime. But it could still happen, so we should fetch the current values for all unsafe operations. If the transaction fails, we can then restore them.
protected generateRollback ( ) : array[]
Результат array[] Array of 2 [key => value] maps: current & scheduled data

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

By storing all updates by key, we've already made sure we don't perform redundant operations on a per-key basis. Now we'll turn those into actual updates.
protected generateUpdates ( ) : array
Результат array

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

public increment ( string $key, integer $offset, integer $initial, integer $expire )
$key string
$offset integer
$initial integer
$expire integer

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

public replace ( string $key, mixed $value, integer $expire )
$key string
$value mixed
$expire integer

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

Roll the cache back to pre-transaction state by comparing the current cache values with what we planned to set them to.
protected rollback ( array $old, array $new )
$old array
$new array

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

public set ( string $key, mixed $value, integer $expire )
$key string
$value mixed
$expire integer

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

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

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

Change the order of the updates in this transaction to ensure we have those most likely to fail first. That'll decrease odds of having to roll back, and make rolling back easier.
protected sortUpdates ( array $a, array $b ) : integer
$a array Update, where index 0 is the operation name
$b array Update, where index 0 is the operation name
Результат integer

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

public touch ( string $key, integer $expire )
$key string
$expire integer

Описание свойств

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

Cache to write to.
protected KeyValueStore,MatthiasMullie\Scrapbook $cache
Результат MatthiasMullie\Scrapbook\KeyValueStore

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

Flush is special - it's not specific to (a) key(s), so we can't store it to $keys.
protected bool $flush
Результат boolean

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

E.g. 2 sets, the later will override the former. E.g. set + increment, might as well set incremented value immediately. This is going to be an array that holds horrible arrays of update data, being: * 0: the operation name (set, add, ...) so we're able to sort them * 1: a callable, to apply the update to cache * 2: the array of data to supply to the callable
protected array[] $keys
Результат array[]