CacheInterface
API reference › yii › caching › CacheInterface
Implemented by 1 type
CacheInterface is the base interface for cache.
A data item can be stored in the cache by calling set() and be retrieved back
later (in the same or different request) by get(). In both operations,
a key identifying the data item is required. An expiration time and/or a dependency
can also be specified when calling set(). If the data item expires or the dependency
changes at the time of calling get(), the cache will return no data.
A typical usage pattern of cache is like the following:
$key = 'demo';$data = $cache->get($key);if ($data === false) { // ...generate $data here... $cache->set($key, $data, $duration, $dependency);}Because CacheInterface extends the ArrayAccess interface, it can be used like an array. For example,
$cache['foo'] = 'some data';echo $cache['foo'];For more details and usage information on Cache, see the guide article on caching.
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
add() abstract | Stores a value identified by a key into cache if the cache does not contain this key. | CacheInterface |
buildKey() abstract | Builds a normalized cache key from a given key. | CacheInterface |
delete() abstract | Deletes a value with the specified key from cache. | CacheInterface |
exists() abstract | Checks whether a specified key exists in the cache. | CacheInterface |
flush() abstract | Deletes all values from cache. | CacheInterface |
get() abstract | Retrieves a value from cache with a specified key. | CacheInterface |
getOrSet() abstract | Method combines both set() and get() methods to retrieve value identified by a $key, or to store the result of $callable execution if there is no cache available for the $key. | CacheInterface |
multiAdd() abstract | Stores multiple items in cache. Each item contains a value identified by a key. | CacheInterface |
multiGet() abstract | Retrieves multiple values from cache with the specified keys. | CacheInterface |
multiSet() abstract | Stores multiple items in cache. Each item contains a value identified by a key. | CacheInterface |
set() abstract | Stores a value identified by a key into cache. | CacheInterface |
Method details
Section titled “Method details”add()
Section titled “add() ”abstract public function add(mixed $key, mixed $value, int $duration = 0, yii\caching\Dependency|null $dependency = null): boolStores a value identified by a key into cache if the cache does not contain this key.
Nothing will be done if the cache already contains the key.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key. |
$value | mixed | The value to be cached |
$duration | int | The number of seconds in which the cached value will expire. 0 means never expire. |
$dependency | Dependency| | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false. |
Returns
bool: Whether the value is successfully stored into cache
buildKey()
Section titled “buildKey() ”abstract public function buildKey(mixed $key): stringBuilds a normalized cache key from a given key.
If the given key is a string containing alphanumeric characters only and no more than 32 characters,
then the key will be returned back prefixed with keyPrefix. Otherwise, a normalized key
is generated by serializing the given key, applying MD5 hashing, and prefixing with keyPrefix.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | The key to be normalized |
Returns
string: The generated cache key
delete()
Section titled “delete() ”abstract public function delete(mixed $key): boolDeletes a value with the specified key from cache.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the value to be deleted from cache. This can be a simple string or a complex data structure consisting of factors representing the key. |
Returns
bool: If no error happens during deletion
exists()
Section titled “exists() ”abstract public function exists(mixed $key): boolChecks whether a specified key exists in the cache.
This can be faster than getting the value from the cache if the data is big.
In case a cache does not support this feature natively, this method will try to simulate it
but has no performance improvement over getting it.
Note that this method does not check whether the dependency associated
with the cached data, if there is any, has changed. So a call to get()
may return false while exists returns true.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key. |
Returns
bool: True if a value exists in cache, false if the value is not in the cache or expired.
flush()
Section titled “flush() ”abstract public function flush(): boolDeletes all values from cache.
Be careful of performing this operation if the cache is shared among multiple applications.
Returns
bool: Whether the flush operation was successful.
get()
Section titled “get() ”abstract public function get(mixed $key): mixedRetrieves a value from cache with a specified key.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the cached value. This can be a simple string or a complex data structure consisting of factors representing the key. |
Returns
mixed: The value stored in cache, false if the value is not in the cache, expired, or the dependency associated with the cached data has changed.
getOrSet()
Section titled “getOrSet() ”abstract public function getOrSet(mixed $key, callable(): yii\caching\TResult|Closure(): yii\caching\TResult $callable, int|null $duration = null, yii\caching\Dependency|null $dependency = null): yii\caching\TResultMethod combines both set() and get() methods to retrieve value identified by a $key,
or to store the result of $callable execution if there is no cache available for the $key.
Usage example:
public function getTopProducts($count = 10) { $cache = $this->cache; // Could be Yii::$app->cache return $cache->getOrSet(['top-n-products', 'n' => $count], function ($cache) use ($count) { return Products::find()->mostPopular()->limit($count)->all(); }, 1000);}Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key. |
$callable | callable(): yii\Closure(): yii\ | The callable or closure that will be used to generate a value to be cached. In case $callable returns false, the value will not be cached. |
$duration | int| | Default duration in seconds before the cache will expire. If not set, defaultDuration value will be used. |
$dependency | Dependency| | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false. |
Returns
yii\caching\TResult: Result of $callable execution
multiAdd()
Section titled “multiAdd() ”abstract public function multiAdd(array $items, int $duration = 0, yii\caching\Dependency|null $dependency = null): arrayStores multiple items in cache. Each item contains a value identified by a key.
If the cache already contains such a key, the existing value and expiration time will be preserved.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array | The items to be cached, as key-value pairs. |
$duration | int | Default number of seconds in which the cached values will expire. 0 means never expire. |
$dependency | Dependency| | Dependency of the cached items. If the dependency changes, the corresponding values in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false. |
Returns
array: Array of failed keys
multiGet()
Section titled “multiGet() ”abstract public function multiGet(string[] $keys): arrayRetrieves multiple values from cache with the specified keys.
Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, which may improve the performance. In case a cache does not support this feature natively, this method will try to simulate it.
Parameters
| Parameter | Type | Description |
|---|---|---|
$keys | string[] | List of string keys identifying the cached values |
Returns
array: List of cached values corresponding to the specified keys. The array is returned in terms of (key, value) pairs. If a value is not cached or expired, the corresponding array value will be false.
multiSet()
Section titled “multiSet() ”abstract public function multiSet(array $items, int|null $duration = null, yii\caching\Dependency|null $dependency = null): arrayStores multiple items in cache. Each item contains a value identified by a key.
If the cache already contains such a key, the existing value and expiration time will be replaced with the new ones, respectively.
Parameters
| Parameter | Type | Description |
|---|---|---|
$items | array | The items to be cached, as key-value pairs. |
$duration | int| | Default duration in seconds before the cache will expire. If not set, default defaultDuration value is used. |
$dependency | Dependency| | Dependency of the cached items. If the dependency changes, the corresponding values in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false. |
Returns
array: Array of failed keys
set()
Section titled “set() ”abstract public function set(mixed $key, mixed $value, int|null $duration = null, yii\caching\Dependency|null $dependency = null): boolStores a value identified by a key into cache.
If the cache already contains such a key, the existing value and expiration time will be replaced with the new ones, respectively.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | mixed | A key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key. |
$value | mixed | The value to be cached |
$duration | int| | Default duration in seconds before the cache will expire. If not set, default defaultDuration value is used. |
$dependency | Dependency| | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false. |
Returns
bool: Whether the value is successfully stored into cache