2016-02-16

iphp 框架增加 lazyload 特性

Views: 15425 | Add Comments

基类中为了方便原则, 一般会加载所有可能用到的属性到 Context 中, 但这会导致性能问题, 所以需要 lazyload 机制. 例如, 某些属性只有在子类中被用到时, 才会真正地去查询数据库, 如果子类中不使用这些属性, 则不会发生数据库请求.

原型:

Context::lazyload($name, callable $callback_func);

示例:

class AppController extends Controller
{
    function init($ctx){
        parent::init($ctx);
        $ctx->lazyload('account', array($this, 'ctx_lazyload'));
    }

    private function ctx_lazyload($name, $ctx){
        if($name == 'account'){
            return Account::get($ctx->user->id);
        }
    }
}

Related posts:

  1. 开发搜索引擎 – PHP中文分词
  2. 消除JavaScript闭包的一般方法
  3. 使用ServletContextListener在服务器启动和关闭时创建和关闭缓存
  4. 集成于 iphp 框架的 PHP 并发模型和工具
  5. 如何让 PHP json_encode 函数不转义中文?
Posted by ideawu at 2016-02-16 11:41:54

Leave a Comment