Wrapper for Memcached with local storage support

\Exception
package Library
subpackage Cache
see \Memcached\http://pecl.php.net/package/memcached
see \Memcached\http://pecl.php.net/package/igbinary
author Aleksey Korzun
version 0.2
license MIT
link http://www.webfoundation.net
link http://www.alekseykorzun.com

 Methods

Pass all method calls directly to instance of Memcached

__call(string $name, mixed[] $arguments) : mixed

Parameters

$name

string

method that was invoked

$arguments

mixed[]

arguments that were passed to invoked method

Returns

mixed

Class initializer, creates a new singleton instance of Memcached with optimized configuration

__construct(string $pool, array $servers) 

Parameters

$pool

string

create an instance of cache client with a specfic pool

$servers

mixed[]

a list of Memcached servers we will be using, each entry in servers is supposed to be an array containing hostname, port, and optionally, weight of the server Example: $servers = array( array('mem1.domain.com', 11211, 33), array('mem2.domain.com', 11211, 67) ); See: http://www.php.net/manual/en/memcached.addservers.php

Exceptions

\Exception we want to bail if Memcached extension is not loaded or if passed server list is invalid

Activate caching

activate() 

Deactivate caching

deactivate() 

Delete key(s) from cache and local storage

delete(string[] | string $keys) : bool

Parameters

$keys

string[]string

array of keys to delete or a single key

Returns

boolreturns false if it failed to delete any of the keys

Retrieve data based on provided key from cache pool, this method will call either getArray or getSimple depending on amount of keys requested.

get(string[] | string $keys, mixed $resource, bool $purge) : bool

Parameters

$keys

string[]string

an array of keys or a single key to look-up from cache

$resource

mixed

where to store retrieved resource

$purge

bool

if you wish to remove key from pool after retrieving resource associated with this you can pass this as true

Returns

boolreturns true on a successful request, false on a failure or forced expiration

Retrieve instance of Memcached client

instance() : \Memcached

Returns

Check if caching is currently active

isActive() : bool

Returns

boolreturns true if caching is active otherwise false

Checks if local storage is enabled

isStorageEnabled() : bool

Returns

boolreturns true if local storage is enabled false otherwise

Check if passed key is stored using local store

isStored(string $key) : bool

Parameters

$key

string

Returns

bool

Add a new cached record using passed resource and key association

set(string $key, mixed $resource, int $ttl) : bool

Parameters

$key

string

key to store passed resource under

$resource

mixed

resource you want to cache

$ttl

int

when should this key expire in seconds

Exceptions

\Exception if the key is over 250 bytes

Returns

bool

Validate array of cache servers that should be loaded to Memcached extension

validateServers(array $servers) 

Parameters

$servers

mixed[]

a list of Memcached servers we will be using

Exceptions

\Exception if we detect something out of specification

Retrieve multiple resources from cache pool and/or local storage

getArray(array $keys) : mixed[] | bool

Parameters

$keys

string[]

array of keys to look-up from cache

Returns

mixed[]boolreturns array of retrieved resources or false if look up fails

Retrieve single resource from cache pool / local storage

getSimple(string $key) : mixed | bool

Parameters

$key

string

key to look-up from cache

Returns

mixedboolreturns cached resource or false on failure

Store data locally

store(string $key, mixed $resource) : mixed

Parameters

$key

string

key to save resource under

$resource

mixed

what you are storing in cache

Returns

mixedresource that we attempted to store

Get requested data back into memory while setting a delayed cache entry if data is expiring soon

unwrap(string $key, array $data) : mixed | bool
see \Memcached\http://highscalability.com/strategy-break-memcache-dog-pile

Parameters

$key

string

key that you are retrieving

$data

mixed[]

packed data that we got back from cache pool

Returns

mixedboolreturns cached resource or false if invalid data was passed for unwrapping

Wrap new cached resource into an array containing TTL stamp

wrap(mixed $resource, int $ttl) : mixed[]
see \Memcached\http://highscalability.com/strategy-break-memcache-dog-pile

Parameters

$resource

mixed

resource that is getting cached

$ttl

int

internal extended expiration

Returns

mixed[]returns packed resource with TTL stamp to store in cache

 Properties

 

$isActive : bool
 

$isResourceExpired : bool
 

$isResourceInvalid : bool
 

$isStorageEnabled : bool
 

$memcached : \Memcached
 

$storage : array

 Constants

 

Default life time of all caches wrapper creates in seconds

DEFAULT_TTL : int
 

Dog-pile prevention delay in seconds, adjust if you have a constant miss of cache after expiration because un-cached call takes more then specified delay

DELAY : int
 

Parent expiration padding (so internal cache stamp does not expire before the actual cache) in seconds

EXTENDED_TTL : int