Documentation

Class: Cache

Full name: Clarity\Engine\Cache

Cache for compiled Clarity template classes.

Each template has one compiled PHP file at <bucket>/<hash>.php, where the hash is derived from its name (templateHash()). The class stores its compiler version, policy digest, debug flag, and dependency revisions.

Identical output is not rewritten, preserving the shared OPcache entry. The comparison runs during compilation and adds no work to rendering.

Public methods

templateHash() ·

public static function templateHash(string $templateName): string

Return the 32-character xxh128 digest used in cache paths.

This stable hash is for cache naming, not collision resistance.

Parameters

Name Type Default Description
$templateName string -

Return value

  • Type: string

__construct() ·

public function __construct(string $path = ''): mixed

Parameters

Name Type Default Description
$path string ''

Return value

  • Type: mixed

setPath() ·

public function setPath(string $path): static

Change the cache directory and clear per-template in-process state.

Parameters

Name Type Default Description
$path string -

Return value

  • Type: static

getPath() ·

public function getPath(): string

Return value

  • Type: string

setRevalidateFreq() ·

public function setRevalidateFreq(int $seconds): static

Set how many seconds a fresh verdict may be reused.

A value of 0 checks the filesystem on every render.

Parameters

Name Type Default Description
$seconds int - Seconds; negative values are treated as 0.

Return value

  • Type: static

getRevalidateFreq() ·

public function getRevalidateFreq(): int

Number of seconds a fresh verdict is reused for.

Return value

  • Type: int

isFresh() ·

public function isFresh(string $templateName, callable $revisionFor): bool

Check whether a compiled template exists, uses the current compiler version, and has unchanged dependency revisions.

A positive verdict is reused for $revalidateFreq seconds when enabled.

Parameters

Name Type Default Description
$templateName string - Logical template name.
$revisionFor callable - Returns the current revision for a dependency.

Return value

  • Type: bool

load() ·

public function load(string $templateName): string|null

Return the class name for a loaded or loadable compiled template.

Parameters

Name Type Default Description
$templateName string - Logical template name.

Return value

  • Type: string|null
  • Description: Null if no cache file exists.

writeAndLoad() ·

public function writeAndLoad(string $templateName, Clarity\Engine\CompiledTemplate $compiled): string

Write the compiled template to disk and return its class name.

Write the file even if its class is already loaded, since another cache directory may need its own copy. Leave identical content untouched to preserve the OPcache entry.

Parameters

Name Type Default Description
$templateName string - Logical template name.
$compiled CompiledTemplate - Result from the compiler.

Return value

  • Type: string
  • Description: The class name that is now live in memory.

cacheFilePath() ·

public function cacheFilePath(string $templateName): string

Return the cache file path for a template.

The first two hash characters select one of up to 256 subdirectories.

Parameters

Name Type Default Description
$templateName string -

Return value

  • Type: string

invalidate() ·

public function invalidate(string $templateName): void

Delete a template's cache file and clear its in-process mappings.

Parameters

Name Type Default Description
$templateName string -

Return value

  • Type: void

flush() ·

public function flush(): void

Delete cached files and clear per-template in-process mappings.

Return value

  • Type: void

getLoadedClassName() ·

public function getLoadedClassName(string $templateName): string|null

Return the loaded class name for a template, or null if it is not loaded.

Parameters

Name Type Default Description
$templateName string -

Return value

  • Type: string|null

getLoadedCodeFile() ·

public function getLoadedCodeFile(string $templateName): string|null

Return the loaded compiled file path for a template, or null.

Used to map errors to their template source.

Parameters

Name Type Default Description
$templateName string -

Return value

  • Type: string|null

Back to the Index