2017 © Pedro Peláez
 

library cache

Unified set of tools for common caching tasks

image

berny/cache

Unified set of tools for common caching tasks

  • Tuesday, October 21, 2014
  • by xphere
  • Repository
  • 1 Watchers
  • 2 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Berny\Cache

This library provides a unified set of tools for common caching tasks., (*1)

SensioLabsInsight, (*2)

Features

Moves from the classic cache interface to a item interface., (*3)

Instalation

From composer/packagist

  • Add "berny/cache": "*@dev" to your composer.json file

From github

Storage

You want to store your cached data somewhere, and this is where StorageInterface comes handy., (*4)

Its only job is to create Items from keys through the getItem($key) method., (*5)

Some implementations of this interface are:, (*6)

  • FilesystemStorage
  • MemoryStorage
  • DoctrineCacheAdapter

Item

Items allow management of ONE key inside a given Storage. You can:, (*7)

  • key(): Get the item's key
  • get(): Get its cached value (if not missed the cache)
  • set($value): Set the cached value
  • miss(): Check if it missed the cache
  • remove(): Clear it

Example

function cacheComplexFunction(ItemInterface $item, Closure $complex)
{
    if ($item->miss()) {
        $value = $complex($item->key());
        $item->set($value);
    }
    return $item->get();
}

$storage = new MemoryStorage();
$result = cacheComplexFunction($storage->getItem('key'), complexCalculation);

Cache

The main brain is Cache, a concrete class which receives a StorageInterface (and optionally a StrategyInterface) to proxy all requests through it. It defines:, (*8)

  • get($key, $default = null): Our plain get by key, returning $default on cache misses.
  • getOrSet($key, $value): Gets cached values by key, storing and returning $value on cache misses.
  • getOrLazySet($key, $callback): Same as getOrSet but with lazy evaluation of $callback on cache misses.
  • set($key, $value): No secrets, a vanilla set by key.
  • remove($key): Remove items only when not missed the cache.
  • getItem($key): Return the ItemInterface created by the underlying StorageInterface.

Example

// Storage is any StorageInterface implementation
$cache = new Cache($storage);

$result = $cache->get('key_1'); // null on cache misses
$result = $cache->get('key_2', 'default'); // 'default' on cache misses
$result = $cache->getOrSet('key_3', 'value'); // 'value' stored on cache misses
$result = $cache->getOrLazySet('key_4', $callback); // $callback called only on cache misses

Strategy

Implement the StrategyInterface to automate generation of concrete keys from any user-domain ones. This allows for automatic cache invalidation., (*9)

If injected into Cache, keys go first through this object before calling setItem($key)., (*10)

Implementations:, (*11)

  • FilesystemStrategy: Treats keys as path filenames. Returns a key with last modified time to invalidate cache entries on file changes.

Example

$path = __DIR__ . '/to-do/';
$strategy = new FilesystemStrategy();
// Storage is any StorageInterface implementation
$cache = new Cache($storage, $strategy);
// Callback only called the first time and every time the $path content changes
$cache->getOrLazySet($path, $callback);

The Versions

21/10 2014

dev-master

9999999-dev https://github.com/xphere/Cache

Unified set of tools for common caching tasks

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Berny Cantos

cache

21/10 2014

1.0

1.0.0.0 https://github.com/xphere/Cache

Unified set of tools for common caching tasks

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Berny Cantos

cache