This documentation describes unreleased changes on the main branch.

CacheStore

Class.

use Saij\WpAppFramework\Cache\CacheStore;

The cache of a theme or plugin. Inject it to cache data of your own, for example the response of an external API:

$weather = $this->cache->remember('weather', fn(): array => $this->client->fetch(), ttl: 600, tags: [Feed::Weather]);

Blocks and REST routes with #[Cache] use the same cache. Entries are stored in the first available backend of the filter wp_app_framework/cache/backends/{slug} (default: object cache, files, transients).

Methods

getBackendName()

getBackendName(): ?string

The backend that stores the entries in this request ("object", "file" or "transient"); null if caching is off.

Returns ?string

invalidate()

invalidate(CacheTag ...$tags): void

Invalidates the tags: entries with one of them are never read again.

ParameterTypeDefaultDescription
...tagsCacheTagThe tags to invalidate, in the cache of this theme or plugin.

Throws InvalidArgumentException: If a tag is invalid, e.g. names an unknown post class.

remember()

remember(string $key, Closure $compute, int $ttl, array $tags = [], array $vary = []): mixed

Returns the cached value of the key, or computes, stores and returns it.

Values must be plain data: null, booleans, numbers, strings and arrays of them. Exceptions of $compute are passed on; nothing is stored then.

ParameterTypeDefaultDescription
keystringrequiredNames the entry within this theme or plugin; not empty. The values of $vary and the versions of $tags are part of the key.
computeClosure(): TrequiredComputes the value when the cache has none; not called on a hit. Called on every call when caching is off.
ttlintrequiredLifetime in seconds, at least 1.
tagslist<CacheTag>[]Invalidate the entry.
varylist<CacheVary>[]Circumstances the entry depends on.

Returns T

Throws InvalidArgumentException: If the key is empty or the ttl, a tag or a variant is invalid.

Throws LogicException: If the value is not plain data.