# TagDependency

> TagDependency associates a cached data item with one or multiple $tags.

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

| class | `yii\caching\TagDependency` |
| --- | --- |
| Extends | [`Dependency`](/docs/api/yii/caching/dependency/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/caching/TagDependency.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/caching/TagDependency.php#L30) |

TagDependency associates a cached data item with one or multiple [`$tags`](/docs/api/yii/caching/tagdependency/index.md#property-tags).

By calling [`invalidate()`](/docs/api/yii/caching/tagdependency/index.md#method-invalidate), you can invalidate all cached data items that are associated with the specified tag name(s).

```
// setting multiple cache keys to store data forever and tagging them with "user-123"
Yii::$app->cache->set('user_42_profile', '', 0, new TagDependency(['tags' => 'user-123']));
Yii::$app->cache->set('user_42_stats', '', 0, new TagDependency(['tags' => 'user-123']));

// invalidating all keys tagged with "user-123"
TagDependency::invalidate(Yii::$app->cache, 'user-123');
```

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-data"></span>[`$data`](/docs/api/yii/caching/dependency/index.md#property-data) | `mixed` | The dependency data that is saved in cache and later is compared with the latest dependency data. Default: `null`. | [`Dependency`](/docs/api/yii/caching/dependency/index.md) |
| <span id="property-reusable"></span>[`$reusable`](/docs/api/yii/caching/dependency/index.md#property-reusable) | `bool` | Whether this dependency is reusable or not. Default: `false`. | [`Dependency`](/docs/api/yii/caching/dependency/index.md) |
| [`$tags`](#property-tags) | `string\|array` | A list of tag names for this dependency. Default: `[]`. | `TagDependency` |

## 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/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/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) |
| <span id="method-evaluateDependency"></span>[`evaluateDependency()`](/docs/api/yii/caching/dependency/index.md#method-evaluateDependency) | Evaluates the dependency by generating and saving the data related with dependency. | [`Dependency`](/docs/api/yii/caching/dependency/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`invalidate()`](#method-invalidate) static | Invalidates all of the cached data items that are associated with any of the specified [`$tags`](/docs/api/yii/caching/tagdependency/index.md#property-tags). | `TagDependency` |
| [`isChanged()`](#method-isChanged) | Checks whether the dependency is changed. | `TagDependency` |
| <span id="method-resetReusableData"></span>[`resetReusableData()`](/docs/api/yii/caching/dependency/index.md#method-resetReusableData) static | Resets all cached data for reusable dependencies. | [`Dependency`](/docs/api/yii/caching/dependency/index.md) |
| [`generateDependencyData()`](#method-generateDependencyData) protected | Generates the data needed to determine if dependency has been changed. | `TagDependency` |
| <span id="method-generateReusableHash"></span>[`generateReusableHash()`](/docs/api/yii/caching/dependency/index.md#method-generateReusableHash) protected | Generates a unique hash that can be used for retrieving reusable dependency data. | [`Dependency`](/docs/api/yii/caching/dependency/index.md) |
| [`getTimestamps()`](#method-getTimestamps) protected | Returns the timestamps for the specified tags. | `TagDependency` |
| [`touchKeys()`](#method-touchKeys) protected static | Generates the timestamp for the specified cache keys. | `TagDependency` |

## Property details

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

```php
public string|array $tags = []
```

A list of tag names for this dependency.

For a single tag, you may specify it as a string.

## Method details

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

```php
public static function invalidate(yii\caching\CacheInterface $cache, string|array $tags): mixed
```

Invalidates all of the cached data items that are associated with any of the specified [`$tags`](/docs/api/yii/caching/tagdependency/index.md#property-tags).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cache` | [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md) | The cache component that caches the data items |
| `$tags` | `string\|array` |  |

**Returns**

`mixed`

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

```php
public function isChanged(mixed $cache): bool
```

Checks whether the dependency is changed.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cache` | `mixed` | The cache component that is currently evaluating this dependency |

**Returns**

`bool`: Whether the dependency has changed.

**Since**

2.0.11

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

```php
protected function generateDependencyData(yii\caching\CacheInterface $cache): mixed
```

Generates the data needed to determine if dependency has been changed.

This method does nothing in this class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cache` | [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md) | The cache component that is currently evaluating this dependency |

**Returns**

`mixed`: The data needed to determine if dependency has been changed.

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

```php
protected function getTimestamps(yii\caching\CacheInterface $cache, string[] $tags): array
```

Returns the timestamps for the specified tags.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cache` | [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md) |  |
| `$tags` | `string[]` |  |

**Returns**

`array`: The timestamps indexed by the specified tags.

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

```php
protected static function touchKeys(yii\caching\CacheInterface $cache, string[] $keys): array
```

Generates the timestamp for the specified cache keys.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cache` | [`CacheInterface`](/docs/api/yii/caching/cacheinterface/index.md) |  |
| `$keys` | `string[]` |  |

**Returns**

`array`: The timestamp indexed by cache keys