动态操作的正确缓存策略
对于那些曾经缩放/缓存过任何东西的人来说,这可能是一个明显的问题。我没有,而且我迷失在互联网上的教程和代码片段中(http:// /guides.rubyonrails.org/caching_with_rails.html)。
我正在部署到安装了 Memcached 的 Heroku,并正在找出执行以下操作的最优化方法:
- 查询数据库以查找帖子并查看它是否已被“标记”
- 查询白名单以查看帖子的不同部分已被“标记”
- 查询 API 以查看他们是否在系统中找到此用户
- 渲染页面,其中对 CSS/JS/等远程系统进行大量重复调用。
我认为#1 经常发生并且经常变化。 #2 不太如此。 #3 很少更改(几个月),而 #4 仅在 #3 更改时才更改。
我希望能够定期增加 flag_count 和 view_count 而无需命中缓存版本。我应该如何组合页面、操作和片段缓存?现在,我根本不缓存此操作...
我的[简化]控制器代码:
def show
expires_in 12.hours, :public => true
@post = Post.find(params[:id])
#CHECK FLAG STATUS
redirect_to root_path and return if @post.flag?
#CHECK WHITELIST STATUS
redirect_to root_path and return if Whitelist.includes?(@post.screen_name)
#Ping API again on the off chance user deleted/changed account
if @post && @post.user = get_user_from_api( @post.screen_name )
@post.increment_views!
render :layout => false
else
redirect_to root_path
end
end
Probably an obvious question for those of you who have scaled/cached anything before. I haven't, and I'm getting lost in the tutorials and code snippets all over the internet (http://guides.rubyonrails.org/caching_with_rails.html).
I'm deploying to Heroku with Memcached installed and am figuring out the most optimized way to do the following:
- Query the database to find a post and see if it has been 'flagged'
- Query a Whitelist to see if a different part of the post has been 'flagged'
- Query an API to see if they find this user in their system
- Render a page with a lot of repetitive calls to remote systems for CSS/JS/etc.
I assume #1 happens frequently and changes often. #2 less so. #3 changes infrequently (months), and #4 should only change if #3 changes.
I want to be able to increment flag_count and view_count regularly without hitting a cached version. What mix of page, action and fragment caching should I be doing? Right now, I'm not caching this action at all...
My [simplified] controller code:
def show
expires_in 12.hours, :public => true
@post = Post.find(params[:id])
#CHECK FLAG STATUS
redirect_to root_path and return if @post.flag?
#CHECK WHITELIST STATUS
redirect_to root_path and return if Whitelist.includes?(@post.screen_name)
#Ping API again on the off chance user deleted/changed account
if @post && @post.user = get_user_from_api( @post.screen_name )
@post.increment_views!
render :layout => false
else
redirect_to root_path
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些小事情可能会与此相关。鉴于无法避免每个请求都触及应用程序堆栈,因此很棘手。
片段缓存
将片段缓存与memcache结合使用,以避免重新生成帖子/评论内容。如果你的观点很强烈,这里可能会有一些收获。 Memcache 对象会自动过期,并且可以通过以下方式将其键入到帖子的最新版本:
其他需要考虑的事项
There's a few small things that might work with this. Tricky given there's no way to avoid hitting the app stack each request.
Fragment Caching
Use fragment caching with memcache to avoid regenerating post/comment content. There might be some gains here if your views are heavy. Memcache objects self-expire and can be keyed to the most recent version of a post with something like:
Other things to consider