webscale/webscale

Cache abstraction library

dev-master 2014-03-23 17:14 UTC

This package is not auto-updated.

Last update: 2024-03-16 11:51:40 UTC


README

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.

WebScale has currently drivers for following data stores:

  • Apc(u)
  • XCache
  • WinCache
  • Redis
  • Memcache(d)
  • Couchbase
  • File system
  • PHP memory

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.

$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.

$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.

$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.

$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
    )
*/