DbCache
in package
Provides caching functionality for database query results.
Tags
Table of Contents
- $action : int
- Defines the cache loading behavior.
- $db_cache : FileCache
- Cache driver instance.
- $instance : DbCache
- Singleton instance of the class.
- cacheOn() : void
- Enable caching.
- clear() : bool|array<string|int, mixed>
- Clear the cache.
- create() : DbCache
- Create an instance of the class (Singleton).
- get() : mixed
- Get data from the cache.
- getAction() : int
- Get the current cache action.
- init() : CacheItem
- Initialize a cache item based on the SQL query and its values.
- save() : bool
- Save the cache item with the provided data.
- setAction() : DbCache
- Set the cache action.
- usedCache() : bool
- Check if the data was retrieved from the cache item.
- __construct() : mixed
- Class constructor.
Properties
$action
Defines the cache loading behavior.
private
int
$action
= 0
- 0: Disable caching.
- 1: Load and automatically save cache.
- 2: Load cache but do not automatically save cache.
$db_cache
Cache driver instance.
private
FileCache
$db_cache
$instance
Singleton instance of the class.
private
static DbCache
$instance
= null
Methods
cacheOn()
Enable caching.
public
cacheOn([bool $auto_save = true ]) : void
Cache will be checked before querying data.
Parameters
- $auto_save : bool = true
-
(optional) Whether to automatically save cache results. Default is true.
Return values
void —clear()
Clear the cache.
public
clear() : bool|array<string|int, mixed>
Return values
bool|array<string|int, mixed> —True if the cache is cleared successfully, or an array of failed items.
create()
Create an instance of the class (Singleton).
public
static create() : DbCache
Return values
DbCache —get()
Get data from the cache.
public
get(CacheItem $item) : mixed
Returns the cached data or false if the cache is not available.
Parameters
- $item : CacheItem
-
The cache item.
Return values
mixed —The cached data or false.
getAction()
Get the current cache action.
public
getAction() : int
- 0: Disable caching.
- 1: Load and automatically save cache.
- 2: Load cache but do not automatically save cache.
Return values
int —The cache action.
init()
Initialize a cache item based on the SQL query and its values.
public
init(string $sql, array<string|int, mixed> $values) : CacheItem
Parameters
- $sql : string
-
The SQL query.
- $values : array<string|int, mixed>
-
The query values.
Return values
CacheItem —The cache item.
save()
Save the cache item with the provided data.
public
save(CacheItem $item, mixed $data) : bool
Once the cache is saved, the automatic cache action will be disabled.
Use this method when calling cacheOn(false) to enable caching manually.
Subsequent queries that require caching must enable cache before each query.
Parameters
- $item : CacheItem
-
The cache item.
- $data : mixed
-
The data to be cached.
Return values
bool —True if the cache is saved successfully, false otherwise.
setAction()
Set the cache action.
public
setAction(int $value) : DbCache
- 0: Disable caching.
- 1: Load and automatically save cache.
- 2: Load cache but do not automatically save cache.
Parameters
- $value : int
-
The cache action value.
Return values
DbCache —usedCache()
Check if the data was retrieved from the cache item.
public
usedCache(CacheItem $item) : bool
Parameters
- $item : CacheItem
-
The cache item.
Return values
bool —True if the cache item was used, false otherwise.
__construct()
Class constructor.
private
__construct() : mixed