0 follower

Final Class Yiisoft\Cache\Dependency\ValueDependency

InheritanceYiisoft\Cache\Dependency\ValueDependency » Yiisoft\Cache\Dependency\Dependency

ValueDependency represents a dependency based on the specified value in the constructor.

The dependency is reported as unchanged if and only if the specified value is the same as the one evaluated when storing the data to cache.

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
__construct() Yiisoft\Cache\Dependency\ValueDependency
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() Yiisoft\Cache\Dependency\ValueDependency
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

Hide inherited methods

__construct() public method

public mixed __construct ( mixed $value )
$value mixed

                public function __construct(private readonly mixed $value)
{
}

            
evaluateDependency() public method

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

            
generateDependencyData() protected method

protected mixed generateDependencyData ( Yiisoft\Cache\CacheInterface $cache )
$cache Yiisoft\Cache\CacheInterface

                protected function generateDependencyData(CacheInterface $cache): mixed
{
    return $this->value;
}

            
generateReusableHash() protected method

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

            
isChanged() public method

Defined in: Yiisoft\Cache\Dependency\Dependency::isChanged()

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

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

            
markAsReusable() public method

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

            
resetReusableData() public static method

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