class cache_support

internal  
 

Cache utility wrapper for Moodle's Cache API.

This class centralizes access to Moodle cache to protect the codebase from future architectural changes. All interactions with cache should go through this class to provide a stable, safe API layer.

Constants

PLUGIN_NAME

DEFAULT_CACHE

Methods

static cache|null
make(string|null $area = null)

Creates a Moodle cache loader instance for the given area.

static mixed
get_or_set(string $key, callable $resolver, string $area = self::DEFAULT_CACHE)

Resolves a value via callback and stores it in cache if missing.

static mixed
get(string $key, string $area = self::DEFAULT_CACHE)

Retrieves a value from the cache.

static bool
set(string $key, mixed $value, string $area = self::DEFAULT_CACHE)

Stores a value into the cache.

static bool
has(string $key, string $area = self::DEFAULT_CACHE)

Checks if a key exists in the cache.

static bool
delete(string $key, string $area = self::DEFAULT_CACHE)

Deletes a key from the cache.

static bool
delete_many(array $keys, string $area = self::DEFAULT_CACHE)

Deletes multiple keys from the cache at once.

static array|false
get_many(array $keys, string $area = self::DEFAULT_CACHE)

Fetches multiple keys from the cache in a single call.

static bool
set_many(array $keyvalues, string $area = self::DEFAULT_CACHE)

Stores multiple key/value pairs into the cache.

static bool
purge(string $area = self::DEFAULT_CACHE)

Purges all entries in the given cache area.

Details

at line 49
static cache|null make(string|null $area = null)

Creates a Moodle cache loader instance for the given area.

Parameters

string|null $area

Cache area name defined in db/caches.php.

Return Value

cache|null

Moodle cache instance or null on failure

at line 76
static mixed get_or_set(string $key, callable $resolver, string $area = self::DEFAULT_CACHE)

Resolves a value via callback and stores it in cache if missing.

This method simplifies the common pattern of reading from cache and, if missing, resolving the value via callback and storing it.

Note: Moodle Cache API does not support per-item TTL at this level; if you need expiration control, use definitions in db/caches.php or invalidation mechanics via keys/versions.

Parameters

string $key

cache key

callable $resolver

callback that returns the value when not cached

string $area

cache area

Return Value

mixed

value from cache or resolved via callback; false on error

at line 109
static mixed get(string $key, string $area = self::DEFAULT_CACHE)

Retrieves a value from the cache.

Parameters

string $key

cache key

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

mixed

returns the cached value, null if not found, or false on error

at line 134
static bool set(string $key, mixed $value, string $area = self::DEFAULT_CACHE)

Stores a value into the cache.

Parameters

string $key

cache key

mixed $value

serializable value to store

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True on success, false otherwise

at line 158
static bool has(string $key, string $area = self::DEFAULT_CACHE)

Checks if a key exists in the cache.

Parameters

string $key

cache key

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True if exists, false otherwise (including on error)

at line 182
static bool delete(string $key, string $area = self::DEFAULT_CACHE)

Deletes a key from the cache.

Parameters

string $key

cache key

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True on success, false otherwise

at line 206
static bool delete_many(array $keys, string $area = self::DEFAULT_CACHE)

Deletes multiple keys from the cache at once.

Parameters

array $keys

list of cache keys

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True on success, false otherwise

at line 231
static array|false get_many(array $keys, string $area = self::DEFAULT_CACHE)

Fetches multiple keys from the cache in a single call.

Parameters

array $keys

list of keys to fetch

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

array|false

associative array of key => value (missing keys omitted) or false on error

at line 255
static bool set_many(array $keyvalues, string $area = self::DEFAULT_CACHE)

Stores multiple key/value pairs into the cache.

Parameters

array $keyvalues

associative array of key => value

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True on success, false otherwise

at line 279
static bool purge(string $area = self::DEFAULT_CACHE)

Purges all entries in the given cache area.

Parameters

string $area

Cache area. Defaults to self::DEFAULT_CACHE.

Return Value

bool

True on success, false otherwise