Abstract Class Yiisoft\Cache\Dependency\Dependency
Dependency is the base class for cache dependency classes.
Child classes should override its {@see \Yiisoft\Cache\Dependency\Dependency::generateDependencyData()} for generating the actual dependency data.
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 |
|---|---|---|
| evaluateDependency() | Evaluates the dependency by generating and saving the data related with dependency. | Yiisoft\Cache\Dependency\Dependency |
| isChanged() | Checks whether the dependency is changed. | Yiisoft\Cache\Dependency\Dependency |
| 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() | Generates the data needed to determine if dependency is changed. | Yiisoft\Cache\Dependency\Dependency |
| 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 |
Property Details
The dependency data that is saved in cache and later is compared with the latest dependency data.
Whether this dependency is reusable or not. True value means that 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. Defaults to false.
Method Details
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];
}
Generates the data needed to determine if dependency is changed.
Derived classes should override this method to generate the actual dependency data.
| protected abstract mixed generateDependencyData ( Yiisoft\Cache\CacheInterface $cache ) | ||
| $cache | Yiisoft\Cache\CacheInterface |
The cache component that is currently evaluating this dependency. |
| return | mixed |
The data needed to determine if dependency has been changed. |
|---|---|---|
abstract protected function generateDependencyData(CacheInterface $cache): mixed;
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;
}
Checks whether the dependency is changed.
| public boolean isChanged ( Yiisoft\Cache\CacheInterface $cache ) | ||
| $cache | Yiisoft\Cache\CacheInterface |
The cache component that is currently evaluating this dependency |
| return | boolean |
Whether the dependency has changed. |
|---|---|---|
public function isChanged(CacheInterface $cache): bool
{
if (!$this->isReusable) {
return $this->data !== $this->generateDependencyData($cache);
}
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$reusableData)) {
self::$reusableData[$hash] = $this->generateDependencyData($cache);
}
return $this->data !== self::$reusableData[$hash];
}
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;
}
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;
}
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.