Func
Func
Class Func.
This class extends the Cache class and provides functionality to cache function calls.
- Full name:
\Hazaar\Cache\Func
- Parent class:
\Hazaar\Cache
Methods
__construct
Cache object constructor.
public __construct(?string $backend = null, array|\Hazaar\Map $options = []): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$backend | ?string | The name of the backend to use. Currently 'apc', 'file', 'memcached', 'session' and 'sqlite' are supported. |
$options | array|\Hazaar\Map |
call
Calls a function with the provided arguments and caches the result based on the configuration.
public call(): mixed
This method retrieves the function name and its arguments, generates a cache key, and checks whether the function result should be cached based on the options provided. If caching is enabled and the result is found in the cache, it returns the cached result. Otherwise, it calls the function, caches the result, and then returns it.
Return Value:
the result of the function call, either from cache or from the actual function execution
Throws:
if no function name is provided in the arguments
generateKey
Generates a unique key string for a given function and its arguments.
private generateKey((mixed|string)[]|string $function, array $paramArray): string
Parameters:
Parameter | Type | Description |
---|---|---|
$function | (mixed|string)[]|string | The function to generate a key for. Can be a string representing the function name or an array with the object and method name. |
$paramArray | array | the array of parameters to be passed to the function |
Return Value:
the generated unique key string
Throws:
if the callback declaration is unsupported
Inherited methods
__construct
Cache object constructor.
public __construct(string[]|string $backend = null, array|\Hazaar\Map $configOptions = [], string $namespace = 'default'): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$backend | string[]|string | The name of the backend to use. Currently 'apc', 'file', 'memcached', 'session' and 'sqlite' are supported. |
$configOptions | array|\Hazaar\Map | |
$namespace | string | The namespace to use for grouping stored data |
__destruct
public __destruct(): mixed
__isset
public __isset(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
__set
public __set(string $key, mixed $value): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | |
$value | mixed |
__unset
public __unset(string $key): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
can
public can(string $feature): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$feature | string |
configure
public configure(array|\Hazaar\Map $options): void
Parameters:
Parameter | Type | Description |
---|---|---|
$options | array|\Hazaar\Map |
getBackend
public getBackend(): \Hazaar\Cache\Backend
setBackendOption
public setBackendOption(string $key, mixed $value): void
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | |
$value | mixed |
lock
public lock(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
unlock
public unlock(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
get
Retrieve a value from cache storage.
public get(string $key, mixed $default = null, bool $saveDefault = false, int $timeout): 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 |
Return Value:
the value that was stored in cache
set
Store a value in the cache using the current cache backend.
public set(string $key, mixed $value, int $timeout): 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 'lifetime' 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 = false): 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 valueand will return false if it is an 'empty' value (ie: 0, null, []) |
remove
Removes a stored value.
public remove(string $key): 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 = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | The array of key/value pairs to store in the cache |
$recursive | bool |
clear
public clear(): void
populate
Set multiple values in the cache.
public populate(array $values): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$values | array |
pull
public pull(string $key): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
toArray
Retrieve all values from the cache.
public toArray(): array
count
public count(): int
on
Disable the use of cache.
public on(): 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(): 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
public __get(string $key): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string |
offsetExists
public offsetExists(mixed $offset): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetGet
public offsetGet(mixed $offset): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
offsetSet
public offsetSet(mixed $offset, mixed $value): void
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed | |
$value | mixed |
offsetUnset
public offsetUnset(mixed $offset): void
Parameters:
Parameter | Type | Description |
---|---|---|
$offset | mixed |
increment
Increment key value.
public increment(string $key, int $amount = 1): 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 |
Return Value:
The new incremented value
decrement
Decrement key value.
public decrement(string $key, int $amount = 1): 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 |
Return Value:
The new decremented value
Automatically generated on 2024-11-14