class Cache

Caching with namespace isolation, dependency tracking, and stampede prevention.

Constants

Priority

dependency

Expire

dependency

Sliding

dependency

Tags

dependency

Files

dependency

Items

dependency

Constants

dependency

Callbacks

dependency

Namespaces

dependency

All

dependency

deprecated PRIORITY

deprecated EXPIRATION

deprecated EXPIRE

deprecated SLIDING

deprecated TAGS

deprecated FILES

deprecated ITEMS

deprecated CONSTS

deprecated CALLBACKS

deprecated NAMESPACES

deprecated ALL

internal NamespaceSeparator

internal NAMESPACE_SEPARATOR

Methods

__construct(Storage $storage, string|null $namespace = null)

No description

getStorage()

No description

string
getNamespace()

No description

derive(string $namespace)

Returns a new cache object scoped to a sub-namespace.

mixed
load(mixed $key, callable|null $generator = null, array|null $dependencies = null)

Returns a cached item, or generates and stores it using the given callback.

array
bulkLoad(array $keys, callable|null $generator = null)

No description

mixed
save(mixed $key, mixed $data, array|null $dependencies = null)

Stores item in the cache and returns it.

void
bulkSave(array $items, array|null $dependencies = null)

Stores multiple items in the cache at once.

void
remove(mixed $key)

No description

void
clean(array|null $conditions = null)

Removes items from the cache by conditions:

  • Cache::Priority => (int) removes items with equal or lower priority
  • Cache::Tags => (array) removes items with matching tags
  • Cache::All => true clears the entire cache

mixed
call(callable $function)

Calls a function and caches its result. Subsequent calls with the same arguments return the cached value.

wrap(callable $function, array|null $dependencies = null)

Returns a cached wrapper around a function. Each unique set of arguments is cached separately.

OutputHelper|null
capture(mixed $key)

Starts output buffering for caching. Returns null and echoes cached content if already cached, or returns an OutputHelper to capture and save new output.

OutputHelper|null
start(mixed $key) deprecated

No description

string
generateKey(mixed $key)

Generates a namespaced storage key from a user-provided key.

static bool
checkCallbacks(array $callbacks)

Validates all callback dependencies. Returns false if any callback returns false.

Details

at line 74
__construct(Storage $storage, string|null $namespace = null)

No description

Parameters

Storage $storage
string|null $namespace

at line 81
final Storage getStorage()

No description

Return Value

Storage

at line 87
final string getNamespace()

No description

Return Value

string

at line 96
Cache derive(string $namespace)

Returns a new cache object scoped to a sub-namespace.

Parameters

string $namespace

Return Value

Cache

at line 107
mixed load(mixed $key, callable|null $generator = null, array|null $dependencies = null)

Returns a cached item, or generates and stores it using the given callback.

Parameters

mixed $key
callable|null $generator
array|null $dependencies

Return Value

mixed

at line 134
array bulkLoad(array $keys, callable|null $generator = null)

No description

Parameters

array $keys
callable|null $generator

Return Value

array

at line 191
mixed save(mixed $key, mixed $data, array|null $dependencies = null)

Stores item in the cache and returns it.

Dependencies:

  • Cache::Priority => (int) priority
  • Cache::Expire => (string|int) expiration time, infinite if null
  • Cache::Sliding => (bool) use sliding expiration?
  • Cache::Tags => (array) tags
  • Cache::Files => (array|string) file paths
  • Cache::Items => (array|string) dependent cache keys
  • Cache::Constants => (array|string) PHP constant names

Parameters

mixed $key
mixed $data
array|null $dependencies

Return Value

mixed

value itself

Exceptions

InvalidArgumentException

at line 226
void bulkSave(array $items, array|null $dependencies = null)

Stores multiple items in the cache at once.

Parameters

array $items
array|null $dependencies

Return Value

void

at line 314
void remove(mixed $key)

No description

Parameters

mixed $key

Return Value

void

at line 327
void clean(array|null $conditions = null)

Removes items from the cache by conditions:

  • Cache::Priority => (int) removes items with equal or lower priority
  • Cache::Tags => (array) removes items with matching tags
  • Cache::All => true clears the entire cache

Parameters

array|null $conditions

Return Value

void

at line 341
mixed call(callable $function)

Calls a function and caches its result. Subsequent calls with the same arguments return the cached value.

Parameters

callable $function

Return Value

mixed

at line 357
Closure wrap(callable $function, array|null $dependencies = null)

Returns a cached wrapper around a function. Each unique set of arguments is cached separately.

Parameters

callable $function
array|null $dependencies

Return Value

Closure

at line 377
OutputHelper|null capture(mixed $key)

Starts output buffering for caching. Returns null and echoes cached content if already cached, or returns an OutputHelper to capture and save new output.

Parameters

mixed $key

Return Value

OutputHelper|null

at line 392
OutputHelper|null start(mixed $key) deprecated

deprecated use capture()

No description

Parameters

mixed $key

Return Value

OutputHelper|null

at line 401
protected string generateKey(mixed $key)

Generates a namespaced storage key from a user-provided key.

Parameters

mixed $key

Return Value

string

at line 414
static bool checkCallbacks(array $callbacks)

Validates all callback dependencies. Returns false if any callback returns false.

Parameters

array $callbacks

Return Value

bool