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::forge(). This section covers static usage which will always work with the same driver as specified in the configuration.

Using cache objects and Cache::forge() is explained in the advanced section.

Garbage Collection

There is currently NO garbage collection mechanism build into the Cache drivers. Storage backends that have built-in support for data expiration, such as APC, Memcached or Redis, will use that feature, and will auto expire stale cache entries.

This means that if you use files to store your cached entries, you will have to write a cron job that will periodically delete all old files based on their last modified timestamp!

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);

// You can add a section or directory of the cache with dot-notation
Cache::set('test.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 catching 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) ));