Cache Engine

Build Status Opensource ByJG GitHub source GitHub license GitHub release

A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers.


PSR-16 defines a Simple Cache interface with less verbosity than PSR-6. Below a list of engines available in this library that is PSR-16 compliant:

Class Description
\ByJG\Cache\Psr16\NoCacheEngine Do nothing. Use it for disable the cache without change your code
\ByJG\Cache\Psr16\ArrayCacheEngine Local cache only using array. It does not persists between requests
\ByJG\Cache\Psr16\FileSystemCacheEngine Save the cache result in the local file system
\ByJG\Cache\Psr16\MemcachedEngine Uses the Memcached as the cache engine
\ByJG\Cache\Psr16\SessionCachedEngine uses the PHP session as cache
\ByJG\Cache\Psr16\ShmopCachedEngine uses the shared memory area for cache

To create a new Cache Instance just create the proper cache engine and use it:

$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine();

// And use it:
if ($cache->has('key')) {
    // Do the complex code to get the value to be cached
    $object = callComplexCode();
    // Save to cache
    $cache->set('key', $object);
$object = $cache->get('key');

See more PSR-16 examples here


The PSR-6 implementation use the engines defined above. PSR-6 is more verbosity and have an extra layer do get and set the cache values.

You can use one of the factory methods to create a instance of the CachePool implementation:

$cachePool = \ByJG\Cache\Factory::createFilePool();

OR just create a new CachePool and pass to the constructor an instance of a PSR-16 compliant class:

$cachePool = new CachePool(new FileSystemCacheEngine());

See more PSR-6 examples here

List of Available Factory Commands

Note: All parameters are optional

Engine Factory Command
No Cache Factory::createNullPool($prefix, $bufferSize, $logger);
Array Factory::createArrayPool($bufferSize, $logger);
File System Factory::createFilePool($prefix, $bufferSize, $logger);
Memcached Factory::createMemcachedPool($servers[], $bufferSize, $logger);
Session Factory::createSessionPool($prefix, $bufferSize, $logger);
Redis Factory::createRedisCacheEngine($server, $pwd, $bufferSize, $logger);
Shmop Factory::createShmopPool($config[], $bufferSize, $logger);

The Common parameters are:

Logging cache commands

You can add a PSR Log compatible to the constructor in order to get Log of the operations

See log examples here


Just type:

composer require "byjg/cache-engine=4.9.*"

Running Unit Testes

vendor/bin/phpunit --stderr

Note: the parameter --stderr after phpunit is to permit run the tests on SessionCacheEngine.

Open source ByJG