Final Class Yiisoft\Cache\Dependency\TagDependency
| Inheritance | Yiisoft\Cache\Dependency\TagDependency » Yiisoft\Cache\Dependency\Dependency |
|---|
TagDependency associates a cached value with one or multiple {@see TagDependency::$tags}.
By calling {@see \Yiisoft\Cache\Dependency\TagDependency::invalidate()}, you can invalidate all cached values that are associated with the specified tag name(s).
// setting multiple cache keys to store data forever and tagging them with "user-123"
$cache->getOrSet('user_42_profile', '', null, new TagDependency('user-123'));
$cache->getOrSet('user_42_stats', '', null, new TagDependency('user-123'));
// setting a cache key to store data and tagging them with "user-123" with the specified TTL for the tag
$cache->getOrSet('user_42_profile', '', null, new TagDependency('user-123', 3600));
// invalidating all keys tagged with "user-123"
TagDependency::invalidate($cache, 'user-123');
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $data | mixed | The dependency data that is saved in cache and later is compared with the latest dependency data. | Yiisoft\Cache\Dependency\Dependency |
| $isReusable | boolean | Whether this dependency is reusable or not. | Yiisoft\Cache\Dependency\Dependency |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Cache\Dependency\TagDependency | |
| evaluateDependency() | Evaluates the dependency by generating and saving the data related with dependency. | Yiisoft\Cache\Dependency\Dependency |
| invalidate() | Invalidates all of the cached values that are associated with any of the specified {@see tags}. | Yiisoft\Cache\Dependency\TagDependency |
| isChanged() | Yiisoft\Cache\Dependency\TagDependency | |
| markAsReusable() | Changes dependency behavior so dependent data for this cache dependency will be generated only once per request. | Yiisoft\Cache\Dependency\Dependency |
| resetReusableData() | Resets all cached data for reusable dependencies. | Yiisoft\Cache\Dependency\Dependency |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| generateDependencyData() | Yiisoft\Cache\Dependency\TagDependency | |
| generateReusableHash() | Generates a unique hash that can be used for retrieving reusable dependency data. | Yiisoft\Cache\Dependency\Dependency |
| iterableToArray() | Converts iterable to array. | Yiisoft\Cache\Dependency\Dependency |
Method Details
| public mixed __construct ( array|string $tags, integer|null $ttl = null ) | ||
| $tags | array|string |
List of tag names for this dependency. For a single tag, you may specify it as a string. |
| $ttl | integer|null |
The TTL value of this item. null means infinity. |
public function __construct(array|string $tags, int|null $ttl = null)
{
$this->tags = (array) $tags;
if ($ttl !== null && $ttl < 1) {
throw new InvalidArgumentException(
'TTL must be a positive number or null, to invalidate tags, use the'
. ' static `\Yiisoft\Cache\Dependency\TagDependency::invalidate()` method.',
);
}
$this->ttl = $ttl;
}
Defined in: Yiisoft\Cache\Dependency\Dependency::evaluateDependency()
Evaluates the dependency by generating and saving the data related with dependency.
This method is invoked by cache before writing data into it.
| public void evaluateDependency ( Yiisoft\Cache\CacheInterface $cache ) | ||
| $cache | Yiisoft\Cache\CacheInterface |
The cache component that is currently evaluating this dependency. |
public function evaluateDependency(CacheInterface $cache): void
{
if (!$this->isReusable) {
$this->data = $this->generateDependencyData($cache);
return;
}
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$reusableData)) {
self::$reusableData[$hash] = $this->generateDependencyData($cache);
}
$this->data = self::$reusableData[$hash];
}
| protected array generateDependencyData ( Yiisoft\Cache\CacheInterface $cache ) | ||
| $cache | Yiisoft\Cache\CacheInterface | |
protected function generateDependencyData(CacheInterface $cache): array
{
if (empty($this->tags)) {
return [];
}
$tags = [];
foreach ($this->getTagsData($cache) as $tag => $time) {
$tags[$tag] = $time ?? microtime();
}
$cache
->psr()
->setMultiple($tags, $this->ttl);
return $tags;
}
Defined in: Yiisoft\Cache\Dependency\Dependency::generateReusableHash()
Generates a unique hash that can be used for retrieving reusable dependency data.
See also \Yiisoft\Cache\Dependency\isReusable().
| protected string generateReusableHash ( ) | ||
| return | string |
A unique hash value for this cache dependency. |
|---|---|---|
protected function generateReusableHash(): string
{
$data = $this->data;
$this->data = null; // https://github.com/yiisoft/yii2/issues/3052
$key = sha1(serialize($this));
$this->data = $data;
return $key;
}
Invalidates all of the cached values that are associated with any of the specified {@see tags}.
| public static void invalidate ( Yiisoft\Cache\CacheInterface $cache, array|string $tags ) | ||
| $cache | Yiisoft\Cache\CacheInterface |
The cache component that caches the values. |
| $tags | array|string |
List of tag names. |
public static function invalidate(CacheInterface $cache, array|string $tags): void
{
$cache
->psr()
->deleteMultiple(self::buildCacheKeys((array) $tags));
}
| public boolean isChanged ( Yiisoft\Cache\CacheInterface $cache ) | ||
| $cache | Yiisoft\Cache\CacheInterface | |
public function isChanged(CacheInterface $cache): bool
{
if (empty($this->tags)) {
return $this->data !== [];
}
return $this->data !== $this->getTagsData($cache);
}
Defined in: Yiisoft\Cache\Dependency\Dependency::iterableToArray()
Converts iterable to array.
| protected array iterableToArray ( iterable $iterable ) | ||
| $iterable | iterable | |
protected function iterableToArray(iterable $iterable): array
{
/** @psalm-suppress RedundantCast */
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
}
Defined in: Yiisoft\Cache\Dependency\Dependency::markAsReusable()
Changes dependency behavior so dependent data for this cache dependency will be generated only once per request.
This allows you to use the same cache dependency for multiple separate cache calls while generating the same page without an overhead of re-evaluating dependency data each time.
| public void markAsReusable ( ) |
public function markAsReusable(): void
{
$this->isReusable = true;
}
Defined in: Yiisoft\Cache\Dependency\Dependency::resetReusableData()
Resets all cached data for reusable dependencies.
| public static void resetReusableData ( ) |
public static function resetReusableData(): void
{
self::$reusableData = [];
}
Signup or Login in order to comment.