PSR-6 cache

Fork PSR-6 cache on GitHub
// boilerplate code example with Memcached, but any
// MatthiasMullie\Scrapbook\KeyValueStore adapter will work
$client = new \Memcached();
$client->addServer('localhost', 11211);
$cache = new \MatthiasMullie\Scrapbook\Adapters\Memcached($client);

// create Pool object from cache engine
$pool = new \MatthiasMullie\Scrapbook\Psr6\Pool($cache);

// get item from Pool
$item = $pool->getItem('key');

// get item value
$value = $item->get();

// ... or change the value & store it to cache

PSR-6 (a PHP-FIG standard) is a drastically different cache model than KeyValueStore & psr/simplecache: instead of directly querying values from the cache, psr/cache basically operates on value objects (Item) to perform the changes, which then feed back to the cache (Pool.)

It doesn’t let you do too many operations. If get, set, delete (and their *multi counterparts) and delete is all you need, you’re probably better off using this (or psr/simplecache) as this interface is also supported by other cache libraries.

This interface bridges the gap between KeyValueStore based adapters & features, and PSR-6: any of the Scrapbook tools are accessible in a PSR-6 compatible manner.

Methods Pool

getItem($key): Item

Returns a Cache Item representing the specified key.

getItems(array $keys = array()): Item[]

Returns a traversable set of cache items.

hasItem($key): bool

Confirms if the cache contains specified cache item.

clear(): bool

Deletes all items in the pool.

deleteItem($key): bool

Removes the item from the pool.

deleteItems(array $keys): bool

Removes multiple items from the pool.

save(CacheItemInterface $item): bool

Persists a cache item immediately.

saveDeferred(CacheItemInterface $item): bool

Sets a cache item to be persisted later.

commit(): bool

Persists any deferred cache items.

Methods Item

getKey(): string

Returns the key for the current cache item.

get(): mixed

Retrieves the value of the item from the cache associated with this object's key.

isHit(): bool

Confirms if the cache item lookup resulted in a cache hit.

set($value): static

Sets the value represented by this cache item.

expiresAt($expiration): static

Sets the expiration time for this cache item.

expiresAfter($expiration): static

Sets the expiration time for this cache item.

The PHP-FIG (Framework Interop Group) have defined this pool driver cache standard. Scrapbook has an implementation that builds on key-value-store, so it works with all adapters.


Project links



composer require matthiasmullie/scrapbook