koded/cache-extended

A PSR-6 caching library with support for several caching technologies.

2.0.0 2021-05-03 13:46 UTC

This package is auto-updated.

Last update: 2024-03-29 03:57:52 UTC


README

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Minimum PHP Version Software license

A PSR-6 caching library for PHP 7 using several caching technologies.

Requirements

  • Linux machine
  • PHP 8

Recommended cache technologies are

  • Redis server
  • Memcached

Recommended PHP modules

For developing purposes you can use

  • Memory client (default)
  • File client

Usage

  • create an instance of CacheItemPoolInterface with desired caching technology
  • manipulate the cache items with the pool instance
$cache = CachePool::use('redis');

$item = $cache->getItem('fubar');
$item->set('some value');
$item->expiresAfter(new DateTime('3 days'));

$cache->save();

The pool instance is created only once.

CachePool::use() accepts specific parameters for the underlying caching technology.
This method uses the Koded Simple Cache package. Please see the README in that repository for the specific arguments.

You can grab the cache client if you want to use it directly

/** $var Koded\Caching\Cache $client */
$client = $cache->client();

Deferring the items

To postpone the saving of the cache items (store all items "at once"), you can use the saveDeferred() method. These cache items are saved when you

  • execute commit()
  • when CacheItemPoolInterface instance is destroyed

Keep in mind that commit() is not an atomic operation. There is no guarantee that all items will be saved, because anything can happen while save() runs (network, client crash, etc).

$cache->saveDeferred($event);
$cache->saveDeferred($counter);

// ... do some stuff

// store this now
$cache->save($dependency);

// ... do more stuff

$cache->saveDeferred($updates);
$cache->saveDeferred($extras);

// Store all deferred items
$cache->commit();

License

The code is distributed under the terms of The 3-Clause BSD license.