corollarium / cachearium
在您的PHP应用程序中缓存。快速、简单且易于失效。
README
Cachearium
PHP应用程序的高级缓存。又一个?不,这个更好。快速、简单且易于失效。包括
- 递归缓存系统,所有你想要的嵌套的俄罗斯套娃
- 易于与您的现有类集成
- 基于键的缓存过期,不再需要为失效内容而头疼
- 多个依赖项
- 生命周期过期,因为东西会变质
- 低级缓存存储访问,当你想要直接操作时
- 大量的示例和测试可供复制粘贴
- 可变片段,用于几乎相同但又不完全相同的东西
- 可插拔后端模块:RAM、Memcached、文件系统,并且你可以添加自己的
- 详细日志,用于性能分析和调试,还可以看到网页中实际缓存的内容
Cachearium是由Corollarium开发的,因为我们需要一个出色的缓存系统。
安装
Composer
将此添加到您的composer.json中:查看Packagist
如果您喜欢最新版本,带有最新的bug
"corollarium/cachearium": "dev-master"
手册
没有composer?别担心!
- 下载包
- 包含
require_once('cachearium/Cached.php');
调试和性能分析
实时缓存探测
图像显示缓存调试探测。粉色区域未缓存。绿色区域已缓存。注意它们是嵌套的。红色方块显示包含每个缓存命中/未命中的信息的对话框,这样你可以轻松地看到缓存键、使用了哪个后端以及其他相关信息。
探测仅在调用start()/end()时可用。
$cache::$debugOnPage = true; ... if (!$cache->start($key)) { // some stuff $cache->end(); } ... // this is required for the probes $cache->footerDebug();
查看详细日志
$cache->setLog(true); .... $cache->report(); // will print a detailed report
用例/示例
查看example/目录,因为那里有所有内容。真的,只需将Web服务器指向那里并开始玩。
存储单个值并使其无效
这是基本的存储。
$data = 'xxxx'; // store $cache = CacheAbstract::factory('your backend'); $cache->store($data, new CacheKey('Namespace', 'Subname')); // get it later try { $data2 = $cache->get(new CacheKey('Namespace', 'Subname')); // $data2 == 'xxxx'; } catch (NotCachedException($e)) { // handle not cached } // store new value with automatic invalidation $data = 'yyy'; $cache->store($data, new CacheKey('Namespace', 'Subname'));
使用 CacheData 进行存储
CacheData 提供了一个更复杂的类来存储值。
$data = 'xxxx'; // store $cache = CacheAbstract::factory('your backend'); $cache->storeData(new CacheData(new CacheKey('Namespace', 'Subname'), $data)); // get it later try { $data2 = $cache->getData(new CacheKey('Namespace', 'Subname')); // $data2->getFirstData() == 'xxxxx' } catch (NotCachedException($e)) { // handle not cached } // store new value with automatic invalidation $lifeTime = 3000; $fancyData = 'someData'; $cache->storeData(new CacheData(new CacheKey('Namespace', 'Subname'), $fancyData), $lifeTime);
存储具有多个依赖关系的值
您可以有多个依赖关系,这样您就可以使与某个键相关的所有缓存数据无效。
$cache = CacheAbstract::factory('your backend'); // create a storage key and bucket $key = new CacheKey('Namespace', 'Subname'); $cd = new CacheData($key, $data); // add dependencies. setDependencies will generate immediately, avoiding races. // otherwise you find results, the DB changes in another process and you get a // stale dependency. note that addDependencies does not do that, leaving the // hash to be calculated later $dep = new CacheKey('Namespace', 'SomeDep'); $cd->setDependencies([$dep]); // store. $data = 'xxxx'; $cache->storeData($cd); // at this point $cache->get($key) will return your data // invalidate a dependency. This will be called on your write method. $cache->invalidate($dep); // at this point $cache->get($key) will throw an NotCachedException
示例:存储搜索并在写入属性时使其无效
function someSearch() { $key = new CacheKey('search', 'someSearch'); // keys for this data $cache = CacheAbstract::factory('backend'); try { return $cache->get($key); // TODO } catch (NotCachedException($e)) { // handle not cached below } $searchdata = getSomeData(); // results of some horrible slow query // attributes that are used in this search $dependencies = [ new CacheKey('attribute', 'name'), new CacheKey('attribute', 'description') new CacheKey('attribute', 'cost') ]; // create cache data $cd = $cache->storeData( (new CacheData($key, $searchdata)) ->setDependencies($dependencies); ); return $searchdata; } function writeSomeStuff() { // changed or added some attribute value in some object $cache = CacheAbstract::factory('backend'); $cache->invalidate(new CacheKey('attribute', 'name')); // invalidates any cache that depends on this key }
与您可轻松清理的对象/模型关联的缓存
您可能有一个 MVC 应用程序。模型类可以轻松缓存数据
class Foo extends Model { use Cached; /** * Unique object id in your application (primary key) */ public function getId() { return $this->id; } public function cacheClean() { $cache = CacheAbstract::factory('backend'); $cache->clean('Foo', $this->getId()); } public function save() { // save stuff on db $this->cacheClean(); // clear any cache associated with this item } public function cacheStore($data, $key) { $cache = CacheAbstract::factory('backend'); return $cache->save($data, 'Foo', $this->getId(), $key); } public function cacheGet($key) { $cache = CacheAbstract::factory('backend'); return $cache->get('Foo', $this->getId(), $key); } }
内容嵌套缓存。对于生成由片段组成的 HTML 非常有用
这使用俄罗斯套娃方法来冒泡任何使无效的操作。这意味着如果您有一个项目列表,并且更改了其中的一个,则只需使自己的缓存条目和整个列表的条目无效。您可以通过单个数据库击中重新生成列表。
$cache = CacheAbstract::factory('your backend'); $cache->start(new CacheKey('main')); $cache->start(new CacheKey('header')); $cache->end(); foreach ($somestuff as $stuff) { $stuff->render(); } $cache->start(new CacheKey('footer')); $cache->end(); $cache->end(); class Stuff { public function getCacheKey() { return new CacheKey('stuff', $this->getId()); } public function write() { write_some_stuff(); $cache = CacheAbstract::factory('your backend'); $cache->clean($this->getCacheKey()); } public function render() { $cache = CacheAbstract::factory('your backend'); $cache->start($stuff->getCacheKey()->setSub('render')); $html = '<p>some html here</p>'; // other dependency if you have it $cache->addDependency($otherstuff->getCacheKey()); $cache->end(); } }
具有可变片段的缓存
这是如何处理类似网站头部的东西,除了每个用户都略有不同的小部分之外,几乎完全相同。
function callbackTesterStart() { return rand(); } $key = new CacheKey("startcallback", 1); $cache->start($key); echo "something "; // this will never be cached, every call to start() will use the rest // of the cached data and call the callback everytime for the variable data $cache->appendCallback('callbackTesterStart'); // everything goes on as planned here echo " otherthing"; $output = $cache->end(false);
始终向缓存键添加一些特定的东西
比如说,你有一个多语言网站。缓存片段始终需要将语言作为键的一部分。Cachearium 通过创建一个特殊函数提供了一个简单的方法来完成此操作
function application_cacheDependencies() { // can return an array or a string return [Application::getLanguage(), somethingelse()]; }
这将在每次调用 start() 时自动添加到您的键中。如果您需要覆盖单个调用,请使用 recursiveStart()。
清理房屋
您可以使用 $cache->clear()
或 CacheAbstract::clearAll()
清除整个缓存。
后端
空
什么也不做。您可以使用它在不更改任何代码调用的情况下关闭测试中的缓存。
RAM
在请求期间仅在 RAM 中缓存。对于不应该在请求之间持久化的快速缓存很有用。
Memcache
使用 Memcache 作为后端,并在运行期间临时存储数据以避免重复请求。
文件系统
存储在文件系统中。