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 |
Cache_Exception - when Cache doesn't exist or has expired |
Example |
// try to retrieve the cache and save to $content var
try
{
$content = Cache::get('test');
}
// generate the $content var contents and save cache when that failed
catch (Exception $e)
{
$content = 'String to be cached.';
Cache::set('test', $content, 3600 * 3);
}
|
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');
|