Skip to content
PHP Ecosystem
Explore

TagDependency

API reference › yii › caching › TagDependency

classyii\caching\TagDependency
ExtendsDependency » yii\base\BaseObject
Implementsyii\base\Configurable
Since2.0
Sourceframework/caching/TagDependency.php

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

By calling 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.

PropertyTypeDescriptionDefined by
$datamixedThe dependency data that is saved in cache and later is compared with the latest dependency data. Default: null.Dependency
$reusableboolWhether this dependency is reusable or not. Default: false.Dependency
$tagsstring|arrayA list of tag names for this dependency. Default: [].TagDependency
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
evaluateDependency()Evaluates the dependency by generating and saving the data related with dependency.Dependency
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
invalidate() staticInvalidates all of the cached data items that are associated with any of the specified $tags.TagDependency
isChanged()Checks whether the dependency is changed.TagDependency
resetReusableData() staticResets all cached data for reusable dependencies.Dependency
generateDependencyData() protectedGenerates the data needed to determine if dependency has been changed.TagDependency
generateReusableHash() protectedGenerates a unique hash that can be used for retrieving reusable dependency data.Dependency
getTimestamps() protectedReturns the timestamps for the specified tags.TagDependency
touchKeys() protected staticGenerates the timestamp for the specified cache keys.TagDependency
public string|array $tags = []

A list of tag names for this dependency.

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

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.

Parameters

ParameterTypeDescription
$cacheCacheInterfaceThe cache component that caches the data items
$tagsstring|array

Returns

mixed

public function isChanged(mixed $cache): bool

Checks whether the dependency is changed.

Parameters

ParameterTypeDescription
$cachemixedThe cache component that is currently evaluating this dependency

Returns

bool: Whether the dependency has changed.

Since

2.0.11

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

ParameterTypeDescription
$cacheCacheInterfaceThe cache component that is currently evaluating this dependency

Returns

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

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

Returns the timestamps for the specified tags.

Parameters

ParameterTypeDescription
$cacheCacheInterface
$tagsstring[]

Returns

array: The timestamps indexed by the specified tags.

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

Generates the timestamp for the specified cache keys.

Parameters

ParameterTypeDescription
$cacheCacheInterface
$keysstring[]

Returns

array: The timestamp indexed by cache keys