【发布时间】:2017-03-24 14:23:01
【问题描述】:
有没有办法缓存虚拟字段?我的意思是自动使用它们所属的实体,因为我明白,即使从缓存中检索到实体,也会在必要时生成虚拟字段。
显然我知道我可以亲自处理它,所以(示例):
protected function _getFullName()
{
$fullName = Cache::read('full_name_for_' . $this->_properties['id'], 'users');
if (empty($fullName)) {
$fullName = $this->_properties['first_name'] . ' ' . $this->_properties['last_name'];
Cache::write('full_name_for_' . $this->_properties['id'], $fullName, 'users');
}
return $fullName;
}
但我想知道CakePHP是否可以直接做到这一点。
编辑
上下文。
Post 实体具有text 属性。 text 可以包含图像(作为 html 代码),甚至是远程的。现在我必须在 somewhere 存储文本中包含的第一个图像的 url 及其大小。所以我创建了first_image 虚拟字段,它使用正则表达式。问题在于图像大小:我不能每次都运行getimagesize() 函数,特别是如果图像是远程的,原因很容易理解。那怎么办呢?
【问题讨论】:
标签: cakephp caching cakephp-3.4