class CRM_Utils_Cache_Tiered implements CRM_Utils_Cache_Interface

Class CRM_Utils_Cache_Tiered

Tiered implements a hierarchy of fast and slow caches. For example, you might have a configuration in which:

  • A local/in-memory array caches info for up to 1 minute (60s).
  • A Redis cache retains info for up to 10 minutes (600s).
  • A SQL cache retains info for up to 1 hour (3600s).

Cached data will be written to all three tiers. When reading, you'll hit the fastest available tier.

The example would be created with:

$cache = new CRM_Utils_Cache_Tiered([ new CRM_Utils_Cache_ArrayCache(...), new CRM_Utils_Cache_Redis(...), new CRM_Utils_Cache_SqlGroup(...), ], [60, 600, 3600]);

Note: - Correctly implementing PSR-16 leads to a small amount of CPU+mem overhead. If you need an extremely high number of re-reads within a thread and can live with only two tiers, try CRM_Utils_Cache_ArrayDecorator or CRM_Utils_Cache_FastArrayDecorator instead. - With the exception of unit-testing, you should not access the underlying tiers directly. The data-format may be different than your expectation.

Traits

Properties

protected array $maxTimeouts
protected array $tiers

Methods

iterable
getMultiple(iterable $keys, mixed $default = NULL)

Obtains multiple cache items by their unique keys.

bool
setMultiple(iterable $values, null|int|DateInterval $ttl = NULL)

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

bool
deleteMultiple(iterable $keys)

Deletes multiple cache items in a single operation.

__construct(array $tiers, array $maxTimeouts = [86400])

CRM_Utils_Cache_Tiered constructor.

bool
set(string $key, mixed $value, null|int|DateInterval $ttl = NULL)

Set the value in the cache.

mixed
get(string $key, mixed $default = NULL)

Get a value from the cache.

bool
delete(string $key)

Delete a value from the cache.

bool
flush()

Delete all values from the cache.

bool
clear()

Delete all values from the cache.

bool
has(string $key)

Determines whether an item is present in the cache.

getEffectiveTtl($tierNum, $ttl)

No description

Details

iterable getMultiple(iterable $keys, mixed $default = NULL)

Obtains multiple cache items by their unique keys.

Parameters

iterable $keys A list of keys that can obtained in a single operation.
mixed $default Default value to return for keys that do not exist.

Return Value

iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.

Exceptions

InvalidArgumentException MUST be thrown if $keys is neither an array nor a Traversable, or if any of the $keys are not a legal value.

bool setMultiple(iterable $values, null|int|DateInterval $ttl = NULL)

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

Parameters

iterable $values A list of key => value pairs for a multiple-set operation.
null|int|DateInterval $ttl Optional. The TTL 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.

Return Value

bool True on success and false on failure.

Exceptions

InvalidArgumentException MUST be thrown if $values is neither an array nor a Traversable, or if any of the $values are not a legal value.

bool deleteMultiple(iterable $keys)

Deletes multiple cache items in a single operation.

Parameters

iterable $keys A list of string-based keys to be deleted.

Return Value

bool True if the items were successfully removed. False if there was an error.

Exceptions

InvalidArgumentException MUST be thrown if $keys is neither an array nor a Traversable, or if any of the $keys are not a legal value.

at line 92
__construct(array $tiers, array $maxTimeouts = [86400])

CRM_Utils_Cache_Tiered constructor.

Parameters

array $tiers List of cache instances, with fastest/closest first. Must be indexed numerically (0, 1, 2...).
array $maxTimeouts A list of maximum timeouts for each cache-tier. There must be at least one value in this array. If timeouts are omitted for slower tiers, they are filled in with the last value.

Exceptions

CRM_Core_Exception

at line 110
bool set(string $key, mixed $value, null|int|DateInterval $ttl = NULL)

Set the value in the cache.

Parameters

string $key
mixed $value
null|int|DateInterval $ttl

Return Value

bool

at line 125
mixed get(string $key, mixed $default = NULL)

Get a value from the cache.

Parameters

string $key
mixed $default

Return Value

mixed The previously set value value, or $default (NULL).

at line 144
bool delete(string $key)

Delete a value from the cache.

Parameters

string $key

Return Value

bool

at line 152
bool flush()

Delete all values from the cache.

NOTE: flush() and clear() should be aliases. flush() is specified by Civi's traditional interface, and clear() is specified by PSR-16.

Return Value

bool

at line 156
bool clear()

Delete all values from the cache.

NOTE: flush() and clear() should be aliases. flush() is specified by Civi's traditional interface, and clear() is specified by PSR-16.

Return Value

bool

at line 166
bool has(string $key)

Determines whether an item is present in the cache.

NOTE: It is recommended that has() is only to be used for cache warming type purposes and not to be used within your live applications operations for get/set, as this method is subject to a race condition where your has() will return true and immediately after, another script can remove it making the state of your app out of date.

Parameters

string $key The cache item key.

Return Value

bool

at line 178
protected getEffectiveTtl($tierNum, $ttl)

Parameters

$tierNum
$ttl