Frontends
Frontends
Frontends serve as the primary interface between your application and the caching system, abstracting the underlying storage mechanisms. They define how data is stored, retrieved, and managed within the cache, providing a consistent API regardless of the backend in use. Each frontend is designed to address specific caching scenarios such as object caching, function result caching, or output buffering while remaining agnostic to the backend implementation. This modular approach allows you to pair any frontend with any supported backend, enabling flexible and efficient caching strategies tailored to your application's requirements.
Hazaar\Cache\Adapter
The basic cache frontend is the core frontend that all other frontends are built upon and is used to store key/value pairs. Keys are a unique text string to identify the cache record. Values can be strings, numbers, arrays and objects. For an object to be stored it MUST be serializable. For a class object to be serializable it must usually implement the __sleep() and __wakeup() methods.
Options
Available options are:
| Name | Data Type | Default Value | Description |
|---|---|---|---|
| lifetime | Integer | 7200 | The number of seconds after which cached data is considered stale and should be updated. Once this timeout is reached, calls to load() will return false. |
| prefix | String | null | A prefix for all cache keys, if set to null, no cache key prefix will be used. The cache key prefix essentially creates a namespace in the cache, allowing multiple applications or websites to use a shared cache. Each application or website can use a different cache key prefix so specific cache keys can be used more than once. |
| serialize | Boolean | false | If a backend can not store objects (such as the file backend), then the data will automatically be serialized. However, using this flag you can force the data to always be serialized and never stored as an object. |
| use_pragma | Boolean | true | If true, cache objects will check the pragma header and honour the no-cache parameter. Typically this means that if the user does a hard refresh on a page (ie: using CTRL-F5) then this cache will be ignored. If this is false, the pragma header will be ignored and the cache will always be used if available. |
Methods
get($key, $default = null, $saveDefault = false, $timeout = 0)
| Parameter | Data Type | Description |
|---|---|---|
| $key | String | The unique identifier for the cache record that should be returned. |
| $default | Mixed | Default value to return if no cache entry exists. |
| $saveDefault | Boolean | If true and no cache entry exists, store the default. |
| $timeout | Integer | Optional timeout for the saved default value. |
set($key, $data, $timeout = 0)
| Parameter | Data Type | Description |
|---|---|---|
| $key | String | The unique identifier for the cache record. |
| $data | Mixed | The data that should be stored in the cache backend. |
| $timeout | Integer | Optional timeout for this value. |
$cache = new Hazaar\Cache\Adapter('file', [
'ttl' => 300,
]);
if (($data = $cache->get('my_cache_key')) === false) {
$data = [];
for ($i = 0; $i < 1000; $i++) {
$data[$i] = uniqid('', true);
}
$cache->set('my_cache_key', $data);
}
# Do something with $data hereHazaar\Cache\Func
The func frontend caches the result of a function call as long as the result is an integer, string, boolean, array or serializable object.
Options
Available options are:
| Name | Data Type | Default Value | Description |
|---|---|---|---|
| cache_by_default | Boolean | true | If true, function calls will be cached by default. |
| cached_functions | Array | null | List of function names that will always be cached. |
| non_cached_functions | Array | null | List of function names that will never be cached. |
Methods
call($function, $args, [...])
| Parameter | Data Type | Description |
|---|---|---|
| $function | Funcref | A function reference. This is either a string to the function name (for global functions) or an array consisting of the object or class name as the first element, and the name of the function as the second element. See PHP Callbacks for more examples. |
| $args | Mixed | The second and subsequent arguments are passed untouched to the function being called. |
Calling a global function:
$cache = new Hazaar\Cache\Func('file', [
'ttl' => 60,
]);
$result = $cache->call('myFunction', 'a string', 1234);
# Do something with result hereCalling a class method:
$cache = new Hazaar\Cache\Func('file', [
'ttl' => 60,
]);
$result = $cache->call([$obj, 'myMethod'], 'a string', 1234);
# Do something with result here
Calling a closure:
```php
$cache = new Hazaar\Cache\Func('file', [
'ttl' => 30,
]);
$result = $cache->call(function (): string {
return uniqid('report_', true);
});
## [`Hazaar\Cache\Output`](/api/class/hazaar/cache/output)
The output frontend captures content from an output buffer based on a key. If the key is cached, it returns the cached content and you can skip rendering. If not, it starts buffering and returns false so you can generate content and persist it.
### Options
There are no extra options available with this frontend.
### Methods
#### `start($key)`
This tells the cache object to start redirecting the output buffer into the cache object.
|Parameter|Data Type|Description|
|---------|---------|-----------|
|$key|String|The unique identifier for the cache record that should be returned.|
#### `stop()`
This tells the cache object to stop redirecting the output buffer into the cache object and store the buffered data in the cache backend.
```php
$cache = new Hazaar\Cache\Output('file', [
'ttl' => 120,
]);
$cached = $cache->start('report:monthly');
if ($cached === false) {
echo "Hello world!";
echo "This is going to be cached!";
$cached = $cache->stop();
}
echo $cached;