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.
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.
| Name | Type | Description |
|---|---|---|
$moduleName | string | Module name identifier. |
$itemName | string | Cache item name. |
$deps | array | Dependencies 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.
| Name | Type | Description |
|---|---|---|
$timeoutSeconds | int | Timeout in seconds; 0 uses RAM only, with no database storage. |
$moduleName | string | Module name identifier. |
$itemName | string | Cache item name. |
$deps | array | Dependencies array; may contain objects or arrays and is JSON encoded into the key. |
$callback | callable | Function 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
| Name | Type | Description |
|---|---|---|
$id | int | The row id, or 0 before it is stored. |
$moduleName | string | Module name identifier. |
$cacheName | string | Cache item name. |
$deps | string | The JSON encoded dependencies. |
$data | string | The JSON encoded cached value. |
$expiryTimestamp | int | The timestamp at which the entry expires. |
$date | string | The database date the entry was stored. |
$addedTimestamp | int | The 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.
| Name | Type | Description |
|---|---|---|
$id | int | The row id, or 0 before it is stored. |
$moduleName | string | Module name identifier. |
$cacheName | string | Cache item name. |
$deps | string | The JSON encoded dependencies. |
$data | string | The JSON encoded cached value. |
$expiryTimestamp | int | The timestamp at which the entry expires. |
$date | string | The database date the entry was stored. |
$addedTimestamp | int | The 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.
| Name | Type | Description |
|---|---|---|
$json | string | The 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.