CakePHP缓存用户特定的查询数据,最佳实践 [英] CakePHP caching user specific query data, best practice

查看:47
本文介绍了CakePHP缓存用户特定的查询数据,最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在当前正在使用的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.

解决方案

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.

这篇关于CakePHP缓存用户特定的查询数据,最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆