Skip to content

Repository files navigation

Quillstack Cache

Tests

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.

About

A simple cache based on PSR-16: Common Interface for Caching Libraries.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages