Cache

For the why — instantiated rather than static, the key_hash collision-defense rule — see Caching. This page is the full PSR-16 method reference.

Construction

new Cache(
    string $driver,          // Cache::DRIVER_FILE | DRIVER_DATABASE | DRIVER_REDIS
    ?string $path = null,    // required for DRIVER_FILE
    ?string $context = null, // DB context, for DRIVER_DATABASE
    ?object $redis = null,   // required for DRIVER_REDIS — anything exposing get/set/setex/del/exists/keys
)

PSR-16 methods

$cache->get(string $key, mixed $default = null): mixed
$cache->set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
$cache->delete(string $key): bool
$cache->has(string $key): bool
$cache->clear(): bool
$cache->getMultiple(iterable $keys, mixed $default = null): iterable
$cache->setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
$cache->deleteMultiple(iterable $keys): bool

$ttl = null means no expiration. A PSR-16 key must be non-empty and may not contain {}()/\@: — an invalid key throws.

Next steps

  • Caching — the full guide, including the database driver's collision-defense rule.