Cache

File caching system with per-instance directory path and file prefix

Namespace: Com\Tecnick\File

class Cache

Source: src/Cache.php:34

File caching system with per-instance directory path and file prefix

Methods

__construct()

Initialize the cache directory and file prefix.

public __construct(?string $prefix = null)

Parameters:

  • $prefix (?string): Common prefix for all cache files; null draws a random one.

Throws:

  • Exception: when the cache directory cannot be resolved, or no secure randomness is available.

Source: src/Cache.php:73

delete()

Delete cached files

A file name is built as prefix + type + key, so a key can only be matched once a type narrows the name, and a key without a type is rejected.

public delete(?string $type = null, ?string $key = null): void

Parameters:

  • $type (?string): Type of files to delete; null means every type.
  • $key (?string): Specific file key to delete; requires $type.

Throws:

Source: src/Cache.php:210

deleteOlderThan()

Delete cache files older than the given number of seconds.

public deleteOlderThan(int $seconds): void

Parameters:

  • $seconds (int): Maximum age in seconds; files whose mtime is older are removed. Must not be negative.

Throws:

Source: src/Cache.php:285

getCachePath()

Get the cache directory path

public getCachePath(): string

Returns: string

Source: src/Cache.php:97

getFilePrefix()

Get the file prefix

public getFilePrefix(): string

Returns: string

Source: src/Cache.php:130

getNewFileName()

Returns a temporary filename for caching files.

The returned name carries a random suffix, so it cannot be reconstructed from $type and $key: the caller is responsible for storing it.

public getNewFileName(string $type = 'tmp', string $key = '0'): string

Parameters:

  • $type (string): Type of file (used to scope delete())
  • $key (string): File key (used to scope delete())

Returns: string: Temporary filename

Throws:

  • Exception: when a cache file cannot be created, or no secure randomness is available.

Source: src/Cache.php:149

setCachePath()

Set the default cache directory path

Falls back to K_PATH_CACHE when $path is null, names a stream wrapper, or is not a writable directory. The fallback is silent: compare getCachePath() against the argument to detect it.

public setCachePath(?string $path = null): static

Parameters:

  • $path (?string): Cache directory path; if null use the K_PATH_CACHE value

Returns: static

Throws:

  • Exception: when the resulting directory cannot be resolved.

Source: src/Cache.php:113