2017 © Pedro Peláez
 

library phossa-cache

The PSR-6 compliant PHP caching library.

image

phossa/phossa-cache

The PSR-6 compliant PHP caching library.

  • Tuesday, April 19, 2016
  • by phossa
  • Repository
  • 1 Watchers
  • 2 Stars
  • 24 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

phossa-cache [ABANDONED]

Build Status HHVM Latest Stable Version License, (*1)

See new lib at phoole/cache, (*2)

Introduction

Phossa-cache is a PSR-6 compliant caching library. It supports various drivers and useful features like bypass, encrypt, stampede protection, garbage collect, taggable item etc., (*3)

More information about PSR-6 and PSR-6 Meta, (*4)

Installation

Install via the composer utility., (*5)

composer require "phossa/phossa-cache=1.*"

or add the following lines to your composer.json, (*6)

{
    "require": {
       "phossa/phossa-cache": "^1.0.8"
    }
}

Features

  • Fully PSR-6 compliant. Maybe the most feature-rich PSR-6 caching package you will find at github at this time., (*7)

  • Support all serializable PHP datatypes., (*8)

  • Extensions:, (*9)

    • Bypass: If sees a trigger in URL (e.g. '?nocache=true'), bypass the cache., (*10)

    • Stampede: Whenever cached object's lifetime is less than a configurable time, by certain percentage, the cache will return false on 'isHit()' which will trigger re-generation of the object., (*11)

    • Encrypt: A simple extension to encrypt the serialized content, (*12)

    • GarbageCollect: A simple extension to auto-clean the cache pool., (*13)

    • Taggable: Item is taggable and can be cleared by tag., (*14)

    • DistributeMiss: Even out the spikes of item misses by alter expiration time a little bit., (*15)

  • Drivers, (*16)

    • FilesystemDriver

    The filesystem driver stores cached item in filesystem. It stores cached items in a md5-filename flat file. Configurable settings are, (*17)

    • dir_root: the base directory for the filesystem cache
    • hash_level: hashed subdirectory level. default to 2
    • file_pref: cached item filename prefix
    • file_suff: cached item filename suffix
    /*
     * construct the driver manually
     */
    $driver = new Driver\FilesystemDriver([
        'hash_level'    => 1,
        'file_pref'     => 'cache.',
        'dir_root'      => '/var/tmp/cache',
    ]);
    
    • NullDriver

    The blackhole driver. used as fallback driver for all other drivers., (*18)

    • Fallback driver

    User may configure a fallback driver if the desired driver is not ready. The NullDriver is the final fallback for all other drivers., (*19)

    // default memcache driver
    $driver = new Driver\MemcacheDriver([
        'server' => [ '127.0.0.1', 11211 ]
    ]);
    
    // set a fallback filesystem driver
    $driver->setFallback(new Driver\FilesystemDriver([
        'dir_root' => '/var/tmp/cache'
    ]));
    
    $cache = new CachePool($driver);
    
    • CompositeDriver

    The CompositeDriver consists of two drivers, the front-end driver and the backend driver. User filters cachable objects by defining a tester callable which will determine which objects stores to both ends or backend only., (*20)

    /*
     * set the composite driver
     */
    $driver = new Driver\CompositeDriver(
        // front-end driver
        new Driver\MemcacheDriver([
            'server' => [ '127.0.0.1', 11211 ]
        ]),
    
        // backend driver
        new Driver\FilesystemDriver([
            'dir_root' => '/var/tmp/cache'
        ]),
    
        // other settings
        [
            // if size > 10k, stores at backend only
            'tester' => function($item) {
                if (strlen($item->get()) > 10240) return false;
                return true;
            }
        ]
    );
    
  • Logging, (*21)

    The phossa-cache supports psr-3 compliant logger. Also provides a log() method for logging., (*22)

    /*
    * set the logger
    */
    $cache->setLogger($psrLogger);
    $cache->log('info', 'this is an info');
    

    Or configure with logger at cache init, (*23)

    /*
    * the third argument is used for configuring CachePool
    */
    $cache = new CachePool($driver, [],
      'logger' => $psrLogger
    );
    $cache->log('info', 'this is an info');
    
  • Error, (*24)

    No exceptions thrown during caching process. So only errors will be used., (*25)

    /*
    * create cache pool, exceptions may thrown here
    */
    $cache = new CachePool();
    $cache->setLogger($psrLogger);
    
    $item = $cache->getItem('widget_list');
    $val  = $item->get();
    if ($cache->hasError()) {
      $cache->log('error', $cache->getError());
      $widget_list = compute_expensive_widget_list();
      $item->set($widget_list);
      $item->expiresAfter(3600); // expires after an hour
      $cache->save($item);
      if ($cache->hasError()) $cache->log('error', $cache->getError());
    } else {
      $widget_list = $val;
    }
    
  • I18n, (*26)

    Messages are in Message\Message.php. I18n is possible. See phossa-shared package for detail., (*27)

  • Support PHP 5.4+, PHP 7.0, HHVM., (*28)

  • PHP7 ready for return type declarations and argument type declarations., (*29)

  • PSR-1, PSR-2, PSR-3, PSR-4, PSR-6 compliant., (*30)

Usage

  • The simple usage, (*31)

    /*
     * use the default FilesystemDriver which also set default cache
     * directory to sys_get_temp_dir() .'/cache'
     */
    $cache = new CachePool();
    
    $item = $cache->getItem('widget_list');
    if (!$item->isHit()) {
        $value = compute_expensive_widget_list();
        $item->set($value);
        $cache->save($item);
    }
    $widget_list = $item->get();
    
  • Configure the driver, (*32)

    $driver = new Driver\FilesystemDriver([
        'hash_level'    => 1, // subdirectory hash levels
        'file_pref'     => 'cache.', // cache file prefix
        'file_suff'     => '.txt',   // cache file suffix
        'dir_root'      => '/var/tmp/cache' // reset cache root
    ]);
    
    $cache = new CachePool($driver);
    
  • Use extensions, (*33)

    /*
     * SerializeExtension is the default ext, always used.
     * Second argument is an array of ExtensionInterface or config array
     */
    $cache = new CachePool(
        $driver,
        [
            new Extension\BypassExtension(),
            new Extension\StampedeExtension(['probability' => 80 ])
        ]
    );
    

    or addExtension(), (*34)

    $cache = new CachePool($driver);
    $cache->addExtension(new Extension\BypassExtension());
    
  • Hierarchal cache support, (*35)

    Directory-style hierarchal structure is supported in FilesystemDriver and so other coming drivers., (*36)

    // hierarchy key
    $item = $cache->getItem('mydomain/host1/newfile_xxx');
    
    // ending '/' means delete the hierarchy structure
    $cache->deleteItem('mydomain/host1/');
    

Dependencies

  • PHP >= 5.4.0
  • phossa/phossa-shared 1.0.6
  • psr/cache 1.*

License

MIT License, (*37)

The Versions

19/04 2016

dev-master

9999999-dev https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching library.

  Sources   Download

MIT

The Requires

 

The Development Requires

cache caching psr-6 phossa

18/04 2016

1.0.10

1.0.10.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching library.

  Sources   Download

MIT

The Requires

 

The Development Requires

cache caching psr-6 phossa

18/04 2016

1.0.9

1.0.9.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching library.

  Sources   Download

MIT

The Requires

 

The Development Requires

cache caching psr-6 phossa

04/02 2016

1.0.8

1.0.8.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa

22/01 2016

1.0.6

1.0.6.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa

21/01 2016

1.0.5

1.0.5.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa

21/01 2016

1.0.4

1.0.4.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa

21/01 2016

1.0.3

1.0.3.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa

21/01 2016

1.0.2

1.0.2.0 https://github.com/phossa/phossa-cache

The PSR-6 compliant PHP caching system.

  Sources   Download

MIT

The Requires

 

cache caching psr-6 phossa