管理密钥(在memcache中)以防止过时的缓存值的最佳方法是什么? [英] Whats the best way to manage keys (in memcache ) to prevent stale cached values?

查看:77
本文介绍了管理密钥(在memcache中)以防止过时的缓存值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的站点上实现了memcache,这已经承受了沉重的mysql负载(mysql已尽可能优化).它解决了我所有的负载问题,并且站点运行良好.

Ive recently implemented memcache on my site which has been under heavy mysql load (mysql was as optimized as I could make it). It solved all my load issues, and site is running beautifully.

我现在面临的问题是过时的缓存值.我在大多数页面上都有1小时的自动到期时间,而且在数据库更改时,Im还会删除密钥,但是Im难以跟踪并有效清除所有密钥.

The problem that Im facing now is stale cached values. I have 1hr auto expiration times on most pages, and Im also deleting the key when the value in the DB chnages, but Im having difficulty keeping track and efficiently clearing out all the keys.

在某些页面上,它是微不足道的.我可以将密钥设置为item_id(例如,item_4653),并在更新其数据或删除该项目时清除密钥.

On some pages, its trivial. I can make the key be item_id (item_4653 for example), and when the data for it is updated, or the item is deleted, the key is cleaned out.

但是在大​​多数页面上,我采用脚本文件名+ querystring,使用md5作为脚本,并将其用作内存缓存中的键.这对于复杂的url(非常常见)特别有用.

But on most pages, I take the script filename + querystring, md5 it, and use that as the key in memcache. This is especially useful for complex urls (which are very common).

例如,我已加载以下页面.

For example I have the following page loaded.

index.php?search_keywords = & search_section = 1& sort = release& page = 2

index.php?search_keywords=good&search_section=1&sort=release&page=2

它将包含一个项目列表,这些列表将从memcache中获取.然后,另一个用户提交了一个标题为" good "的项目,该项目恰好在值的范围内,它将出现在页面2上,除非它不会出现在此处,直到缓存被刷新.这使事情变得更加复杂的原因是,新添加的项目还将出现在index.php?sort = newest以及index.php?category = some_category?page = 1等上.每一个项目都有唯一的键(脚本名称的md5 +查询字符串).

It will contain a list of items, which will be fetched from memcache. Another user then submits an item, which has "good" in its title, and it happens to be in the range of values, where it would appear on page 2, except it will not appear there, until the cache is refreshed. What makes this even more complicated, is that the newly added item will also appear on index.php?sort=newest, as well as index.php?category=some_category?page=1 and etc. Each 1 of those will have a unique key (md5 of the script name + query string).

因此,如果从实时数据库中获取新添加的项目,则它们可能会出现在数十个页面上,但是直到更新过时的缓存之前,在其中的任何页面上都不可见.唯一的选择是等待商品自动过期.

So the newly added item might appear on dozens of pages, if they were fetched from a live DB, but it wont be visible on any of them until the stale cache is updated. The only option is to wait for the item to expire automatically.

这个问题在我的论坛(自定义编码)上变得更加明显,对于所有可能的缓存页面组合,必须按需更新值.可以说我有4个页面线程,并且在第2页上我注意到3个垃圾邮件.删除它们后,将重建第2页,但是它还必须重建第3页和第4页,否则在新重建的第2页上会有重复的帖子,以及旧页面3.这只是一个示例,以此类推.

This problem becomes even more pronounced on my forum (custom coded), where the values HAVE to be updated on demand, for all possible cached page combinations. Lets say I have 4 page thread, and I notice 3 spam posts on page 2. After deleting them, page 2 is rebuilt, but then it also has to rebuild pages 3 and 4, otherwise there will be duplicate posts on newly rebuild page 2, and old page 3. Thats just 1 example to..... there are dozens of these scenarios.

有什么想法吗?

推荐答案

由于您要在memcached中缓存整个页面,因此您的页面无法彼此共享数据库中的缓存数据.假设我有page1.php和page2.php,其中 page1 page2 作为memcached中的键.两个页面均显示项目.我添加一个新项目.现在,我必须到期 page1 page2 .

Since you are caching entire pages in memcached, your pages can't share cached data from the database with each other. Say I have page1.php and page2.php, with page1 and page2 as keys in memcached. Both pages display items. I add a new item. Now I have to expire page1 and page2.

相反,我可以在memcached中有一个 items 键,page1.php和page2.php都用来显示项目.添加新项目时,项目键失效(或者更好的是更新其值),并且page1.php和page2.php都是最新的.

Instead, I could have an items key in memcached, that page1.php and page2.php both use to display items. When I add a new item, I expire the items key (or better, update it's value), and both page1.php and page2.php are up-to-date.

如果您仍想缓存整个页面,则可以在密钥中添加信息,这些信息将在缓存数据更改时更改(如果数据更改频率太高,则没有意义).例如:

If you still want to cache the entire page, you could add information to your keys that will change when data being cached changes (this wouldn't make sense if the data changes too often). For instance:

"page1:[timestamp of newest item]"

这样,您可以查找最新项的时间戳,便宜的查询,并以此构建您的缓存键.一旦添加了新项目,缓存键将更改,并自动过期.这种方法意味着您仍然必须每次都访问数据库以查看最新项的时间戳.

This way you can look up the timestamp of the newest item, an inexpensive query, and build your cache key with it. Once a newer item is added, the cache key will change, automatically expiring. This method means you still have to hit the database to see what the newest item's timestamp is, every time.

这篇关于管理密钥(在memcache中)以防止过时的缓存值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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