Valhalla's Memcached class is easy-to-use Memcached wrapper with advanced logic to ensure valid data is always returned by utilizing logic so that if it is not possible to retrieve new data, the previously cached result will be returned.
The Memcached class's constructor requires an array containing server and caching configuration in the following format:
array(
'servers' => array(
array('localhost', 11211),
),
'default_ttl' => 600,
'max_age' => 604800,
)
When using the Memcached class, you can manually fetch results from cache with the
fetch()
method, or you can used the advanced fetching logic described above with the
getDataWithCaching()
method. This method will return the previously cached result for a time when unable to successfully update the cache.
use Valhalla\CoreUtilities\Cache\Memcached;
Class Test{
public function getData(array $parameters = array()){
//Here one would normally retreive data from a database or API, but for simplicity's sake we'll return static data
return array(
'a' => 'blah',
'b' => 'werwer',
'c' => 'blorp',
);
}
public function testCall(){
$serverConfig = array(
'servers' => array(
array('localhost', 11211),
),
'default_ttl' => 600,
'max_age' => 604800,
);
$c = new Memcached($serverConfig);
return $c->getDataWithCaching($this, 'getData');
}
}
Here is an explanation of each of the available functions for Memcached.
__construct: (The Constructor)
Parameters:
$cacheConfig
The configuration for the cache and Memcached servers (see above)Return Values: None
Creats an instance of the Memcached object and sets the server config.
createCacheKey:
Parameters:
$classname
$parameters
(Optional) Defaults to empty array.Return Value: Returns a unique id for inserting into cache based on the classname and parameters.
Example:
$cacheKey = $cache->createCacheKey(__CLASS__, array(__FUNCTION__, $id));
contains:
Parameters:
$id
A cache IdReturn Value:
Returns
true
if an entry exists in cache with the given id, otherwise
false
.
replace:
Parameters:
$id
A cache Id
$payload
The data to save in cache
$lifetime
(Optional) Defaults to 0 which means to use the default TTL from the server settingsReturn Value:
Returns
true
on success, otherwise
false
.
Given a cache id, payload (i.e. The data to save in cache), and an optional time-to-live value, update an existing entry in cache.
save:
Parameters:
$id
A cache Id
$payload
The data to save in cache
$lifetime
(Optional) Defaults to 0 which means to use the default TTL from the server settingsReturn Value:
Returns
true
on success, otherwise
false
.
Given a cache id, payload (i.e. The data to save in cache), and an optional time-to-live value, inserts an existing entry into cache.
fetch:
Parameters:
$id
A cache IdReturn Value:
Returns an entry from cache with the given cache id. Returns
false
if entry does not exist.
isStillValid:
Parameters:
$cachedData
An entry from cacheReturn Value:
Given a result from cache (e.g. from
fetch()
), returns
true
or
false
to indicate whether the cached result is still within it's allowed time-to-live.
getDataWithCaching:
Parameters:
$class
The class which is requesting data
$function
The function defined in $class for retreiving the desired data
$parameters
(Optional) Arguments to
$class->{$function}()
. Defaults to empty array.
$ttl
The user-defined lifetime for the cacheReturn Value: Returns the cached data.
Given a class name, function name, an optional array of parameters, and an optional time-to-live, retrieve an entry from cache. If the entry is no longer valid, attempt to retrieve new data by calling
$class->{$function}($parameters)
. If unable to retrieve new data, continues to serve the old, invalid cached result until that result has reached max_age at which point it will be deleted. To see an example, see the example above under "Example Usage".