jmf/simple-cache

Cache package implementing PSR-16 simple cache interface.

1.0.0 2024-05-20 02:23 UTC

This package is auto-updated.

Last update: 2024-12-20 03:31:39 UTC


README

Allows caching of data (with Memcached, file-system, etc).

Usage

Initialization

<?php

use Jmf\Cache\CacheClient;
use Jmf\Cache\Storage\FileSystemStorage;
use Jmf\Cache\Storage\MemcachedStorage;
use Jmf\Cache\Storage\NullStorage;
use Jmf\Cache\Storage\VolatileStorage;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;

/* @var ClockInterface $clock */
/* @var LoggerInterface $logger */

// Memcached
$storage = MemcachedStorage::createFromCredentials('123.45.67.89');
$cache   = new CacheClient($storage, $clock, $logger);

// Volatile storage
$storage = new VolatileStorage();
$cache   = new CacheClient($storage, $clock, $logger);

// File-system storage
$storage = new FileSystemStorage('/tmp/cache');
$cache   = new CacheClient($storage, $clock, $logger);

// Null storage (caches nothing)
$storage = new NullStorage();
$cache   = new CacheClient($storage, $clock, $logger);

Storing and retrieving data

<?php

$objectToStore = new \stdClass();
$objectToStore->bar = 'baz';

$cache->set('foo', $objectToStore);

// ...

$object = $cache->get('foo');