# Cache

> Cache is the base class for cache classes supporting different cache storage implementations.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [caching](/docs/api/yii/caching/index.md) › Cache

| abstract class | `yii\caching\Cache` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/caching/Cache.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/caching/Cache.php#L55) |

7 subclasses:

- [`ApcuCache`](/docs/api/yii/caching/apcucache/index.md)
- [`ArrayCache`](/docs/api/yii/caching/arraycache/index.md)
- [`DbCache`](/docs/api/yii/caching/dbcache/index.md)
- [`DummyCache`](/docs/api/yii/caching/dummycache/index.md)
- [`FileCache`](/docs/api/yii/caching/filecache/index.md)
- [`MemCache`](/docs/api/yii/caching/memcache/index.md)
- [`WinCache`](/docs/api/yii/caching/wincache/index.md)

Cache is the base class for cache classes supporting different cache storage implementations.

A data item can be stored in the cache by calling [`set()`](/docs/api/yii/caching/cache/index.md#method-set) and be retrieved back
later (in the same or different request) by [`get()`](/docs/api/yii/caching/cache/index.md#method-get). In both operations,
a key identifying the data item is required. An expiration time and/or a [dependency](/docs/api/yii/caching/dependency/index.md)
can also be specified when calling [`set()`](/docs/api/yii/caching/cache/index.md#method-set). If the data item expires or the dependency
changes at the time of calling [`get()`](/docs/api/yii/caching/cache/index.md#method-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 Cache implements the [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface, it can be used like an array. For example,

```
$cache['foo'] = 'some data';
echo $cache['foo'];
```

Derived classes should implement the following methods which do the actual cache storage operations:

- [`getValue()`](/docs/api/yii/caching/cache/index.md#method-getValue): retrieve the value with a key (if any) from cache
- [`setValue()`](/docs/api/yii/caching/cache/index.md#method-setValue): store the value with a key into cache
- [`addValue()`](/docs/api/yii/caching/cache/index.md#method-addValue): store the value only if the cache does not have this key before
- [`deleteValue()`](/docs/api/yii/caching/cache/index.md#method-deleteValue): delete the value with the specified key from cache
- [`flushValues()`](/docs/api/yii/caching/cache/index.md#method-flushValues): delete all values from cache

For more details and usage information on Cache, see the [guide article on caching](/docs/guide/caching-overview/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`$defaultDuration`](#property-defaultDuration) | `int` | Default duration in seconds before a cache entry will expire. Default: `0`. Available since 2.0.11. | `Cache` |
| [`$keyPrefix`](#property-keyPrefix) | `string` | A string prefixed to every cache key so that it is unique globally in the whole cache storage. Default: `null`. | `Cache` |
| [`$serializer`](#property-serializer) | `array\|null\|false` | The functions used to serialize and unserialize cached data. Default: `null`. | `Cache` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`add()`](#method-add) | Stores a value identified by a key into cache if the cache does not contain this key. | `Cache` |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`buildKey()`](#method-buildKey) | Builds a normalized cache key from a given key. | `Cache` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`delete()`](#method-delete) | Deletes a value with the specified key from cache. | `Cache` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`exists()`](#method-exists) | Checks whether a specified key exists in the cache. | `Cache` |
| [`flush()`](#method-flush) | Deletes all values from cache. | `Cache` |
| [`get()`](#method-get) | Retrieves a value from cache with a specified key. | `Cache` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getOrSet()`](#method-getOrSet) | Method combines both [`set()`](/docs/api/yii/caching/cache/index.md#method-set) and [`get()`](/docs/api/yii/caching/cache/index.md#method-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. | `Cache` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `Cache` |
| [`multiAdd()`](#method-multiAdd) | Stores multiple items in cache. Each item contains a value identified by a key. | `Cache` |
| [`multiGet()`](#method-multiGet) | Retrieves multiple values from cache with the specified keys. | `Cache` |
| [`multiSet()`](#method-multiSet) | Stores multiple items in cache. Each item contains a value identified by a key. | `Cache` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`offsetExists()`](#method-offsetExists) | Returns whether there is a cache entry with a specified key. | `Cache` |
| [`offsetGet()`](#method-offsetGet) | Retrieves the value from cache with a specified key. | `Cache` |
| [`offsetSet()`](#method-offsetSet) | Stores the value identified by a key into cache. | `Cache` |
| [`offsetUnset()`](#method-offsetUnset) | Deletes the value with the specified key from cache This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `Cache` |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`set()`](#method-set) | Stores a value identified by a key into cache. | `Cache` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`addValue()`](#method-addValue) protected abstract | Stores a value identified by a key into cache if the cache does not contain this key. | `Cache` |
| [`addValues()`](#method-addValues) protected | Adds multiple key-value pairs to cache. | `Cache` |
| [`deleteValue()`](#method-deleteValue) protected abstract | Deletes a value with the specified key from cache This method should be implemented by child classes to delete the data from actual cache storage. | `Cache` |
| [`flushValues()`](#method-flushValues) protected abstract | Deletes all values from cache. | `Cache` |
| [`getValue()`](#method-getValue) protected abstract | Retrieves a value from cache with a specified key. | `Cache` |
| [`getValues()`](#method-getValues) protected | Retrieves multiple values from cache with the specified keys. | `Cache` |
| [`setValue()`](#method-setValue) protected abstract | Stores a value identified by a key in cache. | `Cache` |
| [`setValues()`](#method-setValues) protected | Stores multiple key-value pairs in cache. | `Cache` |

## Property details

### $defaultDuration <span id="property-defaultDuration"></span>

```php
public int $defaultDuration = 0
```

Default duration in seconds before a cache entry will expire.

Default value is 0, meaning infinity.
This value is used by [`set()`](/docs/api/yii/caching/cache/index.md#method-set) if the duration is not explicitly given.

**Since**

2.0.11

### $keyPrefix <span id="property-keyPrefix"></span>

```php
public string $keyPrefix = null
```

A string prefixed to every cache key so that it is unique globally in the whole cache storage.

It is recommended that you set a unique cache key prefix for each application if the same cache
storage is being used by different applications.

To ensure interoperability, only alphanumeric characters should be used.

### $serializer <span id="property-serializer"></span>

```php
public array|null|false $serializer = null
```

The functions used to serialize and unserialize cached data.

Defaults to null, meaning
using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient
serializer (e.g. [igbinary](https://pecl.php.net/package/igbinary)), you may configure this property with
a two-element array. The first element specifies the serialization function, and the second the deserialization
function. If this property is set false, data will be directly sent to and retrieved from the underlying
cache component without any serialization or deserialization. You should not turn off serialization if
you are using [cache dependency](/docs/api/yii/caching/dependency/index.md), because it relies on data serialization. Also, some
implementations of the cache can not correctly save and retrieve data different from a string type.

## Method details

### add() <span id="method-add"></span>

```php
public function add(mixed $key, mixed $value, int $duration = 0, yii\caching\Dependency|null $dependency = null): bool
```

Stores 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`](/docs/api/yii/caching/dependency/index.md)`\|null` | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via [`get()`](/docs/api/yii/caching/cache/index.md#method-get). This parameter is ignored if [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer) is false. |

**Returns**

`bool`: Whether the value is successfully stored into cache

### buildKey() <span id="method-buildKey"></span>

```php
public function buildKey(mixed $key): string
```

Builds 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`](/docs/api/yii/caching/cache/index.md#property-keyPrefix). Otherwise, a normalized key
is generated by serializing the given key, applying MD5 hashing, and prefixing with [`$keyPrefix`](/docs/api/yii/caching/cache/index.md#property-keyPrefix).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `mixed` | The key to be normalized |

**Returns**

`string`: The generated cache key

### delete() <span id="method-delete"></span>

```php
public function delete(mixed $key): bool
```

Deletes 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() <span id="method-exists"></span>

```php
public function exists(mixed $key): bool
```

Checks 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()`](/docs/api/yii/caching/cache/index.md#method-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() <span id="method-flush"></span>

```php
public function flush(): bool
```

Deletes 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() <span id="method-get"></span>

```php
public function get(mixed $key): mixed
```

Retrieves 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() <span id="method-getOrSet"></span>

```php
public function getOrSet(mixed $key, callable|Closure $callable, int|null $duration = null, yii\caching\Dependency|null $dependency = null): mixed
```

Method combines both [`set()`](/docs/api/yii/caching/cache/index.md#method-set) and [`get()`](/docs/api/yii/caching/cache/index.md#method-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 () 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\|`[`Closure`](https://www.php.net/manual/en/class.closure.php) | The callable or closure that will be used to generate a value to be cached. If you use $callable that can return `false`, then keep in mind that [`getOrSet()`](/docs/api/yii/caching/cache/index.md#method-getOrSet) may work inefficiently because the [`Cache::get()`](/docs/api/yii/caching/cache/index.md#method-get) method uses `false` return value to indicate the data item is not found in the cache. Thus, caching of `false` value will lead to unnecessary internal calls. |
| `$duration` | `int\|null` | Default duration in seconds before the cache will expire. If not set, [`$defaultDuration`](/docs/api/yii/caching/cache/index.md#property-defaultDuration) value will be used. |
| `$dependency` | [`Dependency`](/docs/api/yii/caching/dependency/index.md)`\|null` | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via [`get()`](/docs/api/yii/caching/cache/index.md#method-get). This parameter is ignored if [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer) is `false`. |

**Returns**

`mixed`: Result of $callable execution

**Since**

2.0.11

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### multiAdd() <span id="method-multiAdd"></span>

```php
public function multiAdd(array $items, int $duration = 0, yii\caching\Dependency|null $dependency = null): array
```

Stores 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`](/docs/api/yii/caching/dependency/index.md)`\|null` | Dependency of the cached items. If the dependency changes, the corresponding values in the cache will be invalidated when it is fetched via [`get()`](/docs/api/yii/caching/cache/index.md#method-get). This parameter is ignored if [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer) is false. |

**Returns**

`array`: Array of failed keys

**Since**

2.0.7

### multiGet() <span id="method-multiGet"></span>

```php
public function multiGet(string[] $keys): array
```

Retrieves 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.

**Since**

2.0.7

### multiSet() <span id="method-multiSet"></span>

```php
public function multiSet(array $items, int|null $duration = null, yii\caching\Dependency|null $dependency = null): array
```

Stores 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\|null` | Default duration in seconds before the cache will expire. If not set, default [`$defaultDuration`](/docs/api/yii/caching/cache/index.md#property-defaultDuration) value is used. |
| `$dependency` | [`Dependency`](/docs/api/yii/caching/dependency/index.md)`\|null` | Dependency of the cached items. If the dependency changes, the corresponding values in the cache will be invalidated when it is fetched via [`get()`](/docs/api/yii/caching/cache/index.md#method-get). This parameter is ignored if [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer) is false. |

**Returns**

`array`: Array of failed keys

**Since**

2.0.7

### offsetExists() <span id="method-offsetExists"></span>

```php
public function offsetExists(string $key): bool
```

Returns whether there is a cache entry with a specified key.

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | A key identifying the cached value |

**Returns**

`bool`

### offsetGet() <span id="method-offsetGet"></span>

```php
public function offsetGet(string $key): mixed
```

Retrieves the value from cache with a specified key.

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | A key identifying the cached value |

**Returns**

`mixed`: The value stored in cache, false if the value is not in the cache or expired.

### offsetSet() <span id="method-offsetSet"></span>

```php
public function offsetSet(string $key, mixed $value): mixed
```

Stores the value identified by a key into cache.

If the cache already contains such a key, the existing value will be
replaced with the new ones. To add expiration and dependencies, use the [`set()`](/docs/api/yii/caching/cache/index.md#method-set) method.
This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the value to be cached |
| `$value` | `mixed` | The value to be cached |

**Returns**

`mixed`

### offsetUnset() <span id="method-offsetUnset"></span>

```php
public function offsetUnset(string $key): mixed
```

Deletes the value with the specified key from cache
This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key of the value to be deleted |

**Returns**

`mixed`

### set() <span id="method-set"></span>

```php
public function set(mixed $key, mixed $value, int|null $duration = null, yii\caching\Dependency|null $dependency = null): bool
```

Stores 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\|null` | Default duration in seconds before the cache will expire. If not set, default [`$defaultDuration`](/docs/api/yii/caching/cache/index.md#property-defaultDuration) value is used. |
| `$dependency` | [`Dependency`](/docs/api/yii/caching/dependency/index.md)`\|null` | Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via [`get()`](/docs/api/yii/caching/cache/index.md#method-get). This parameter is ignored if [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer) is false. |

**Returns**

`bool`: Whether the value is successfully stored into cache

### addValue() <span id="method-addValue"></span>

```php
abstract protected function addValue(string $key, mixed $value, int $duration): bool
```

Stores a value identified by a key into cache if the cache does not contain this key.

This method should be implemented by child classes to store the data
in specific cache storage.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the value to be cached |
| `$value` | `mixed` | The value to be cached. Most often it's a string. If you have disabled [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer), it could be something else. |
| `$duration` | `int` | The number of seconds in which the cached value will expire. 0 means never expire. |

**Returns**

`bool`: True if the value is successfully stored into cache, false otherwise

### addValues() <span id="method-addValues"></span>

```php
protected function addValues(array $data, int $duration): array
```

Adds multiple key-value pairs to cache.

The default implementation calls [`addValue()`](/docs/api/yii/caching/cache/index.md#method-addValue) multiple times add values one by one. If the underlying cache
storage supports multi-add, this method should be overridden to exploit that feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | Array where key corresponds to cache key while value is the value stored. |
| `$duration` | `int` | The number of seconds in which the cached values will expire. 0 means never expire. |

**Returns**

`array`: Array of failed keys

### deleteValue() <span id="method-deleteValue"></span>

```php
abstract protected function deleteValue(string $key): bool
```

Deletes a value with the specified key from cache
This method should be implemented by child classes to delete the data from actual cache storage.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key of the value to be deleted |

**Returns**

`bool`: If no error happens during deletion

### flushValues() <span id="method-flushValues"></span>

```php
abstract protected function flushValues(): bool
```

Deletes all values from cache.

Child classes may implement this method to realize the flush operation.

**Returns**

`bool`: Whether the flush operation was successful.

### getValue() <span id="method-getValue"></span>

```php
abstract protected function getValue(string $key): mixed|false
```

Retrieves a value from cache with a specified key.

This method should be implemented by child classes to retrieve the data
from specific cache storage.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | A unique key identifying the cached value |

**Returns**

`mixed|false`: The value stored in cache, false if the value is not in the cache or expired. Most often value is a string. If you have disabled [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer), it could be something else.

### getValues() <span id="method-getValues"></span>

```php
protected function getValues(array $keys): array
```

Retrieves multiple values from cache with the specified keys.

The default implementation calls [`getValue()`](/docs/api/yii/caching/cache/index.md#method-getValue) multiple times to retrieve
the cached values one by one. If the underlying cache storage supports multiget,
this method should be overridden to exploit that feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$keys` | `array` | A list of keys identifying the cached values |

**Returns**

`array`: A list of cached values indexed by the keys

### setValue() <span id="method-setValue"></span>

```php
abstract protected function setValue(string $key, mixed $value, int $duration): bool
```

Stores a value identified by a key in cache.

This method should be implemented by child classes to store the data
in specific cache storage.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$key` | `string` | The key identifying the value to be cached |
| `$value` | `mixed` | The value to be cached. Most often it's a string. If you have disabled [`$serializer`](/docs/api/yii/caching/cache/index.md#property-serializer), it could be something else. |
| `$duration` | `int` | The number of seconds in which the cached value will expire. 0 means never expire. |

**Returns**

`bool`: True if the value is successfully stored into cache, false otherwise

### setValues() <span id="method-setValues"></span>

```php
protected function setValues(array $data, int $duration): array
```

Stores multiple key-value pairs in cache.

The default implementation calls [`setValue()`](/docs/api/yii/caching/cache/index.md#method-setValue) multiple times store values one by one. If the underlying cache
storage supports multi-set, this method should be overridden to exploit that feature.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `array` | Array where key corresponds to cache key while value is the value stored |
| `$duration` | `int` | The number of seconds in which the cached values will expire. 0 means never expire. |

**Returns**

`array`: Array of failed keys