Code Monkey home page Code Monkey logo

php-cache-engine's Introduction

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

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\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:

<?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

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.

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

<?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:

$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:

  • 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

See log examples here

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.

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

After the PSR-11 container is defined, when I run:

$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.

Install

Just type:

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.

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

php-cache-engine's People

Contributors

byjg avatar farmerau avatar techhead avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

php-cache-engine's Issues

InvalidArgumentException not PSR-4 compliant

The exception is in a subdirectory Exception, but its namespace doesn't reflect that. I'd move it to the parent folder, since it's the only file anyway and it already has "Exception" in its name. This causes a warning when installing this as dependency in php-swagger-tests with a newer Composer version.

BTW, if you're at it anyway, you could enable testing with PHP 7.4.

Cheers!

Uli

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.