我应该在Memcache中存储数组还是单个项目? [英] Should I store an array or individual items in Memcache?

查看:66
本文介绍了我应该在Memcache中存储数组还是单个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我们将一些查询结果存储在Memcache上.经过更多调查后,我发现许多人将每个单独的项目保存在Memcache中.这样做的好处是,他们可以在任何其他请求下从Memcache中获取这些项目.

Right now we are storing some query results on Memcache. After investigating a bit more, I've seen that many people save each individual item in Memcache. The benefit of doing this is that they can get these items from Memcache on any other request.

存储数组

$key = 'page.items.20';
if( !( $results = $memcache->get($key) ) )
{
    $results = $con->execute('SELECT * FROM table LEFT JOIN .... LIMIT 0,20')->fetchAll();
    $memcache->save($results, $key, 3600);
}
...

优点:

  • 更容易

缺点:

  • 如果我更改单个项目,则必须删除所有缓存(这很麻烦)
  • 我可以得到重复的结果(在不同的查询中使用相同的项目)

vs

存储每个项目

$key = 'page.items.20';
if( !( $results_ids = $memcache->get($key) ) )
{
    $results = $con->execute('SELECT * FROM table LEFT JOIN .... LIMIT 0,20')->fetchAll();

    $results_ids = array();
    foreach ( $results as $result )
    {
        $results_ids[] = $result['id'];
        // if doesn't exist, save individual item
        $memcache->add($result, 'item'.$result['id'], 3600);
    }

    // save results_ids 
    $memcache->save($results_ids, $key, 3600);
}
else
{
    $results = $memcache->multi_get($results_ids);
    // get elements which are not cached
    ...
}
... 

优点:

  • 我没有将相同的项目存储在Memcache中两次
  • 更容易使几个查询的结果无效(只是我们更改的项目)

缺点:

  • 更复杂的业务逻辑.

您怎么看?每种方式还有其他优点或缺点吗?

What do you think? Any other PROS or CONS on each way?

某些链接

  • Post explaining the second method in Memcached list
  • Thread in Memcached Group

推荐答案

抢先统计并尝试计算点击率或可能的改进(如果您缓存完整查询而不是在MC中抓取单个项目).对此类代码进行概要分析对于实际了解您的理论如何应用也很有帮助.

Grab stats and try to calculate a hit ratio or possible improvement if you cache the full query vs doing individual item grabs in MC. Profiling this kind of code is also very helpful to actually see how your theory applies.

这取决于查询的作用.如果您有一组用户,然后想与其中一些朋友争夺十大音乐亲和力",那么这两种缓存都值得: -每个朋友(实际上是网站的每个用户) -每个用户的前10个查询(空间比CPU时间便宜)

It depends on what the query does. If you have a set of users and then want to grab the "top 10 music affinity" with some of those friends, it is worth to have both cachings: - each friend (in fact, each user of the site) - the top 10 query for each user (space is cheaper than CPU time)

但是一般来说,值得在MC中存储所有将经常使用的单个实体(在同一代码执行中,在后续请求中或由其他用户使用).然后,诸如CPU或资源密集型查询和数据处理之类的事情要么由MC-them进行,要么将它们委托给异步.作业而不是实时进行(例如,排名前10位的网站用户不需要实时进行,可以每小时或每天进行更新). 当然要考虑到,如果您存储和MC单个实体,则必须从数据库中删除所有参照完整性,以便能够单独或成组地重复使用它们.

But in general it is worth to store in MC all individual entities that are going to be used frequently (either in the same code execution, or in subsequent requests or by other users). Then things like CPU or resource heavy queries and data processings either MC-them or delegate them to async. jobs instead of making them realtime (e.g. Top 10 site users doesn't needs to be realtime, can be updated hourly or daily). And of course taking into account that if you store and MC individual entities, you have to remove all referential integrity from the DB to be able to reuse them either individually or in groups.

这篇关于我应该在Memcache中存储数组还是单个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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