dev-master
9999999-dev https://github.com/webscalePHP/webscaleCache abstraction library
MIT
The Requires
The Development Requires
cache redis memcache apc caching memcached wincache session performance apcu couchbase xcache
Wallogit.com
2017 © Pedro Peláez
Cache abstraction library
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)
$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');
$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
/*
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();
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);
$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();
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();
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
)
*/
Cache abstraction library
MIT
cache redis memcache apc caching memcached wincache session performance apcu couchbase xcache