phoole/cache

Slim and full compatible PSR-16 cache library for PHP

1.1.0 2019-11-18 01:29 UTC

This package is auto-updated.

Last update: 2024-04-18 16:43:19 UTC


README

Build Status Scrutinizer Code Quality Code Climate PHP 7 Latest Stable Version License

Slim and full compatible PSR-16 cache library for PHP

Installation

Install via the composer utility.

composer require "phoole/cache"

or add the following lines to your composer.json

{
    "require": {
       "phoole/cache": "1.1.*"
    }
}

Features

  • Fully PSR-16 compliant.

  • Support all serializable PHP data types.

  • Extra features:

    • Stampede Protection: Whenever ONE cached object's lifetime is less than a configurable stampedeGap time in seconds (60s default), by a configurable stampedePercent (5% default) percentage, it will be considered stale. It may then trigger generating new cache depend on your decision. This feature is quite useful for reducing a single hot item stampede situation.

      // overwrite stampede defaults
      $cache = new Cache($fileAdatpor, [
          'stampedeGap' => 120,   // 120second
          'stampedePercent' => 2  // 2%
      ]);
    • Distributed expiration: By setting distributedPercent (5% default) to a reasonable percentage, system will store each cache item with its TTL(time to live) a small random fluctuation. This will help avoiding large amount of items expired at the same time.

      $cache = new Cache($fileAdaptor, [
          'distributedPercent' => 3,   // 3%, default is 5%
      ]);
  • CacheAwareInterface and CacheAwareTrait

Usage

  • Simple usage

    use Phoole\Cache\Cache;
    
    // using default adaptor and default settings
    $cache = new Cache();
    
    // get with default value 'phoole'
    $name  = $cache->get('name', 'phoole');
      
    // set cache
    $cache->set('name', 'wow');
  • Specify the adaptor

    use Phoole\Cache\Cache;
    use Phoole\Cache\Adaptor\FileAdaptor;
    
    // use file adaptor and specific cache directory 
    $cache = new Cache(new FileAdaptor('/tmp/cache');
  • Use with dependency injection

    use Phoole\Cache\Cache;
    use Phoole\Di\Container;
    use Phoole\Config\Config;
    
    // config cache in the container
    $container = new Container(new Config(
        'di.service' => [
            'cache' => Cache::class
        ],
    ));
    
    // get from container
    $cache = $container->get('cache');
    
    // or static FACADE way
    $cache = Container::cache();

Testing

$ composer test

Dependencies

License