A data item can be stored in the cache by calling Cache::set and be retrieved back
later (in the same or different request) by Cache::get. In both operations,
a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]]
can also be specified when calling Cache::set. If the data item expires or the dependency
changes at the time of calling Cache::get, the cache will return no data.
A typical usage pattern of cache is like the following:
php
$key = 'demo';
$data = $cache->get($key);
if ($data === false) {
...generate $data here...
$cache->set($key, $data, $duration, $dependency);
}
Because Cache implements the [[\ArrayAccess]] interface, it can be used like an array. For example,
php
$cache['foo'] = 'some data';
echo $cache['foo'];
Derived classes should implement the following methods which do the actual cache storage operations:
- Cache::getValue: retrieve the value with a key (if any) from cache
- Cache::setValue: store the value with a key into cache
- Cache::addValue: store the value only if the cache does not have this key before
- Cache::deleteValue: delete the value with the specified key from cache
- Cache::flushValues: delete all values from cache