quillstack / cache
A simple cache based on PSR-16: Common Interface for Caching Libraries.
Requires
- php: ^8.1
- psr/clock: ^1.0
- psr/simple-cache: ^3.0
- quillstack/clock: ^0.6
- quillstack/local-storage: ^0.6
- quillstack/storage-interface: ^0.6
Requires (Dev)
- phpstan/phpstan: ^2.0
- quillstack/unit-tests: ^0.9
This package is auto-updated.
Last update: 2026-08-23 00:22:15 UTC
README
A simple cache based on PSR-16: Common Interface for Caching Libraries.
Installation
composer require quillstack/cache
Usage
Two caches come with it. ArrayCache keeps entries for as long as the process runs, which
is what a single request needs:
use Quillstack\Cache\ArrayCache; $cache = new ArrayCache(); $cache->set('user.42', $user, 300); $user = $cache->get('user.42', $default);
FileCache writes them to disk, so they outlive the request:
use Quillstack\Cache\FileCache; use Quillstack\LocalStorage\LocalStorage; $cache = new FileCache(new LocalStorage(), __DIR__ . '/var/cache');
How long an entry lives is given in seconds, as a DateInterval, or left out to keep it for
as long as the cache does:
$cache->set('key', $value, 60); $cache->set('key', $value, new DateInterval('PT1H')); $cache->set('key', $value);
getMultiple(), setMultiple() and deleteMultiple() work on several keys at once,
has() says whether a key is there, and clear() empties the cache.
Time
Both caches take a PSR-20 clock, which is what decides when an entry has run out. Without
one they read the wall clock. FrozenClock stands still until it is moved, so a test does
not have to wait:
use Quillstack\Cache\Clock\FrozenClock; $clock = new FrozenClock(); $cache = new ArrayCache($clock); $cache->set('key', 'value', 60); $clock->sleep(61); $cache->get('key'); // null
Keys
PSR-16 asks a key to hold at least one character and none of {}()/\@:. A key which does
not is refused with an InvalidCacheKeyException, which is a
Psr\SimpleCache\InvalidArgumentException.
Tests
composer test
Coverage needs phpdbg:
composer test:coverage
License
MIT. See LICENSE.