\PhalApi_ModelProxy

PhalApi_ModelProxy 模型Model代理 - 重量级数据获取的应对方案

  • 结合缓存,进行对重量级成本高的数据进行缓冲读取
  • 为了传递获取源数据而需要的参数,引入封装成值对象的PhalApi_ModelQuery查询对象
  • 具体子类需要实现源数据获取、返回缓存唯一key、和返回有效期
  • 仅在有需要的情况下,使用此Model代理


实例和使用示例:

class ModelProxy_UserBaseInfo extends PhalApi_ModelProxy {

     protected function doGetData($query) {
        $model = new Model_User();

        return $model->getByUserId($query->id);
     }

     protected function getKey($query) {
        return 'userbaseinfo_' . $query->id;
     }

     protected function getExpire($query) {
        return 600;
     }
}

//最终的调用
$query = new PhalApi_ModelQuery();
$query->id = $userId;
$modelProxy = new ModelProxy_UserBaseInfo();
$rs = $modelProxy->getData($query);

Summary

Methods
Properties
Constants
__construct()
getData()
No public properties found
No constants found
doGetData()
getKey()
getExpire()
$cache
N/A
No private methods found
No private properties found
N/A

Properties

$cache

$cache : 

Type

Methods

__construct()

__construct(\PhalApi_Cache  $cache = NULL) 

为代理指定委托的缓存组件,默认情况下使用DI()->cache

Parameters

\PhalApi_Cache $cache

getData()

getData(\PhalApi_ModelQuery  $query = NULL) : mixed

获取源数据 - 模板方法

Parameters

\PhalApi_ModelQuery $query

查询对象

Returns

mixed —

返回源数据,但在失败的情况下别返回NULL,否则依然会穿透到此

doGetData()

doGetData(  $query) 

获取源数据 - 具体实现

Parameters

$query

getKey()

getKey(  $query) 

返回唯一缓存key,这里将$query传入,以便同类数据根据不同的值生成不同的key

Parameters

$query

getExpire()

getExpire(  $query) 

返回缓存有效时间,单位为:秒

Parameters

$query