Skip to content

Repository files navigation

cache

Build StatusScrutinizer Code QualityCode ClimatePHP 7Latest Stable VersionLicense

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 = newCache($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 = newCache($fileAdaptor, [
      'distributedPercent' => 3, // 3%, default is 5%
      ]);
  • CacheAwareInterface and CacheAwareTrait

Usage

  • Simple usage

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

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

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

Testing

$ composer test

Dependencies

License

About

Slim and full compatible PSR-16 cache library for PHP

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages