0 follower

Abstract Class Yiisoft\Cache\Dependency\Dependency

InheritanceYiisoft\Cache\Dependency\Dependency
SubclassesYiisoft\Cache\Dependency\AllDependencies, Yiisoft\Cache\Dependency\AnyDependency, Yiisoft\Cache\Dependency\CallbackDependency, Yiisoft\Cache\Dependency\FileDependency, Yiisoft\Cache\Dependency\TagDependency, Yiisoft\Cache\Dependency\ValueDependency

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

Hide inherited 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

Hide inherited 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

Hide inherited 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

Hide inherited properties

$data protected property

The dependency data that is saved in cache and later is compared with the latest dependency data.

protected mixed $data null
$isReusable protected property

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.

protected boolean $isReusable false

Method Details

Hide inherited methods

evaluateDependency() public method

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];
}

            
generateDependencyData() protected abstract method

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;

            
generateReusableHash() protected method

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;
}

            
isChanged() public method

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];
}

            
iterableToArray() protected method

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;
}

            
markAsReusable() public method

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;
}

            
resetReusableData() public static method

Resets all cached data for reusable dependencies.

public static void resetReusableData ( )

                public static function resetReusableData(): void
{
    self::$reusableData = [];
}