CakePHP 缓存用户特定查询数据,最佳实践

发布于 2024-11-04 18:25:04 字数 1172 浏览 2 评论 0原文

我想在我当前正在开发的 Web 应用程序中在模型级别使用缓存。

我对缓存一般内容很满意,例如“最新新闻”或其他非用户特定的内容,我正在寻找依赖于用户特定数据的缓存查询的一些方向。

例如,用户评论列表。查询将需要用户 ID,并且结果将仅特定于该用户,那么我应该如何缓存它?

更重要的是,在更新评论时简单地清除整个缓存似乎效率很低,如果能获得一些有关如何清除用户特定缓存键的建议,那就太好了。

示例

function getEntryByInventory($id,$company_id) {
  $getEntryByInventory = Cache::read('Entry.getEntryByInventory.'.$id.'.'.$company_id);
    if($getEntryByInventory !==false) {
        return $getEntryByInventory;
    } else {    
        $getEntryByInventory =  $this->find('first' , array(
            'conditions' => array(
                'Entry.id' => $id, 
                'Inventory.company_id' => $company_id
            )
        ));
        Cache::write('Entry.getEntryByInventory.'.$id.'.'.$company_id , $getEntryByInventory);
        return $getEntryByInventory;
    }
}

// How would I clear the above cache without knowing the company id?
function beforeSave() {
    Cache::delete('????');
    return true;
}

我可以将此查询的结果保存在缓存中,并通过将 $company_id 添加到缓存键来使其具体化,因此读取和写入缓存就可以了。

但在这种情况下,如果我需要从缓存中删除此键,我将需要 $company_id。

我的问题是,我是否以正确的方式处理这个问题,如果不是,请给我一些指示!

提前致谢。

I would like to employ caching at model level in the current web application I am working on.

I am comfortable with caching general things, like "latest news" or other non-user specific things like that, I am looking for some direction on caching queries which rely on user specific data.

For instance, a list of a User's comments. The query will need the Users ID, and the result will only be specific to that User, so how should I cache this?

More importantly, simply clearing the entire cache when a comment is updated seems very inefficient, it would be nice to get some advice on how to clear user specific cache keys.

Example

function getEntryByInventory($id,$company_id) {
  $getEntryByInventory = Cache::read('Entry.getEntryByInventory.'.$id.'.'.$company_id);
    if($getEntryByInventory !==false) {
        return $getEntryByInventory;
    } else {    
        $getEntryByInventory =  $this->find('first' , array(
            'conditions' => array(
                'Entry.id' => $id, 
                'Inventory.company_id' => $company_id
            )
        ));
        Cache::write('Entry.getEntryByInventory.'.$id.'.'.$company_id , $getEntryByInventory);
        return $getEntryByInventory;
    }
}

// How would I clear the above cache without knowing the company id?
function beforeSave() {
    Cache::delete('????');
    return true;
}

I can save the result of this query in the cache, and make it specific by adding the $company_id to the cache key, so reading and writing to the cache is fine.

But in this instance, if I needed to delete this key from the cache I would require the $company_id.

My question is, am I going about this the correct way, and if not can I get some pointers please!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-11-11 18:25:04

确保最后而不是最先实现缓存。一旦您的应用程序正常运行,您就可以开始缓存特定元素。有些东西不值得缓存(您不能在任何地方缓存所有用户的评论)。存储就是为此而存在的。在尝试深入研究复杂的缓存之前,应该先努力消除瓶颈(其中大部分都是直截了当的)。

Make sure you implement caching last and not first. Once your application works then you can start working on caching specific elements. Some things are just not worth caching (you can't cache all users' comments everywhere). Storage is there for that. Rather work on eliminating the bottlnecks (most of which will be straight forward) before trying to delve into complex caching.

说谎友 2024-11-11 18:25:04

如果您使用 afterSave() 函数而不是 beforeSave() 函数,则保存的数据/信息将在 $this->data 中可用。

 public function afterSave($created, $options = array()) {
        debug($this->data); // contains id of the modified record
        if ($created) {
            Cache::clear('Entry.getEntryByInventory.'.$this->data['Model']['id'].'.'.$this->data['Model']['company_id']);
        }
    }

If you use afterSave() function instead of beforeSave() function, the saved data/information will be available in $this->data.

 public function afterSave($created, $options = array()) {
        debug($this->data); // contains id of the modified record
        if ($created) {
            Cache::clear('Entry.getEntryByInventory.'.$this->data['Model']['id'].'.'.$this->data['Model']['company_id']);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文