Cache

Reference for the Cache module: fetch a value or generate it once, keyed by module, item name and a set of dependencies. Values are held in per-request RAM and, when a timeout is set, in the database until they expire.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Cache - fetch a cached value or generate it once, in RAM and the database.
  • CacheData - one stored cache entry, its key, value and expiry.

Cache

The main class: fetch-or-generate caching. A key is the module name, the item name and the JSON of a dependencies array. Values are checked in RAM first, then the database; a miss runs the callback and stores the result.

ClearAllRamCache

Cache::ClearAllRamCache(): void

Clear the whole per-request RAM cache.

Returns void

ClearRamCache

Cache::ClearRamCache(string $moduleName, string $itemName, array $deps): void

Clear the RAM cache entry for one item and dependency set.

NameTypeDescription
$moduleNamestringModule name identifier.
$itemNamestringCache item name.
$depsarrayDependencies array; JSON encoded to form the key.

Returns void

Item

Cache::Item(int $timeoutSeconds, string $moduleName, string $itemName, array $deps, callable $callback): mixed

Fetch a cached item or generate it with the callback, storing the result. Returns the RAM or database hit when the dependencies match and it has not expired.

NameTypeDescription
$timeoutSecondsintTimeout in seconds; 0 uses RAM only, with no database storage.
$moduleNamestringModule name identifier.
$itemNamestringCache item name.
$depsarrayDependencies array; may contain objects or arrays and is JSON encoded into the key.
$callbackcallableFunction that generates the value when it is not cached.

Returns mixed The cached or generated value.

CacheData

A stored cache entry: the module and item name, the JSON dependencies and value, the expiry and added timestamps and the date. Its fields are read-only, set on construction.

Properties

NameTypeDescription
$idintThe row id, or 0 before it is stored.
$moduleNamestringModule name identifier.
$cacheNamestringCache item name.
$depsstringThe JSON encoded dependencies.
$datastringThe JSON encoded cached value.
$expiryTimestampintThe timestamp at which the entry expires.
$datestringThe database date the entry was stored.
$addedTimestampintThe timestamp the entry was first added.

__construct

CacheData::__construct(int $id = 0, string $moduleName = '', string $cacheName = '', string $deps = '', string $data = '', int $expiryTimestamp = 0, string $date = '', int $addedTimestamp = 0)

Create a cache entry from its fields; every argument has a default.

NameTypeDescription
$idintThe row id, or 0 before it is stored.
$moduleNamestringModule name identifier.
$cacheNamestringCache item name.
$depsstringThe JSON encoded dependencies.
$datastringThe JSON encoded cached value.
$expiryTimestampintThe timestamp at which the entry expires.
$datestringThe database date the entry was stored.
$addedTimestampintThe timestamp the entry was first added.

Returns CacheData The new instance.

FromJson

CacheData::FromJson(string $json): CacheData

Build a cache entry from its JSON representation.

NameTypeDescription
$jsonstringThe JSON to decode.

Returns CacheData The decoded instance.

GetDataSize

CacheData::GetDataSize(): int

The length of the stored JSON value.

Returns int The number of characters in the value.

ToJson

CacheData::ToJson(): string

Encode the cache entry as a JSON string.

Returns string The JSON representation.