CRM_Utils_Cache_Tiered
in package
implements
CRM_Utils_Cache_Interface
uses
CRM_Utils_Cache_NaiveMultipleTrait
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.
Table of Contents
Interfaces
Properties
- $maxTimeouts : array<string|int, mixed>
- $tiers : array<string|int, mixed>
Methods
- __construct() : mixed
- CRM_Utils_Cache_Tiered constructor.
- clear() : bool
- Delete all values from the cache.
- delete() : bool
- Delete a value from the cache.
- deleteMultiple() : bool
- Deletes multiple cache items in a single operation.
- flush() : bool
- Delete all values from the cache.
- get() : mixed
- Get a value from the cache.
- getMultiple() : iterable<string|int, mixed>
- Obtains multiple cache items by their unique keys.
- has() : bool
- Determines whether an item is present in the cache.
- set() : bool
- Set the value in the cache.
- setMultiple() : bool
- Persists a set of key => value pairs in the cache, with an optional TTL.
- getEffectiveTtl() : mixed
- assertIterable() : mixed
Properties
$maxTimeouts
protected
array<string|int, mixed>
$maxTimeouts
Array(int $tierNum => int $seconds).
$tiers
protected
array<string|int, mixed>
$tiers
List of cache instances, with fastest/closest first. Array(int $tierNum => CRM_Utils_Cache_Interface).
Methods
__construct()
CRM_Utils_Cache_Tiered constructor.
public
__construct(array<string|int, mixed> $tiers[, array<string|int, mixed> $maxTimeouts = [86400] ]) : mixed
Parameters
- $tiers : array<string|int, mixed>
-
List of cache instances, with fastest/closest first. Must be indexed numerically (0, 1, 2...).
- $maxTimeouts : array<string|int, mixed> = [86400]
-
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.
Tags
clear()
Delete all values from the cache.
public
clear() : bool
NOTE: flush() and clear() should be aliases. flush() is specified by Civi's traditional interface, and clear() is specified by PSR-16.
Return values
booldelete()
Delete a value from the cache.
public
delete(mixed $key) : bool
Parameters
- $key : mixed
Return values
booldeleteMultiple()
Deletes multiple cache items in a single operation.
public
deleteMultiple(iterable<string|int, mixed> $keys) : bool
Parameters
- $keys : iterable<string|int, mixed>
-
A list of string-based keys to be deleted.
Tags
Return values
bool —True if the items were successfully removed. False if there was an error.
flush()
Delete all values from the cache.
public
flush() : bool
NOTE: flush() and clear() should be aliases. flush() is specified by Civi's traditional interface, and clear() is specified by PSR-16.
Return values
boolget()
Get a value from the cache.
public
get(mixed $key[, mixed $default = NULL ]) : mixed
Parameters
- $key : mixed
- $default : mixed = NULL
Return values
mixed —The previously set value value, or $default (NULL).
getMultiple()
Obtains multiple cache items by their unique keys.
public
getMultiple(iterable<string|int, mixed> $keys[, mixed $default = NULL ]) : iterable<string|int, mixed>
Parameters
- $keys : iterable<string|int, mixed>
-
A list of keys that can obtained in a single operation.
- $default : mixed = NULL
-
Default value to return for keys that do not exist.
Tags
Return values
iterable<string|int, mixed> —A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
has()
Determines whether an item is present in the cache.
public
has(mixed $key) : bool
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
- $key : mixed
-
The cache item key.
Return values
boolset()
Set the value in the cache.
public
set(mixed $key, mixed $value[, mixed $ttl = NULL ]) : bool
Parameters
- $key : mixed
- $value : mixed
- $ttl : mixed = NULL
Return values
boolsetMultiple()
Persists a set of key => value pairs in the cache, with an optional TTL.
public
setMultiple(iterable<string|int, mixed> $values[, null|int|DateInterval $ttl = NULL ]) : bool
Parameters
- $values : iterable<string|int, mixed>
-
A list of key => value pairs for a multiple-set operation.
- $ttl : null|int|DateInterval = NULL
-
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.
Tags
Return values
bool —True on success and false on failure.
getEffectiveTtl()
protected
getEffectiveTtl(mixed $tierNum, mixed $ttl) : mixed
Parameters
- $tierNum : mixed
- $ttl : mixed
assertIterable()
private
assertIterable(mixed $func, mixed $keys) : mixed
Parameters
- $func : mixed
- $keys : mixed