Scrapbook

Buffer extends MemoryStore
in package

This is a helper class for BufferedStore & TransactionalStore, which buffer real cache requests in memory.

The memory-part can easily be handled by MemoryStore. There's just 1 gotcha: when an item is to be deleted (but not yet committed), it needs to be deleted from the MemoryStore too, but we need to be able to make a distinction between "this is deleted" and "this value is not known in this memory cache, fall back to real cache".

This is where this class comes in to play: we'll add an additional "expired" method, which allows BufferedStore to just expire the keys that are supposed to be deleted (instead of deleting them) - then we can keep track of when a key is just not known, or known-but-deleted (=expired)

Tags
author

Matthias Mullie [email protected]

copyright

Copyright (c) 2014, Matthias Mullie. All rights reserved

license

LICENSE MIT

Table of Contents

Methods

__construct()  : mixed
add()  : bool
Adds an item under new key.
cas()  : bool
Replaces an item in 1 atomic operation, to ensure it didn't change since it was originally read, when the CAS token was issued.
decrement()  : int|bool
Decrements a counter value, or sets an initial value if it does not yet exist.
delete()  : bool
Deletes an item from the cache.
deleteMulti()  : array<string|int, bool>
Deletes multiple items at once (reduced network traffic compared to individual operations).
expired()  : bool
Check if a key existed in local storage, but is now expired.
flush()  : bool
Clears the entire cache.
get()  : mixed|bool
Retrieves an item from the cache.
getMulti()  : array<string|int, mixed>
Retrieves multiple items at once.
increment()  : int|bool
Increments a counter value, or sets an initial value if it does not yet exist.
replace()  : bool
Replaces an item.
set()  : bool
Stores a value, regardless of whether or not the key already exists (in which case it will overwrite the existing value for that key).
setMulti()  : array<string|int, bool>
Store multiple values at once.
touch()  : bool
Updates an item's expiration time without altering the stored value.

Methods

__construct()

public __construct([int|string $limit = null ]) : mixed
Parameters
$limit : int|string = null

Memory limit in bytes (defaults to 10% of memory_limit)

add()

Adds an item under new key.

public add(mixed $key, mixed $value[, mixed $expire = 0 ]) : bool
Parameters
$key : mixed
$value : mixed
$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
bool

cas()

Replaces an item in 1 atomic operation, to ensure it didn't change since it was originally read, when the CAS token was issued.

public cas(mixed $token, mixed $key, mixed $value[, mixed $expire = 0 ]) : bool
Parameters
$token : mixed

Token received from get() or getMulti()

$key : mixed
$value : mixed
$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
bool

decrement()

Decrements a counter value, or sets an initial value if it does not yet exist.

public decrement(mixed $key[, mixed $offset = 1 ][, mixed $initial = 0 ][, mixed $expire = 0 ]) : int|bool
Parameters
$key : mixed
$offset : mixed = 1

Value to decrement with

$initial : mixed = 0

Initial value (if item doesn't yet exist)

$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
int|bool

New value or false on failure

delete()

Deletes an item from the cache.

public delete(mixed $key) : bool
Parameters
$key : mixed
Return values
bool

deleteMulti()

Deletes multiple items at once (reduced network traffic compared to individual operations).

public deleteMulti(array<string|int, mixed> $keys) : array<string|int, bool>
Parameters
$keys : array<string|int, mixed>
Return values
array<string|int, bool>

expired()

Check if a key existed in local storage, but is now expired.

public expired(string $key) : bool

Because our local buffer is also just a real cache, expired items will just return nothing, which will lead us to believe no such item exists in that local cache, and we'll reach out to the real cache (where the value may not yet have been expired because that may have been part of an uncommitted write) So we'll want to know when a value is in local cache, but expired!

Parameters
$key : string
Return values
bool

flush()

Clears the entire cache.

public flush() : bool
Return values
bool

get()

Retrieves an item from the cache.

public get(mixed $key[, mixed &$token = null ]) : mixed|bool
Parameters
$key : mixed
$token : mixed = null

Will be filled with the CAS token

Return values
mixed|bool

Value, or false on failure

getMulti()

Retrieves multiple items at once.

public getMulti(array<string|int, mixed> $keys[, array<string|int, mixed> &$tokens = null ]) : array<string|int, mixed>
Parameters
$keys : array<string|int, mixed>
$tokens : array<string|int, mixed> = null

Will be filled with the CAS tokens, in [key => token] format

Return values
array<string|int, mixed>

[key => value]

increment()

Increments a counter value, or sets an initial value if it does not yet exist.

public increment(mixed $key[, mixed $offset = 1 ][, mixed $initial = 0 ][, mixed $expire = 0 ]) : int|bool
Parameters
$key : mixed
$offset : mixed = 1

Value to increment with

$initial : mixed = 0

Initial value (if item doesn't yet exist)

$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
int|bool

New value or false on failure

replace()

Replaces an item.

public replace(mixed $key, mixed $value[, mixed $expire = 0 ]) : bool
Parameters
$key : mixed
$value : mixed
$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
bool

set()

Stores a value, regardless of whether or not the key already exists (in which case it will overwrite the existing value for that key).

public set(mixed $key, mixed $value[, mixed $expire = 0 ]) : bool
Parameters
$key : mixed
$value : mixed
$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
bool

setMulti()

Store multiple values at once.

public setMulti(array<string|int, mixed> $items[, mixed $expire = 0 ]) : array<string|int, bool>
Parameters
$items : array<string|int, mixed>

[key => value]

$expire : mixed = 0

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
array<string|int, bool>

touch()

Updates an item's expiration time without altering the stored value.

public touch(mixed $key, mixed $expire) : bool
Parameters
$key : mixed
$expire : mixed

Time when item falls out of the cache: 0 = permanent (doesn't expires); under 2592000 (30 days) = relative time, in seconds from now; over 2592000 = absolute time, unix timestamp

Return values
bool

        
On this page

Search results