webvariants / babelcache
Requires
- php: >=5.3.0
Requires (Dev)
- apigen/apigen: ~2.8
Suggests
- predis/predis: install this to use the Redis adapter
README
BabelCache 是一个功能完整的 PHP 5.3+ 缓存库。它支持多种适配器、命名空间缓存,并提供对 PSR Cache Proposal 的实验性支持。
支持的缓存后端包括
- APC
- AWS ElastiCache
- 黑洞(用于透明地禁用缓存)
- 文件系统
- Memcached (
`php_memcache
,
php_memcached
` 和一个带有 SASL 认证的 PHP-only memcached 实现) - MariaDB & MySQL
- 内存(仅运行时缓存)
- Redis(需要纯 PHP
`predis/prdis
` 库) - SQLite
- WinCache
- XCache
- ZendServer
您可以存储任意元素(资源和闭包除外)。BabelCache 总是尊重它们的类型,因此当您存储一个 int 时,您将获得一个 int。
安装
将以下要求添加到您的 composer.json
:::json
{
"require": {
"webvariants/babelcache": "$VERSION"
}
}
将 $VERSION
替换为 Packagist 上可用的版本之一。使用 `composer update`
安装 BabelCache 和 Composer 自动加载器。
用法
在大多数情况下,您将想要使用工厂为您创建缓存适配器。您可以使用预置的 `wv\BabelCache\SimpleFactory`
或,如果您需要更多控制,扩展 `wv\BabelCache\Factory`
。
::::php
<?php
$factory = new wv\BabelCache\SimpleFactory();
$adapter = $factory->getAdapter('apc');
$adapter->set('world', 'dominated');
当然,如果您需要,也可以自己实例化所有类。
概览
BabelCache 分为四个部分
适配器
适配器是 BabelCache 的基本构建块。每个技术(APC、Memcached、Redis、...)有一个适配器,并且每个适配器仅实现一个非常基本的 键值接口 (`set
,
get
,
delete
,
exists
,
clear
`)
您可以根据需要使用适配器,因此可以有一个平面的键值存储。例如
::::php
<?php
$factory = new MyFactory(); // extends wv\BabelCache\Factory
$adapter = $factory->getAdapter('apc');
$adapter->set('key', 'value');
print $adapter->get('key'); // prints 'value'
缓存
缓存实现了一个高级的 命名空间接口,其中元素根据命名空间和键分组。这允许部分刷新。有关此概念的更多信息,请参阅完整的 文档。
有一个 `Generic`
实现,它使用键值适配器在之上构建一个命名空间系统。这增加了一些开销,因为命名空间是版本化的,缓存必须与后端进行更多的往返。因此,支持自身结构化存储的适配器(例如文件系统或内存适配器)有对应的缓存类。因此,文件系统适配器有一个特定的文件系统缓存实现,使用目录来管理命名空间。
一般情况下,工厂会自动处理构建最佳实现。然而,您可以强制工厂创建一个泛型缓存和适配器的糟糕组合。
构建缓存与构建适配器一样简单
::::php
<?php
$factory = new MyFactory(); // extends wv\BabelCache\Factory
$adapter = $factory->getCache('apc'); // returns a Generic instance wrapping the APC adapter
$adapter = $factory->getCache('filesystem'); // returns a FilesystemCache instance
$adapter->set('my.awesome.namespace', 'key', 'value');
print $adapter->get('my.awesome.namespace', 'key'); // prints 'value'
PSR 包装器
BabelCache 不遵循 PSR 建议。它跳过了 `Item`
对象,并将所有方法放在服务上而不是项目上。我们认为在对象中封装缓存数据是一种不必要的开销。
然而,要在 PSR 生态系统中使用 BabelCache,您可以选择性地包装 BabelCache 以使其符合 PSR。为此,使用 `wv\BabelCache\Psr`
中的实现。
::::php
<?php
$factory = new MyFactory();
$pool = $factory->getPsrPool('apc'); // returns a Psr\Generic\Pool instance wrapping the APC adapter
$pool->getItem('mykey')->set('value');
$item = $pool->getItem('mykey'); // returns an Item
print $item->get();
注意:PSR 建议仍然只是一个建议,所以还没有官方接口。在它们可用之前,您必须自己提供,因为 BabelCache 只包含它们的实现。
装饰器
此外,还有一些可用的装饰器,它们在缓存上添加了额外的行为,例如 `Expiring`
缓存,它将过期时间透明地编码到缓存值中。
要使用装饰器,只需将其应用于您的缓存实例。如果您需要,可以混合使用装饰器。
::::php
<?php
$factory = new MyFactory();
$cache = $factory->getCache('apc');
// make the cache handle timeouts, using 10s as the default timeout
$cache = new wv\BabelCache\Decorator\Expiring($cache, 10);
// set a value that expires in 10 seconds
$cache->set('name.space', 'key', 'value');
// set a value that expires in 42 seconds
$cache->set('name.space', 'key', 'value', 42);
// bring back the old BabelCache 1.x interface
$cache = new wv\BabelCache\Decorator\Compat($cache);
$cache->flush('name', true); // it's clear() now
监禁
通常,您会希望为您的组件之一拥有一个简单的键值缓存。如果您,您 可以 只给他们一个缓存适配器并完成。不幸的是,当涉及到清除缓存时(因为 APC 中的“清除”实际上意味着“清除整个用户空间缓存”),这将会很糟糕。
为了确保您可以使用多个缓存适配器到同一存储,并且仍然可以进行命名空间化的缓存清除,您可以使用 Jailed 适配器。此适配器将常规的、完整的缓存监禁到固定命名空间,并且只会清除那个。
::::php
<?php
$factory = new MyFactory();
$adapter = $factory->getAdapter('apc');
// The following will work just fine, if you can live with service A
// wiping the cache from service B:
$imaginaryServiceA = new MyFooService($adapter);
$imaginaryServiceB = new MyBarService($adapter);
// to jail them, you can wrap a cache like this:
// (The third argument controls whether clearing will be recursive or not)
$cache = $factory->getCache('apc');
$adapterA = new wv\BabelCache\Adapter\Jailed($cache, 'service.foo', true);
$adapterB = new wv\BabelCache\Adapter\Jailed($cache, 'service.bar', true);
$imaginaryServiceA = new MyFooService($adapterA);
$imaginaryServiceB = new MyBarService($adapterB);
// Now imaginaryServiceA can clear its cache until it's blue in the face
// without affecting the other service's cache.
许可
BabelCache 在 MIT 许可证下授权 - 有关详细信息,请参阅 LICENSE 文件。