Adapter
Adapter
Cache frontend/facade for backend-agnostic key/value operations.
class Adapter implements \ArrayAccessImplements: ArrayAccess
This adapter selects an available cache backend, normalizes common cache operations (get, set, has, remove, clear, etc.), and provides convenience integration through magic property access and ArrayAccess.
It also supports optional object serialization fallback for backends that cannot store PHP objects natively, pragma-driven cache bypass, and key exclusion when exporting cache contents.
Properties
options
protected array $optionsType: array
settings
Typed view of this adapter's own options (ttl, use_pragma, keepalive).
protected Adapter\Options $settingsType: Hazaar\Cache\Adapter\Options
useCache
Flag controlling read-side cache usage.
protected bool $useCache = trueType: bool
backend
Active backend instance handling low-level cache operations.
protected Backend $backendType: Hazaar\Cache\Backend
excludeKeys
private array $excludeKeysType: array
Methods
__construct
Cache object constructor.
public __construct(array|string|null $backend, array $config, string $namespace = 'default'): voidParameters
| Parameter | Type | Description |
|---|---|---|
$backend | array | string |
$config | array | |
$namespace | string | The namespace to use for grouping stored data |
__destruct
Destructor method for the cache adapter.
public __destruct(): voidCloses the backend connection when the adapter is destroyed.
__isset
Checks whether a cache key exists via property-style access.
public __isset(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | cache key |
__set
Sets a cache value via property-style assignment.
public __set(string $key, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | cache key |
$value | mixed | value to store |
__unset
Removes a cache key via property-style unset.
public __unset(string $key): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | cache key |
getBackend
Returns the active cache backend instance.
public getBackend(): BackendReturns: Hazaar\Cache\Backend
setBackendOption
Updates a single backend option at runtime.
public setBackendOption(string $key, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | backend option key |
$value | mixed | backend option value |
lock
Acquires a backend lock for the specified cache key.
public lock(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | lock key |
unlock
Releases a backend lock for the specified cache key.
public unlock(string $key): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | lock key |
exclude
Excludes the specified keys from some cache operations.
public exclude(array|string $keys): voidReturns: void
Accepts a variable number of arguments, which can be individual keys or arrays of keys. If an argument is an array, it recursively processes its elements. The final list of excluded keys is stored in the $excludeKeys property.
Parameters
| Parameter | Type | Description |
|---|---|---|
$keys | array | string |
get
Retrieve a value from cache storage.
public get(string $key, mixed $default, bool $saveDefault, int $timeout): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The reference key used to store the value |
$default | mixed | If the value doesn't exist, this default will be returned instead |
$saveDefault | bool | If the value doesn't exist and a default is specified, save that default to cache |
$timeout | int |
set
Store a value in the cache using the current cache backend.
public set(string $key, mixed $value, int $timeout): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The reference key under which to store the value |
$value | mixed | The value that should be stored. Values can be pretty much anything including integers, booleans, strings and any object that can be serialised. |
$timeout | int | The number of seconds after which the value becomes invalid. If not set the global 'ttl' option is used. Set a value of '-1' to indicate that the value should never timeout. |
has
Check if a stored value exists.
public has(string $key, bool $checkEmpty): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The value key to check for |
$checkEmpty | bool | Normally this method will return try if the value exists with $key. Setting $checkEmpty looks at the value and will return false if it is an 'empty' value (ie: 0, null, []) |
remove
Removes a stored value.
public remove(string $key): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key of the value to remove |
extend
Extend the cache with an array of key/value pairs.
public extend(array $array, bool $recursive): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array of key/value pairs to store in the cache |
$recursive | bool |
clear
Clears all values from the active backend namespace.
public clear(): voidReturns: void
populate
Set multiple values in the cache.
public populate(array $values): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$values | array |
pull
Retrieves and removes a value in a single operation.
public pull(string $key): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | cache key |
toArray
Retrieve all values from the cache.
public toArray(): ?arrayReturns: array
count
Returns the number of entries in the active backend namespace.
public count(): intReturns: int
on
Disable the use of cache.
public on(): voidReturns: void
This allows a cache object to be disabled but still be used without having to modify existing application code. The application can still call the get() method but it will always return false, simulating a negative cache response. Calling set() will still have an affect however.
off
Enable the use of cache.
public off(): voidReturns: void
Cache is enabled by default. This is to allow cache to be re-enabled after calling off().
This method can also be used to force cache back on after being disabled by a "Pragma: no-cache" header in the case where the use_pragma setting is enabled (which is the default).
__get
Retrieves a cache value via property-style access.
public __get(string $key): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | cache key |
offsetExists
public offsetExists(string $offset): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | string |
offsetGet
public offsetGet(string $offset): mixedReturns: mixed
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | string |
offsetSet
public offsetSet(string $offset, mixed $value): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | string | |
$value | mixed |
offsetUnset
public offsetUnset(string $offset): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$offset | string |
increment
Increment key value.
public increment(string $key, int $amount = 1): intReturns: int
This method will increment a cached integer value by a defined amount (default is 1). Once the value is incremented it will be stored back in the cache and the new value returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The cache key |
$amount | int | the amount to increment the value by |
decrement
Decrement key value.
public decrement(string $key, int $amount = 1): intReturns: int
This method will decrement a cached integer value by a defined amount (default is 1). Once the value is decremented it will be stored back in the cache and the new value returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The cache key |
$amount | int | the amount to decrement the value by |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:16 +0000