yarri / lazy-loader
LazyLoader提供了一种高效的闭包懒加载机制
v2.0
2022-05-10 14:30 UTC
Requires
- php: >=7.1
- atk14/stop-watch: 0.*
- atk14/string4: 0.* >=0.1.1
Requires (Dev)
- atk14/tester: *
README
LazyLoader提供了一种高效的闭包懒加载机制。
在LazyLoader中,可以定义一个或多个命名的闭包。当需要其输出时,任何闭包最多只调用一次。
基本用法
LazyLoader实现了ArrayAccess,因此最简单的方法是像使用关联数组一样使用它。
$lazy_loader = new LazyLoader();
// Setting up a closure,
$lazy_loader["recent_articles"] = function(){
return Article::FindAll(["order_by" => "published_at DESC", "limit" => 10]);
};
// ... another closure
$lazy_loader["top_product"] = function(){
return Product::FindFirst(["order_by" => "pieces_sold DESC"]);
}
// Reading - closure is being executed only during the first occurence of reading
if($lazy_loader["recent_articles"]){
foreach($lazy_loader["recent_articles"] as $article){
// ...
}
}
//
$top_product = $lazy_loader["top_product"];
共有三种方式可以设置闭包或获取其输出。它们都做同样的事情,可以混合使用。
// Setting a closure
$lazy_loader->set("recent_articles",function(){ /* ... */ });
// or
$lazy_loader["recent_articles"] = function(){ /* ... */ };
// or
$lazy_loader->setRecentArticles(function(){ /* ... */ });
// Getting the output
$recent_articles = $lazy_loader->get("recent_articles");
// or
$recent_articles = $lazy_loader["recent_articles"];
// or
$recent_articles = $lazy_loader->getRecentArticles();
还可以涉及参数。在这种情况下,使用驼峰式虚拟方法会很方便。
$lazy_loader->setRecentArticles(function($limit = 10){
return Article::FindAll(["order_by" => "published_at DESC", "limit" => $limit]);
});
$five_recent_articles = $lazy_loader->getRecentArticles(5);
在模板引擎中的使用
LazyLoader可以优雅地用于任何模板引擎,例如在Smarty中。
为Smarty模板准备数据
$smarty->assign("lazy_loader",$lazy_loader);
在Smarty模板中
<h3>Recent Articles</h3>
<ul>
{foreach $lazy_loader.recent_articles as $article}
<li><a href="{$article->getUrl()}">{$article->getTitle()}</a></li>
{/foreach}
</ul>
在ATK14框架中的使用
<?php
// file: app/controllers/application.php
class ApplicationController extends Atk14Controller {
// ..
function _before_render(){
$lazy_loader = new $lazy_loader;
$lazy_loader["recent_articles"] = function(){
return Article::FindAll(["order_by" => "published_at DESC", "limit" => 10]);
};
$this->tpl_data["lazy_loader"] = $lazy_loader;
}
}
最近的文章在每一页的侧边栏中显示。因此,缓存是合适的。
{* file: app/layouts/default.tpl *}
{cache key=sidebar expire=600}
<div class="sidebar">
{render partial="shared/recent_articles" recent_articles=$lazy_loader.recent_articles}
</div>
{/cache}
共享模板不必了解任何关于懒加载的信息。
{* file: app/views/shared/_recent_articles.tpl *}
<h3>Recent Articles</h3>
<ul>
{foreach $recent_articles as $article}
<li><a href="{$article->getUrl()}">{$article->getTitle()}</a></li>
{/foreach}
</ul>
正如您所预期的,"recent_articles"闭包仅在缓存重新创建时执行。
Tracy面板集成
LazyLoader包附带LazyLoaderPanel,可以轻松集成到流行的调试器Tracy (https://packagist.org.cn/packages/tracy/tracy)
$tracy_bar = Tracy\Debugger::getBar();
$tracy_bar->addPanel(new LazyLoaderPanel($lazy_loader));
安装
安装LazyLoader的最佳方式是使用Composer
composer require yarri/lazy-loader
测试
安装开发所需的依赖关系
composer update --dev
运行测试
./test/run_tests.sh
许可证
LazyLoader是免费软件,根据MIT许可证的条款分发。