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

PRIORITY

EXPIRATION

EXPIRE

SLIDING

TAGS

FILES

ITEMS

CONSTS

CALLBACKS

NAMESPACES

ALL

internal NamespaceSeparator

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)

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 72
__construct(Storage $storage, string|null $namespace = null)

No description

Parameters

Storage $storage
string|null $namespace

at line 79
final Storage getStorage()

No description

Return Value

Storage

at line 85
final string getNamespace()

No description

Return Value

string

at line 94
Cache derive(string $namespace)

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

Parameters

string $namespace

Return Value

Cache

at line 105
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 132
array bulkLoad(array $keys, callable|null $generator = null)

No description

Parameters

array $keys
callable|null $generator

Return Value

array

at line 189
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 224
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 312
void remove(mixed $key)

No description

Parameters

mixed $key

Return Value

void

at line 325
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 339
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 355
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 375
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 387
OutputHelper|null start(mixed $key)

No description

Parameters

mixed $key

Return Value

OutputHelper|null

at line 398
protected string generateKey(mixed $key)

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

Parameters

mixed $key

Return Value

string

at line 411
static bool checkCallbacks(array $callbacks)

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

Parameters

array $callbacks

Return Value

bool