remotelyliving/php-cache-adapter

A lightweight PSR6 / PSR16 cache adapter library for common caching scenarios

1.0.0 2020-05-23 20:40 UTC

README

Build Status Total Downloads Coverage Status License Scrutinizer Code Quality

php-cache-adapter:

💰 A PSR-6 and PSR-16 Cache Implementation For PHP 💰

Use Cases

If you want a lightweight, no frills PSR-6 / PSR-16 cache Memcache, Redis, or Memory adapter, this is for you. This library as born out of the idea that many other libraries offer way too much functionality for cache adapters and end up being overly complex or underperformant.

This library seeks to address the three most common cache storage mechanisms in the PHP ecosystem and not much more.

Installation

composer require remotelyliving/php-cache-adapter

Usage

SimpleCache

Simple Cache is the PSR-16 implementation of a simple cache adapter. Below are the different adapters you can create and use.

// Memcached flavor Simple Cache
$memcached = new \Memcached();
$memcached->addServer($memcacheHost, $memcachePort);
$memcachedAdapter = RemotelyLiving\PHPCacheAdapter\SimpleCache\Memcached::create($memcached);

// Redis Simple Cache
$redis = new \Redis();
$redis->pconnect($redisHost, $redisPort, $timeout);
$redisAdapter = RemotelyLiving\PHPCacheAdapter\SimpleCache\Redis::create($redis);

// Memory / Runtime Simple Cache
$memoryAdapter = RemotelyLiving\PHPCacheAdapter\SimpleCache\Memory::create($maxItemsForArray); // can set max items to keep in array

// APCu
$apcu = RemotelyLiving\PHPCacheAdapter\SimpleCache\APCu::create();

// Chain adapter calls through all adapters until values are found in order of FIFO
// so here we would check memory first, then Memcache, then Redis
$chainAdapter = RemotelyLiving\PHPCacheAdapter\SimpleCache\Chain::create($memoryAdapter, $memcachedAdapter, $redisAdapter);

CacheItemPool

Cache Item Pool is the PSR-6 implementation this library provides. You can create a CacheItemPool with a cache extension OR any of the PSR-16 Simple Cache adapters.

$memcached = new \Memcached();
$memcached->addServer('127.0.0.1', 11211);


$redis = new \Redis();
$redis->pconnect('127.0.0.1', 6379, 30);

$memcacheCacheItemPool = RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItemPool::createMemcached($memcached, 'namespace');
// OR
$redisCacheItemPool = RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItemPool::createRedis($redis, 'namespace');
// OR
$inMemoryCacheItemPool = RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItemPool::createMemory($maxItems, 'namespace');
// OR
$apcuCacheItemPool = RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItemPool::createAPCu();
// OR
$cacheItemPool = RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItemPool::createFromSimpleCache($chainAdapter, 'namespace');
// OR
$chain = RemotelyLiving\PHPCacheAdapter\CacheItemPool\ChainBuilder::create('namespace', 300)
    ->addMemory()
    ->addAPCu()
    ->addMemcached($memcached)
    ->addRedis($redis)
    ->build();

Future Development

  • Consider adding a filesystem storage mechanism