PHP Class MatthiasMullie\Scrapbook\Adapters\Apc

Author: Matthias Mullie ([email protected])
Inheritance: implements MatthiasMullie\Scrapbook\KeyValueStore
Show file Open project: matthiasmullie/scrapbook

Protected Properties

Property Type Description
$expires array APC does this crazy thing of only deleting expired data on every new (page) request, not checking it when you actually retrieve the value (which you may just have set in the same request) Since it's totally possible to store values that expire in the same request, we'll keep track of those expiration times here & filter them out in case they did expire.

Public Methods

Method Description
__construct ( )
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 )

Protected Methods

Method Description
APCuIterator ( string | string[] | null $search = null, integer $format = null, integer $chunk_size = null, integer $list = null ) : APCIterato\APCIterator | APCuIterato\APCuIterator
acquire ( string | string[] $keys ) : string[] Acquire a lock - required to provide CAS functionality.
apcu_add ( string | string[] $key, mixed $var, integer $ttl ) : boolean | bool[]
apcu_clear_cache ( ) : boolean
apcu_delete ( string | string[] | APCIterato\APCIterator | APCuIterato\APCuIterator $key ) : boolean | string[]
apcu_fetch ( string | string[] $key, boolean &$success = null ) : mixed | false
apcu_store ( string | string[] $key, mixed $var, integer $ttl ) : boolean | bool[]
doIncrement ( string $key, integer $offset, integer $initial, integer $expire ) : integer | boolean Shared between increment/decrement: both have mostly the same logic (decrement just increments a negative value), but need their validation split up (increment won't accept negative values).
expire ( array | string $key = [], integer $ttl ) Store the expiration time for items we're setting in this request, to work around APC's behavior of only clearing expires per page request.
lock ( string | string[] $keys ) : array Acquire a lock. If we failed to acquire a lock, it'll automatically try again in 1ms, for a maximum of 10 times.
ttl ( integer $expire ) : integer APC expects true TTL, not expiration timestamp.
unlock ( string | string[] $keys ) : boolean Release a lock.

Method Details

APCuIterator() protected method

protected APCuIterator ( string | string[] | null $search = null, integer $format = null, integer $chunk_size = null, integer $list = null ) : APCIterato\APCIterator | APCuIterato\APCuIterator
$search string | string[] | null
$format integer
$chunk_size integer
$list integer
return APCIterato\APCIterator | APCuIterato\APCuIterator

__construct() public method

public __construct ( )

acquire() protected method

Acquire a lock - required to provide CAS functionality.
protected acquire ( string | string[] $keys ) : string[]
$keys string | string[]
return string[] Array of successfully locked keys

add() public method

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

apcu_add() protected method

protected apcu_add ( string | string[] $key, mixed $var, integer $ttl ) : boolean | bool[]
$key string | string[]
$var mixed
$ttl integer
return boolean | bool[]

apcu_clear_cache() protected method

protected apcu_clear_cache ( ) : boolean
return boolean

apcu_delete() protected method

protected apcu_delete ( string | string[] | APCIterato\APCIterator | APCuIterato\APCuIterator $key ) : boolean | string[]
$key string | string[] | APCIterato\APCIterator | APCuIterato\APCuIterator
return boolean | string[]

apcu_fetch() protected method

protected apcu_fetch ( string | string[] $key, boolean &$success = null ) : mixed | false
$key string | string[]
$success boolean
return mixed | false

apcu_store() protected method

protected apcu_store ( string | string[] $key, mixed $var, integer $ttl ) : boolean | bool[]
$key string | string[]
$var mixed
$ttl integer
return boolean | bool[]

cas() public method

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

decrement() public method

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

delete() public method

public delete ( $key )

deleteMulti() public method

public deleteMulti ( array $keys )
$keys array

doIncrement() protected method

Shared between increment/decrement: both have mostly the same logic (decrement just increments a negative value), but need their validation split up (increment won't accept negative values).
protected doIncrement ( string $key, integer $offset, integer $initial, integer $expire ) : integer | boolean
$key string
$offset integer
$initial integer
$expire integer
return integer | boolean

expire() protected method

Store the expiration time for items we're setting in this request, to work around APC's behavior of only clearing expires per page request.
See also: static::$expires
protected expire ( array | string $key = [], integer $ttl )
$key array | string
$ttl integer

flush() public method

public flush ( )

get() public method

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

getMulti() public method

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

increment() public method

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

lock() protected method

APC provides nothing that would allow us to do CAS. To "emulate" CAS, we'll work with locks: all cache writes also briefly create a lock cache entry (yup: #writes * 3, for lock & unlock - luckily, they're not over the network) Writes are disallows when a lock can't be obtained (= locked by another write), which makes it possible for us to first retrieve, compare & then set in a nob-atomic way. However, there's a possibility for interference with direct APC access touching the same keys - e.g. other scripts, not using this class. If CAS is of importance, make sure the only things touching APC on your server is using these classes!
protected lock ( string | string[] $keys ) : array
$keys string | string[]
return array Array of successfully locked keys

replace() public method

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

set() public method

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

setMulti() public method

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

touch() public method

public touch ( $key, $expire )

ttl() protected method

APC expects true TTL, not expiration timestamp.
protected ttl ( integer $expire ) : integer
$expire integer
return integer TTL in seconds

unlock() protected method

Release a lock.
protected unlock ( string | string[] $keys ) : boolean
$keys string | string[]
return boolean

Property Details

$expires protected property

APC does this crazy thing of only deleting expired data on every new (page) request, not checking it when you actually retrieve the value (which you may just have set in the same request) Since it's totally possible to store values that expire in the same request, we'll keep track of those expiration times here & filter them out in case they did expire.
See also: http://stackoverflow.com/questions/11750223/apc-user-cache-entries-not-expiring
protected array $expires
return array