Menu

## DbCache
in package

DbCache.php : 22

Provides caching functionality for database query results.

##### Tags

see
  https://www.kotchasan.com/

### 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

DbCache.php : 33

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

DbCache.php : 40

Cache driver instance.

`private FileCache $db_cache`

#### $instance

DbCache.php : 47

Singleton instance of the class.

`private static DbCache $instance = null`

### Methods

#### cacheOn()

DbCache.php : 58

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()

DbCache.php : 68

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()

DbCache.php : 78

Create an instance of the class (Singleton).

`public static create() : DbCache`

##### Return values

DbCache —

#### get()

DbCache.php : 95

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()

DbCache.php : 109

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()

DbCache.php : 122

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()

DbCache.php : 139

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()

DbCache.php : 157

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()

DbCache.php : 170

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()

DbCache.php : 178

Class constructor.

`private __construct() : mixed`

##### Return values

mixed —

## Search results