2017 © Pedro Peláez
 

library cache-engine

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

image

byjg/cache-engine

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

  • Monday, April 9, 2018
  • by byjg
  • Repository
  • 2 Watchers
  • 2 Stars
  • 12,200 Installations
  • PHP
  • 7 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 14 % Grown

The README.md

Cache Engine

Build Status Opensource ByJG GitHub source GitHub license GitHub release, (*1)

A multi-purpose cache engine PSR-6 and PSR-16 implementation with several drivers., (*2)

PSR-16

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:, (*3)

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\AnyDataset\NoSql\Cache\KeyValueCacheEngine Use S3-Like or ClouflareKV as a store for the cache (other repository)
\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\RedisCachedEngine uses the Redis as cache
\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:, (*4)

<?php
$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, (*5)

PSR-6

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., (*6)

You can use one of the factory methods to create a instance of the CachePool implementation:, (*7)

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

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

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

See more PSR-6 examples here, (*9)

List of Available Factory Commands

Note: All parameters are optional, (*10)

Engine Factory Command
No Cache Factory::createNullPool($prefix, $bufferSize, $logger);
Aws S3 See Anydataset-NoSql component
Array Factory::createArrayPool($bufferSize, $logger);
Cloudflare KV See Anydataset-NoSql component
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:, (*11)

  • logger: A valid instance that implement the LoggerInterface defined by the PSR/LOG
  • bufferSize: the Buffer of CachePool
  • prefix: A prefix name to compose the KEY physically
  • servers: An array of memcached servers. E.g.: [ '127.0.0.1:11211' ]
  • config: Specific setup for shmop. E.g.: [ 'max-size' => 524288, 'default-permission' => '0700' ]

Logging cache commands

You can add a PSR Log compatible to the constructor in order to get Log of the operations, (*12)

See log examples here, (*13)

Use a PSR-11 container to retrieve the cache keys

You can use a PSR-11 compatible to retrieve the cache keys. Once is defined, only the keys defined in the PSR-11 will be used to cache., (*14)

<?php
$fileCache = new \ByJG\Cache\Psr16\FileSystemCacheEngine()
$fileCache->withKeysFromContainer(new SomePsr11Implementation());

After the PSR-11 container is defined, when I run:, (*15)

$value = $fileCache->get('my-key');

The key my-key will be retrieved from the PSR-11 container and the value retrieved will be used as the cache key. If it does not exist in the PSR-11 container, an exception will be thrown., (*16)

Install

Just type:, (*17)

composer require "byjg/cache-engine"

Running Unit Testes

vendor/bin/phpunit --stderr

Note: the parameter --stderr after phpunit is to permit run the tests on SessionCacheEngine., (*18)

Dependencies

flowchart TD
    byjg/cache-engine --> psr/cache
    byjg/cache-engine --> psr/log
    byjg/cache-engine --> psr/simple-cache
    byjg/cache-engine --> psr/container

Open source ByJG, (*19)

The Versions

09/04 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Gilberto Magalhães

09/04 2018

4.0.3

4.0.3.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Gilberto Magalhães

11/03 2018

4.0.2

4.0.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Gilberto Magalhães

31/12 2017

4.0.1

4.0.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by João Gilberto Magalhães

27/05 2017

4.0.0

4.0.0.0

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

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

14/02 2017

3.0.0

3.0.0.0

A multi-purpose cache engine in PHP with several drivers. PSR-6 compliant.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

10/08 2016

2.0.0

2.0.0.0

A multi-purpose cache engine in PHP with several drivers. PSR-6 compliant.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

10/08 2016

dev-pre-2.0

dev-pre-2.0

A multi-purpose cache engine in PHP with several drivers. PSR-6 compliant.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

18/11 2015

1.0.4

1.0.4.0

A multi-purpose cache engine in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

12/09 2015

1.0.3

1.0.3.0

A multi-purpose cache engine in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

10/09 2015

1.0.2

1.0.2.0

A multi-purpose cache engine in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

31/08 2015

1.0.1

1.0.1.0

A multi-purpose cache engine in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

21/07 2015

1.0.0

1.0.0.0

A multi-purpose cache engine in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães