Cache class abstract

Framework\Cache\Cache

Class Cache.

Defined in libraries/cache/src/Cache.php

Properties #

$configs
Driver specific configurations.
$prefix
Keys prefix.
$serializer
Data serializer.
$logger
The Logger instance if is set.
$defaultTtl
The default Time To Live value.
$debugCollector
-
$autoClose
-

$configs

protected array $configs = array ( )

Driver specific configurations.

$prefix

protected ?string $prefix = NULL

Keys prefix.

$serializer

protected \Framework\Cache\Serializer $serializer

Data serializer.

$logger

protected ?\Framework\Log\Logger $logger

The Logger instance if is set.

$defaultTtl

protected int $defaultTtl = 60

The default Time To Live value.

$debugCollector

protected \Framework\Cache\Debug\CacheCollector $debugCollector

$autoClose

protected bool $autoClose = true

Methods #

__construct()
Cache constructor.
__destruct()
-
close()
Close the cache storage.
decrement()
Decrements the value of one item.
delete()
Deletes one item from the cache storage.
deleteMulti()
Deletes multi items from the cache storage.
flush()
Flush the cache storage.
get()
Gets one item from the cache storage.
getDefaultTtl()
Get the default Time To Live value in seconds.
getMulti()
Gets multi items from the cache storage.
getSerializer()
-
increment()
Increments the value of one item.
isAutoClose()
-
set()
Sets one item to the cache storage.
setAutoClose()
-
setDebugCollector()
-
setDefaultTtl()
Set the default Time To Live value in seconds.
setMulti()
Sets multi items to the cache storage.
addDebugDelete()
-
addDebugFlush()
-
addDebugGet()
-
addDebugSet()
-
initialize()
Initialize Cache handlers and configurations.
log()
-
makeTtl()
Make the Time To Live value.
renderKey()
-
serialize()
-
setConfigs()
-
setSerializer()
-
unserialize()
-

__construct()

public function __construct(mixed $configs = array ( ), ?string $prefix = NULL, \Framework\Cache\Serializer|string $serializer = \Framework\Cache\Serializer::PHP, ?\Framework\Log\Logger $logger = NULL)

Cache constructor.

Parameters
  • mixed $configs - Driver specific configurations. Set null to not initialize and set a custom object.
  • ?string $prefix - Keys prefix
  • \Framework\Cache\Serializer|string $serializer - Data serializer
  • ?\Framework\Log\Logger $logger - Logger instance

__destruct()

public function __destruct()

close()

public function close(): bool

Close the cache storage.

Returns bool - TRUE on success, otherwise FALSE

decrement()

public function decrement(string $key, int $offset = 1, ?int $ttl = NULL): int

Decrements the value of one item.

Parameters
  • string $key - The item name
  • int $offset - The value to decrement
  • ?int $ttl - The Time To Live for the item or null to use the default
Returns int - The current item value

delete()

public abstract function delete(string $key): bool

Deletes one item from the cache storage.

Parameters
  • string $key - the item name
Returns bool - TRUE if the item was deleted, FALSE if fail to delete

deleteMulti()

public function deleteMulti(array $keys): array

Deletes multi items from the cache storage.

Parameters
  • array $keys - List of items names to be deleted
Returns array - associative array with key names and respective delete status

flush()

public abstract function flush(): bool

Flush the cache storage.

Returns bool - TRUE if all items are deleted, otherwise FALSE

get()

public abstract function get(string $key): mixed

Gets one item from the cache storage.

Parameters
  • string $key - The item name
Returns mixed - The item value or null if not found

getDefaultTtl()

public function getDefaultTtl(): int

Get the default Time To Live value in seconds.

Returns int

getMulti()

public function getMulti(array $keys): array

Gets multi items from the cache storage.

Parameters
  • array $keys - List of items names to get
Returns array - associative array with key names and respective values

getSerializer()

public function getSerializer(): \Framework\Cache\Serializer
Returns \Framework\Cache\Serializer

increment()

public function increment(string $key, int $offset = 1, ?int $ttl = NULL): int

Increments the value of one item.

Parameters
  • string $key - The item name
  • int $offset - The value to increment
  • ?int $ttl - The Time To Live for the item or null to use the default
Returns int - The current item value

isAutoClose()

public function isAutoClose(): bool
Returns bool

set()

public abstract function set(string $key, mixed $value, ?int $ttl = NULL): bool

Sets one item to the cache storage.

Parameters
  • string $key - The item name
  • mixed $value - The item value
  • ?int $ttl - The Time To Live for the item or null to use the default
Returns bool - TRUE if the item was set, FALSE if fail to set

setAutoClose()

public function setAutoClose(bool $autoClose): static
Parameters
  • bool $autoClose - True to enable auto close, false to disable
Returns static

setDebugCollector()

public function setDebugCollector(\Framework\Cache\Debug\CacheCollector $debugCollector): static
Parameters
  • \Framework\Cache\Debug\CacheCollector $debugCollector
Returns static

setDefaultTtl()

public function setDefaultTtl(int $seconds): static

Set the default Time To Live value in seconds.

Parameters
  • int $seconds - An integer greater than zero
Returns static

setMulti()

public function setMulti(array $data, ?int $ttl = NULL): array

Sets multi items to the cache storage.

Parameters
  • array $data - Associative array with key names and respective values
  • ?int $ttl - The Time To Live for all the items or null to use the default
Returns array - associative array with key names and respective set status

addDebugDelete()

protected function addDebugDelete(string $key, float $start, bool $status): bool
Parameters
  • string $key
  • float $start
  • bool $status
Returns bool

addDebugFlush()

protected function addDebugFlush(float $start, bool $status): bool
Parameters
  • float $start
  • bool $status
Returns bool

addDebugGet()

protected function addDebugGet(string $key, float $start, mixed $value): mixed
Parameters
  • string $key
  • float $start
  • mixed $value
Returns mixed

addDebugSet()

protected function addDebugSet(string $key, ?int $ttl, float $start, mixed $value, bool $status): bool
Parameters
  • string $key
  • ?int $ttl
  • float $start
  • mixed $value
  • bool $status
Returns bool

initialize()

protected function initialize(): void

Initialize Cache handlers and configurations.

Returns void

log()

protected function log(string $message, \Framework\Log\LogLevel $level = \Framework\Log\LogLevel::ERROR): void
Parameters
  • string $message
  • \Framework\Log\LogLevel $level
Returns void

makeTtl()

protected function makeTtl(?int $seconds): int

Make the Time To Live value.

Parameters
  • ?int $seconds - TTL value or null to use the default
Returns int - The input $seconds or the $defaultTtl as integer

renderKey()

protected function renderKey(string $key): string
Parameters
  • string $key
Returns string

serialize()

protected function serialize(mixed $value): string
Parameters
  • mixed $value
Returns string
throws
\JsonException

setConfigs()

protected function setConfigs(array $configs): static
Parameters
  • array $configs
Returns static

setSerializer()

protected function setSerializer(\Framework\Cache\Serializer|string $serializer): static
Parameters
  • \Framework\Cache\Serializer|string $serializer
Returns static

unserialize()

protected function unserialize(string $value): mixed
Parameters
  • string $value
Returns mixed