2017 © Pedro PelĆ”ez
 

library cache

image

ceus-media/cache

  • Wednesday, September 13, 2017
  • by kriss0r
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,325 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 4 % Grown

The README.md

Ceus Media Cache

A caching library for PHP, implementing both PSR-6 and PSR-16., (*1)

PSR-6: Cache Interface PSR-16: Simple Cache, (*2)

This library is a storage abstraction layer, which can be used as a cache client., (*3)

It provides CRUD access to several storage backends., (*4)

Branch Release PHP version PHPStan level Monthly downloads Package version License, (*5)

About

This library is a storage abstraction layer, which can be used as a cache client., (*6)

It provides CRUD access to several storage backends., (*7)

Backends

You can use this layer to store and read information using these backends:, (*8)

  • Database: any database supported by PDO
  • Folder: local files
  • IniFile: pairs within a INI file
  • JsonFile: pairs within a JSON file
  • Memcache: pairs within local or remote Memcache server
  • Memory: pairs within local memory, not persistent
  • Noop: dummy cache without any function, fallback if no other cache backend is available, yet
  • Redis: pairs within local or remote Redis server
  • SerialFile: pairs within a local PHP serial file
  • SerialFolder: PHP serial files within a local folder
  • Session: pairs within the HTTP session

Installation

This library is available as composer package:, (*9)

composer require ceus-media/cache

Of cource, you will need to use composer autoloader:, (*10)

<?php
require_once 'vendor/autoload.php';

Usage

PSR-16 - Simple cache

Create cache

use CeusMedia\Cache\Factory as CacheFactory;

$cache  = CacheFactory::createStorage( 'Folder', __DIR__.'/cache' );

This would create a new folder cache in the current working directory, if allowed., (*11)

Write to cache

$cache->set( 'aRandomDigit', rand( 0, 9 ) );

Within the folder there would be file aRandomDigit, holding a digit between 0 and 9., (*12)

Reading from cache

You can later read this information, again:, (*13)

$digit = $cache->get( 'aRandomDigit' );
if( NULL !== $digit ){
    //  cache hit
} else {
    //  cache miss
}

As you can see, the result is NULL, if the requested information is not cached (cache miss)., (*14)

You can check if an information is cached:, (*15)

if( $cache->has( 'aRandomDigit' ){
    //  cache hit
} else {
    //  cache miss
}

PSR-6 - Cache Pool

As defined in PHP-Figs PSR-6 there is a cache pool with items., (*16)

Create cache

use CeusMedia\Cache\CachePoolFactory;

$pool   = CachePoolFactory::createPool( 'Folder', __DIR__.'/cache' );

This would create a new folder cache in the current working directory, if allowed., (*17)

Write to cache

// get an existing or empty item
$item   = $pool->getItem( 'datetime' );

// set the new (or first) value
$item->set( date( DATE_ATOM ) );

// persist item in pool
$pool->save( $item );

Within the folder there would be file datetime, holding a timestamp., (*18)

Reading from cache

You can later read this information, again:, (*19)

$item   = $pool->getItem( 'datetime' );
if( $item->isHit() ){
    $date   = $item->get();
    // ...
} else {
    // ...
}

History

Past

In the past, this library was called Ceus Media Modules: Storage Engine Abstraction, in short CMM_SEA., (*20)

We used it for caching, mostly., (*21)

During migration via different VCSs and due to corporate wide product renaming, it became CeusMedia/Cache on GitHub., (*22)

Since a migration to implement PHP-Figs cache releated PSRs, there are now two ways to use this library., (*23)

Slow storages have been removed to keep an eye on performance., (*24)

Ideas

Connector Factory, (*25)

Replace the current resource strings, used on connecting a cache backend, by connector objects. For each backend there will be a connector class. A factory will ease the creation of these connectors., (*26)

Layered Caches, (*27)

A cache client instance can have several backends, which are used in a defined order. This way, a slow cache backend (like a database or file based cache) can be wrapped by a faster cache backend (like a local memcache server). There are many interesting use cases., (*28)

Flattening Strategy, (*29)

Since some cache backends cannot store structured data, a flattening strategy is needed. At the moment, each backend implements its own strategy. A better way is to select a stategy., (*30)

Cache Manager, (*31)

Several cache client instances can be registered on a cache manager, which is a single resource for larger projects, like an application framework. During actions within the framework, the developer can select between several caches for different purposes., (*32)

More backends, (*33)

  • No-SQL databases: MongoDB, CouchDB

Custom backends, (*34)

Allow to register own cache backends., (*35)


The Versions

13/09 2017

dev-master

9999999-dev

  Sources   Download

GPL-3.0+

The Requires

 

28/06 2017

0.1.1

0.1.1.0

  Sources   Download

GPL-3.0+

The Requires

 

02/03 2017

0.1

0.1.0.0

  Sources   Download

GPL-3.0+

The Requires