0 follower

Final Class Yiisoft\Cache\Dependency\FileDependency

InheritanceYiisoft\Cache\Dependency\FileDependency » Yiisoft\Cache\Dependency\Dependency

FileDependency represents a dependency based on a file's last modification time.

If the last modification time of the file specified via \Yiisoft\Cache\Dependency\FileDependency::$fileName is changed, the dependency is considered as changed.

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\FileDependency
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\FileDependency
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 __construct( string $fileName ): mixed
$fileName string

The file path whose last modification time is used to check if the dependency has been changed.

                public function __construct(private readonly string $fileName) {}

            
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 evaluateDependency( Yiisoft\Cache\CacheInterface $cache ): void
$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 generateDependencyData( Yiisoft\Cache\CacheInterface $cache ): false|integer
$cache Yiisoft\Cache\CacheInterface

                protected function generateDependencyData(CacheInterface $cache): false|int
{
    clearstatcache(false, $this->fileName);
    return @filemtime($this->fileName);
}

            
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 generateReusableHash( ): string
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 isChanged( Yiisoft\Cache\CacheInterface $cache ): boolean
$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 iterableToArray( iterable $iterable ): array
$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 markAsReusable( ): void

                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 resetReusableData( ): void

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