Nan*_*com 7 caching apc cache-invalidation doctrine-orm
我在检索用户(消息应用程序)的新消息数量的查询中使用Doctrine 2的结果缓存:
$query->useResultCache(true, 500, 'messaging.nb_new_messages.'.$userId);
Run Code Online (Sandbox Code Playgroud)
我试图像这样(在我的实体库中)使这个缓存无效:
public function clearNbNewMessagesOfUserCache($userId) {
$cacheDriver = $this->getEntityManager()->getConfiguration()->getResultCacheImpl();
$result = $cacheDriver->delete('skepin_messaging.nbNewMessages.'.$userId);
if (!$result) {
return false;
}
return $cacheDriver->flushAll();
}
Run Code Online (Sandbox Code Playgroud)
因此,我不需要在我的网站的每个页面上进行无用的查询.
我的问题:这是推荐的做法吗?我最终会遇到问题吗?
我想到了构建一个 onFlush 钩子。在那里,所有实体都排队等待插入、更新和删除,因此您可以根据实体名称和标识符等使缓存失效。
不幸的是,我还没有构建任何事件监听器,但我绝对计划为我的项目构建这样的东西。
这是 onFlush 事件的学说文档的链接
编辑: 甚至还有一种更简单的方法来实现事件。在实体类中,您可以将 @HasLifecycleCallbacks 添加到注释中,然后可以使用 @PreUpdate 或 @PrePersist 注释定义函数。每次更新或保留此模型时,都会调用此函数。
/**
* @Entity
* @Table(name="SomeEntity")
* @HasLifecycleCallbacks
*/
class SomeEntity
{
...
/**
* @PreUpdate
* @PrePersist
*/
public function preUpdate()
{
// This function is called every time this model gets updated
// or a new instance of this model gets persisted
// Somethink like this maybe...
// I have not yet completely thought through all this.
$cache->save(get_class($this) . '#' . $this->getId(), $this);
}
}
Run Code Online (Sandbox Code Playgroud)
那么也许这可以用来使实体的每个实例无效?
| 归档时间: |
|
| 查看次数: |
2334 次 |
| 最近记录: |