Cache Class
The cache class allows you to cache the result of a resource heavy operation.
Usage
Using caches can be done in 2 ways: through static usage of the Cache class and through cache objects
returned by the Cache::factory(). This section covers static usage which will always work with the same
driver as specified in the configuration.
Using cache objects and Cache::factory() is explained in the advanced
section.
set($identifier, $contents = null, $expiration = false, $dependencies = array())
Writes a cache.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$identifier |
string |
Name of the cache |
$contents |
mixed |
Contents to be cached |
$expiration |
int |
Amount of seconds until the cache expires. null will make it never expire, false
makes it use the default expiration set in config. |
$dependencies |
array |
Array of identifiers this cache depends on, this cache expires if one of those
is newer or doesn't exist anymore. |
|
Returns |
void |
Example |
// Cache 'String to be cached' for 3 hours with identifier "test"
Cache::set('test', 'String to be cached.', 3600 * 3);
|
get($identifier, $use_expiration = true)
Retrieves a cache.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$identifier |
string |
Name of the cache |
$use_expiration |
true |
Disable cache expiration for this fetching by setting this to false |
|
Returns |
mixed - cache contents |
Throws |
CacheNotFoundException when Cache doesn't exist or CacheExpiredException when it has expired |
Example |
// try to retrieve the cache and save to $content var
try
{
$content = Cache::get('test');
}
catch (\CacheNotFoundException $e)
{
/*
Catching the CacheNotFoundException exception will catch
both CacheNotFoundException and CacheExpiredException.
Use this when cathing the exception.
*/
}
|
delete($identifier)
Deletes a cache.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$identifier |
string |
Name of the cache |
|
Returns |
void |
Example |
Cache::delete('test');
|
delete_all($section = null, $driver = null)
Flushes the whole cache for a specific storage driver or just a subsection of it.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$section |
string |
Name of a section or directory of the cache, null to delete everything |
$driver |
string |
Name of the driver to use, null to use the default driver. |
|
Returns |
void |
Example |
// Delete the whole cache for the default driver
Cache::delete_all();
// Delete the subdirectory "test" of file
Cache::delete_all('test', 'file');
|
call($identifier, $callback, $args = array(), $expiration = null, $dependencies = array())
Cache the result of any callable function or method.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$identifier |
string |
Name of the cache |
$callback |
mixed |
Any valid PHP callback |
$args |
array |
Any arguments for the callback |
$expiration |
int |
Amount of seconds until the cache expires. null will make it never expire, false
makes it use the default expiration set in config. |
$dependencies |
array |
Array of identifiers this cache depends on, this cache expires if one of those
is newer or doesn't exist anymore. |
|
Returns |
void |
Example |
// Cache the result of a model find
Cache::call('article_something', array('Model_Article', 'find'), array("all", array( 'where' => array('something' => $something) ));
|