Cache クラス
The cache class allows you to cache the result of a resource heavy operation.
使用方法
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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$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. |
|
返り値 |
void |
例 |
// 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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$identifier |
string |
Name of the cache |
$use_expiration |
true |
Disable cache expiration for this fetching by setting this to false |
|
返り値 |
mixed - cache contents |
例外 |
CacheNotFoundException when Cache doesn't exist or CacheExpiredException when it has expired |
例 |
// 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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$identifier |
string |
Name of the cache |
|
返り値 |
void |
例 |
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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$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. |
|
返り値 |
void |
例 |
// 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 |
パラメータ |
パラメータ |
規定値 |
説明 |
$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. |
|
返り値 |
void |
例 |
// Cache the result of a model find
Cache::call('article_something', array('Model_Article', 'find'), array("all", array( 'where' => array('something' => $something) ));
|