Oihana PHP System

CacheableTrait

Provides a standardized caching layer for classes using PSR-16 (Simple Cache).

This trait manages:

  • Instance-level caching: Easily enable/disable cache per object.
  • Dependency Injection: Can resolve cache instances from a PSR-11 Container.
  • Flexible TTL: Defines a default Time To Live (TTL) that can be overridden at call time.
  • Fluent Initialization: Provides methods to hydrate the cache configuration from arrays.
  • Expected configuration keys in $init arrays:
  • 'cache' (string|CacheInterface): The cache instance or its container ID.
  • 'cacheable' (bool): Toggle to enable/disable the cache functionality.
  • 'ttl' (int|DateInterval|null): The default expiration time.
Tags
author

Marc Alcaraz (eKameleon)

Table of Contents

Constants

CACHE  = 'cache'
The 'cache' parameter constant.
CACHEABLE  = 'cacheable'
The 'cacheable' parameter constant.
TTL  = 'ttl'
The 'ttl' parameter constant.

Properties

$cache  : CacheInterface|mixed|null
The PSR-16 cache reference.
$cacheable  : bool
Indicates if the instance use an internal PSR-16 cache.
$ttl  : null|int|DateInterval
Default TTL for cache items.

Methods

clearCache()  : void
Clear the cache.
deleteCache()  : void
Delete a key/value in the cache.
getCache()  : mixed
Returns the registered value in the cache with a specific key.
hasCache()  : bool
Indicates if the key is registered in the cache.
initializeCache()  : static
Initialize the cache reference.
initializeCacheable()  : static
Initialize the cacheable property.
initializeTtl()  : static
Initialize the TTL property.
isCacheable()  : bool
Indicates if the ressource can use the cache.
setCache()  : bool
Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
setCacheMultiple()  : bool
Persists a set of key => value pairs in the cache, with an optional TTL.

Constants

CACHE

The 'cache' parameter constant.

public mixed CACHE = 'cache'

CACHEABLE

The 'cacheable' parameter constant.

public mixed CACHEABLE = 'cacheable'

Properties

$cache

The PSR-16 cache reference.

public CacheInterface|mixed|null $cache = null

$cacheable

Indicates if the instance use an internal PSR-16 cache.

public bool $cacheable = true

$ttl

Default TTL for cache items.

public null|int|DateInterval $ttl = null

Methods

deleteCache()

Delete a key/value in the cache.

public deleteCache(string $key) : void
Parameters
$key : string
Tags
throws
InvalidArgumentException

getCache()

Returns the registered value in the cache with a specific key.

public getCache(string $key) : mixed
Parameters
$key : string
Tags
throws
InvalidArgumentException

hasCache()

Indicates if the key is registered in the cache.

public hasCache(string|null $key) : bool
Parameters
$key : string|null
Tags
throws
InvalidArgumentException
Return values
bool

initializeCache()

Initialize the cache reference.

public initializeCache([array<string|int, mixed> $init = [] ][, Container|null $container = null ]) : static
Parameters
$init : array<string|int, mixed> = []
$container : Container|null = null
Tags
throws
DependencyException
throws
NotFoundException
Return values
static

initializeCacheable()

Initialize the cacheable property.

public initializeCacheable([array<string|int, mixed> $init = [] ]) : static
Parameters
$init : array<string|int, mixed> = []
Return values
static

initializeTtl()

Initialize the TTL property.

public initializeTtl([array<string|int, mixed> $init = [] ]) : static
Parameters
$init : array<string|int, mixed> = []
Return values
static

isCacheable()

Indicates if the ressource can use the cache.

public isCacheable([array<string|int, mixed> $init = [] ]) : bool
Parameters
$init : array<string|int, mixed> = []
Return values
bool

setCache()

Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.

public setCache(string $key, mixed $value[, null|int|DateInterval $ttl = null ]) : bool
Parameters
$key : string

The key of the item to store.

$value : mixed

The value of the item to store, must be serializable.

$ttl : null|int|DateInterval = null

Optional TTL (Time to Live)value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

Tags
throws
InvalidArgumentException

MUST be thrown if the $key string is not a legal value.

Return values
bool

True on success and false on failure.

setCacheMultiple()

Persists a set of key => value pairs in the cache, with an optional TTL.

public setCacheMultiple(array<string|int, mixed> $values[, null|int|DateInterval $ttl = null ]) : bool
Parameters
$values : array<string|int, mixed>

A list of key => value pairs for a multiple-set operation.

$ttl : null|int|DateInterval = null

Optional TTL (Time to Live) value of this item. If no value is sent and the driver supports TTL then the library may set a default value for it or let the driver take care of that.

Tags
throws
InvalidArgumentException
Return values
bool

True on success and false on failure.


        
On this page

Search results