class CRM_Utils_Cache

Cache is an empty base object, we'll modify the scheme when we have different caching schemes

Constants

DELIMITER

Properties

static object $_singleton (Quasi-Private) Treat this as private. It is marked public to facilitate testing.

Methods

__construct(array $config)

Constructor.

singleton()

Singleton function used to manage this object.

static array
getCacheSettings($cachePlugin)

Get cache relevant settings.

create(array $params = [])

Create a new, named, limited-use cache.

static string
cleanKey(string $key)

Normalize a cache key.

static string
assertValidKey(string $key)

Assert that a key is well-formed.

static string
getCacheDriver()

No description

static string
nack()

Generate a unique negative-acknowledgement token (NACK).

Details

at line 59
CRM_Utils_Cache __construct(array $config)

Constructor.

Parameters

array $config An array of configuration params.

Return Value

CRM_Utils_Cache

at line 68
static CRM_Utils_Cache_Interface singleton()

Singleton function used to manage this object.

at line 88
static array getCacheSettings($cachePlugin)

Get cache relevant settings.

Parameters

$cachePlugin

Return Value

array associative array of settings for the cache

at line 182
static CRM_Utils_Cache_Interface create(array $params = [])

Create a new, named, limited-use cache.

This is a factory function. Generally, you should use Civi::cache($name) to locate managed cached instance.

Parameters

array $params Array with keys: - name: string, unique symbolic name. For a naming convention, use snake_case or CamelCase to maximize portability/cleanliness. Any other punctuation or whitespace should function correctly, but it can be harder to inspect/debug. - type: array|string, list of acceptable cache types, in order of preference. - prefetch: bool, whether to prefetch all data in cache (if possible). - withArray: bool|null|'fast', whether to setup a thread-local array-cache in front of the cache driver. Note that cache-values may be passed to the underlying driver with extra metadata, so this will slightly change/enlarge the on-disk format. Support varies by driver: - For most memory backed caches, this option is meaningful. - For SqlGroup, this option is ignored. SqlGroup has equivalent behavior built-in. - For ArrayCache, this option is ignored. It's redundant. If this is a short-lived process in which TTL's don't matter, you might use 'fast' mode. It sacrifices some PSR-16 compliance and cache-coherency protections to improve performance.

Return Value

CRM_Utils_Cache_Interface

Exceptions

CRM_Core_Exception

See also

Civi::cache

at line 235
static string cleanKey(string $key)

Normalize a cache key.

This bridges an impedance mismatch between our traditional caching and PSR-16 -- PSR-16 accepts a narrower range of cache keys.

Parameters

string $key Ex: 'ab/cd:ef'

Return Value

string Ex: '_abcd1234abcd1234' or 'ab_xx/cd_xxef'. A similar key, but suitable for use with PSR-16-compliant cache providers.

at line 262
static string assertValidKey(string $key)

Assert that a key is well-formed.

Parameters

string $key

Return Value

string Same $key, if it's valid.

Exceptions

CRM_Utils_Cache_InvalidArgumentException

at line 284
static string getCacheDriver()

Return Value

string Ex: 'ArrayCache', 'Memcache', 'Redis'.

at line 327
static string nack()

Generate a unique negative-acknowledgement token (NACK).

When using PSR-16 to read a value, the $cahce->get() will a return a default value on cache-miss, so it's hard to know if you've gotten a geniune value from the cache or just a default. If you're in an edge-case where it matters (and you want to do has()+get() in a single roundtrip), use the nack() as the default:

$nack = CRM_Utils_Cache::nack(); $value = $cache->get('foo', $nack); echo ($value === $nack) ? "Cache has a value, and we got it" : "Cache has no value".

The value should be unique to avoid accidental matches.

Return Value

string Unique nonce value indicating a "negative acknowledgement" (failed read). If we need to accurately perform has($key)+get($key), we can use get($key,$nack).