Skip to content

Internals Vfs Cache CachePool

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

vfs\cache\CachePool

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/vfs/cache/CachePool.php · Snapshot 8411f4287706.

VFS includes package resource lookup, database-backed file metadata, filesystem collaborators, transient pools and persistent PHP-array caches.

Imports and dependencies

use vfs\cache\enums\ECacheValidation;
use vfs\transient\enums\ETransientType;
use vfs\transient\interfaces\ITransientStorage;
use vfs\transient\TransientStorage;

CachePool

Declaration and architectural role

class CachePool

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$pool public string $pool; Used by __construct().
$watched public array $watched = []; Used by __construct(), createConfig(), setCache().
$cacheValidation public ECacheValidation $cacheValidation = ECacheValidation::NONE; Used by __construct(), createConfig(), setCache().
$transientStorage private ITransientStorage $transientStorage; Used by __construct(), createConfig(), setCache(), getCache(), deleteCache().
Constructor-promoted public function __construct(string $pool) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(string $pool)
createConfig public function createConfig(ECacheValidation $validation, array $watched): self
cacheFileName private function cacheFileName(string $name): string
setCache public function setCache(string $name, array $data): CacheEntry
getCache public function getCache(string $name): bool|array
deleteCache public function deleteCache(string $name, bool $missingIsSuccess = false): bool
validate private function validate(array $data): bool
snapshot private function snapshot(array $paths, ECacheValidation $mode): array
watch private function watch(string $path, ECacheValidation $mode, array &$result, bool $root = false): void

__construct

Source line 17

public function __construct(string $pool)

Parameters

Parameter Type, modifiers and default Purpose
$pool string $pool Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->pool, $this->transientStorage, $this->cacheValidation, $this->watched.

Implementation, validation guards and interaction order
public function __construct(string $pool)
    {
        $this->pool = $pool;
        $this->transientStorage = (new TransientStorage())->setPool('cache')->setSubPool($pool);
        $config = $this->transientStorage->getFile('config.json');
        if ($config !== null) {
            $config->loadContent();
            $data = is_string($config->content) ? json_decode($config->content, true) : null;
            if (is_array($data) && is_string($data['validation'] ?? null)) {
                $this->cacheValidation = ECacheValidation::tryFrom($data['validation']) ?? ECacheValidation::NONE;
                $this->watched = array_values(array_filter(is_array($data['watched'] ?? null) ? $data['watched'] : [], 'is_string'));
            }
        }
    }

createConfig

Source line 32

public function createConfig(ECacheValidation $validation, array $watched): self

Parameters

Parameter Type, modifiers and default Purpose
$validation ECacheValidation $validation Operation input; the exact use, validation and propagation are shown in the implementation trace.
$watched array $watched Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->cacheValidation, $this->watched.

Return expressions (including local callbacks): $this.

Explicit exceptions: \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
public function createConfig(ECacheValidation $validation, array $watched): self
    {
        foreach ($watched as $path) {
            if (!is_string($path) || $path === '') throw new \InvalidArgumentException('Watched paths must be nonempty strings.');
        }
        $this->cacheValidation = $validation;
        $this->watched = array_values(array_unique($watched));
        $this->transientStorage->createFile('config', ETransientType::JSON, json_encode([
            'validation' => $validation->value, 'watched' => $this->watched,
        ], JSON_THROW_ON_ERROR));
        return $this;
    }

cacheFileName

Source line 45

private function cacheFileName(string $name): string

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): hash('sha256', $name).

Implementation, validation guards and interaction order
private function cacheFileName(string $name): string { return hash('sha256', $name); }

setCache

Source line 47

public function setCache(string $name, array $data): CacheEntry

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.
$data array $data Input data array or payload; see allowed fields and validation guards below.

Return expressions (including local callbacks): new CacheEntry($name, $data, $file).

Implementation, validation guards and interaction order
public function setCache(string $name, array $data): CacheEntry
    {
        $payload = ['version' => 2, 'time' => time(), 'validation' => $this->cacheValidation->value,
            'roots' => $this->watched, 'watched' => $this->snapshot($this->watched, $this->cacheValidation), 'data' => $data];
        $file = $this->transientStorage->createFile($this->cacheFileName($name), ETransientType::PHP, $payload);
        return new CacheEntry($name, $data, $file);
    }

getCache

Source line 55

public function getCache(string $name): bool|array

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): false; $data['data'].

Failure handling catches \Throwable.

External effects include include.

Implementation, validation guards and interaction order
public function getCache(string $name): bool|array
    {
        $entry = $this->transientStorage->getFile($this->cacheFileName($name) . '.php');
        if ($entry === null) return false;
        try {
            $data = @include $entry->getPath();
            if (is_array($data) && $this->validate($data)) return $data['data'];
        } catch (\Throwable $e) {
            // A corrupt or obsolete cache is a miss; application data is rebuilt.
        }
        $this->transientStorage->deleteFile($entry);
        return false;
    }

deleteCache

Source line 69

public function deleteCache(string $name, bool $missingIsSuccess = false): bool

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.
$missingIsSuccess bool $missingIsSuccess = false Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): $entry === null ? $missingIsSuccess : $this->transientStorage->deleteFile($entry).

Implementation, validation guards and interaction order
public function deleteCache(string $name, bool $missingIsSuccess = false): bool
    {
        $entry = $this->transientStorage->getFile($this->cacheFileName($name) . '.php');
        return $entry === null ? $missingIsSuccess : $this->transientStorage->deleteFile($entry);
    }

validate

Source line 75

private function validate(array $data): bool

Parameters

Parameter Type, modifiers and default Purpose
$data array $data Input data array or payload; see allowed fields and validation guards below.

Return expressions (including local callbacks): false; match ($mode) { ECacheValidation::NONE => true, ECacheValidation::EXPIRE => time() - $data['time'] < 3600, ECacheValidation::EXPIRE_LONGER => time() - $data['time'] < 86400, ECacheValidation::MODIFIED, ECacheValidation::HASH => is_array($data['roots'] ?? null) && is_array($data['watched'] ?? null) && $this->snapshot($data['roots'], $mode) === $data['watched'], }.

Implementation, validation guards and interaction order
private function validate(array $data): bool
    {
        if (($data['version'] ?? null) !== 2 || !is_array($data['data'] ?? null)
            || !is_int($data['time'] ?? null) || !is_string($data['validation'] ?? null)) return false;
        $mode = ECacheValidation::tryFrom($data['validation']);
        if ($mode === null) return false;
        return match ($mode) {
            ECacheValidation::NONE => true,
            ECacheValidation::EXPIRE => time() - $data['time'] < 3600,
            ECacheValidation::EXPIRE_LONGER => time() - $data['time'] < 86400,
            ECacheValidation::MODIFIED, ECacheValidation::HASH =>
                is_array($data['roots'] ?? null) && is_array($data['watched'] ?? null)
                && $this->snapshot($data['roots'], $mode) === $data['watched'],
        };
    }

snapshot

Source line 91

private function snapshot(array $paths, ECacheValidation $mode): array

Parameters

Parameter Type, modifiers and default Purpose
$paths array $paths Operation input; the exact use, validation and propagation are shown in the implementation trace.
$mode ECacheValidation $mode Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): []; $result.

Implementation, validation guards and interaction order
private function snapshot(array $paths, ECacheValidation $mode): array
    {
        if ($mode !== ECacheValidation::MODIFIED && $mode !== ECacheValidation::HASH) return [];
        $result = [];
        foreach ($paths as $path) $this->watch($path, $mode, $result, true);
        ksort($result);
        return $result;
    }

watch

Source line 100

private function watch(string $path, ECacheValidation $mode, array &$result, bool $root = false): void

Parameters

Parameter Type, modifiers and default Purpose
$path string $path Filesystem or route path as defined by the owning component; no universal path normalization is implied.
$mode ECacheValidation $mode Operation input; the exact use, validation and propagation are shown in the implementation trace.
$result array &$result Operation input; the exact use, validation and propagation are shown in the implementation trace.
$root bool $root = false Operation input; the exact use, validation and propagation are shown in the implementation trace.

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
private function watch(string $path, ECacheValidation $mode, array &$result, bool $root = false): void
    {
        clearstatcache(true, $path);
        if (is_link($path)) {
            $result[$path] = ['link' => readlink($path), 'target' => realpath($path)];
            // Do not recurse through directory links: package trees may contain cycles.
            if (!is_file($path) && !$root) return;
        }
        if (is_dir($path)) {
            $names = @scandir($path);
            if ($names === false) throw new \RuntimeException("Cannot scan watched directory: {$path}");
            $names = array_values(array_diff($names, ['.', '..']));
            $result[$path] = ($result[$path] ?? []) + ['directory' => $names];
            foreach ($names as $name) $this->watch(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $name, $mode, $result);
        } elseif (is_file($path)) {
            $stat = @stat($path);
            if ($stat === false) throw new \RuntimeException("Cannot stat watched file: {$path}");
            $signature = ['mtime' => $stat['mtime'], 'ctime' => $stat['ctime'], 'size' => $stat['size']];
            if ($mode === ECacheValidation::HASH) {
                $hash = @hash_file('sha256', $path);
                if ($hash === false) throw new \RuntimeException("Cannot hash watched file: {$path}");
                $signature['hash'] = $hash;
            }
            $result[$path] = ($result[$path] ?? []) + $signature;
        } else $result[$path] = ['missing' => true];
    }

Related types

Clone this wiki locally