2017-25 © Pedro Peláez
 

library webscale

Cache abstraction library

image

webscale/webscale

Cache abstraction library

  • Sunday, March 23, 2014
  • by sgtk
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

WebScale

WebScale is a cache abstraction library under development. It is based on proposed PSR-6 interfaces. First stable release (1.0) is expected to be ready soon after PSR-6 is finalised., (*1)

WebScale has currently drivers for following data stores: - Apc(u) - XCache - WinCache - Redis - Memcache(d) - Couchbase - File system - PHP memory, (*2)

How can I contribute?

  • Fork, hack, commit and push. Do not put your name to source code files (@author tags).
  • Share your (non-PSR-6) ideas on the issues tab.
  • Go here and push PSR-6 forward.

Usage

Set up your pools

$driver = new WebScale\Driver\Apc;

// items in different pools are separated from each other
$userpool = new WebScale\Pool($driver, 'users');
$blogpostpool = new WebScale\Pool($driver, 'blogposts');

Get and set items

$item = $pool->getItem('foo');
if ($item->isHit()) {
    /*
        Item::get() does not make a second call to your
        cache backend: value is already there.
    */
    $value = $item->get();
} else {
    $value = doSomeExpensiveStuff();
    $item->set($value, 3600);
}
// now do something with the value

Delete items

/*
    Don't worry: item's value isn't actually fetched
    unless you call Item::isHit or Item::get before
    deleting it.
 */
$pool->getItem('foo')->delete();

/*
    Invalidate all items from a pool.
 */
$pool->clear();

Logging

You can use any PSR-3 compatible logger., (*3)

$driver = WebScale\Driver\Factory::getRedisDriver(array(
    'host' => 'localhost',
    'port' => 6379
));

$logger = new Monolog\Logger('log', array(
    /* handlers */
));

$driver->setLogger($logger);

Session handler

$driver = WebScale\Driver\Factory::getMemcachedDriver(array(
    'host' => 'localhost',
    'port' => 11211
));

$handler = new WebScale\Session\Handler($driver);
$handler->register();

session_start();

Cache another Session handler. This allows you to store sessions in a long-term storage (like database) while still keeping currently active sessions in the cache., (*4)

$driver = WebScale\Driver\Factory::getMemcachedDriver(array(
    'host' => 'localhost',
    'port' => 11211
));

$pdoHandler = new Acme\PdoSessionHandler($pdo);

$handler = new WebScale\Session\DecoratingHandler($driver, $pdoHandler);
$handler->register();

session_start();

Nested pools

Pool can also have nested subpools. Clearing subpool does not affect it's parent or siblings. This functionality is not part of the current PSR-6 draft., (*5)

$mainpool = new WebScale\Pool($driver, 'example.com');
$postpool = $mainpool->getSubPool('posts');
$userpool = $mainpool->getSubPool('users');

// Invalidate items from the userpool.
$userpool->clear();

// Invalidate all items from the main pool and it's subpools.
$mainpool->clear();

Piping multiple operations at once

This functionality should be considered experimental. It is (obviously) faked with some drivers. Not part of the current PSR-6 draft., (*6)

$collection = $pool->getItems(array('foo', 'bar', 'baz'));

$output = $collection->pipe(function ($collection) use ($db) {
    foreach ($collection as $key => $item) {
        if (!$item->isHit()) {
            $value = $db->xyz->findOne(array('key' => $key));
            $item->set($value);
        }
    }
});

print_r($output);
/*
    Array
    (
        [foo] => value
        [bar] => value
        [baz] => value
    )
*/

The Versions

23/03 2014

dev-master

9999999-dev https://github.com/webscalePHP/webscale

Cache abstraction library

  Sources   Download

MIT

The Requires

 

The Development Requires

cache redis memcache apc caching memcached wincache session performance apcu couchbase xcache