1

我有像 facebook 或 SocialEngine 这样的社交引擎系统......用户主页显示他们的朋友的帖子和他们的活动。我想缓存这个主页以减少用户主页中 ex 的 mysql 服务器负载存在 10 个帖子,所以我们有这样的场景:

if cached is exist 

   show homepage cached // fo 10  new post

else
  homepage db query // fetch 10 new post
  cache homepage to memcache // we cache it for 5 minitues 

我有一个问题:一个用户的朋友写了一个新帖子,所以当用户刷新主页时,他直到 5 分钟才能看到朋友的新帖子,有没有办法在主页更新时自动更新缓存?

4

2 回答 2

0

是的,有一种方法可以自动更新您的缓存。事实上,这是一个非常常见的问题,称为缓存失效

您可以通过Memcached::delete()方法使用基本缓存失效。这将导致下次有人访问(未)缓存的页面时重新生成缓存。

或者您可以使用直写缓存,这意味着您执行更新页面所需的操作,然后将新结果保存到旧缓存键中,就像您访问页面时保存它的方式一样。这种解决方案往往要复杂得多。

另外,请参阅kander的出色评论。

于 2013-08-02T14:01:49.680 回答
0

Tricky situation you have here, and I can see two ways of going about it.

1) You hold a cache entry for each individual user with their friends posts on (e.g. Memcache entry under the key "User1" which would contain an array of 10 items, "User2" => "post content", "User8" => "post content", etc) which would require you to update all of the user friends memcache entries when they make a new post.

2) You hold a cache entry for each individual user with their posts on (e.g. Memcache entry under the key "User 1" which would contain an array of the users most 10 recent posts, "postid" => "post content", "timestamp", "content" which would require you to update the users individual cache of their 10 most recent posts, but whenever one of the users friend loads the homepage, that users friend would have to query all of their friends caches and compare the timestamp on their recent posts to the current timestamp.

I would use solution 2.

于 2013-08-02T14:10:55.223 回答