开发者

Proper Caching Strategy for a Dynamic Action

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 o开发者_运维知识库ut the most optimized way to do the following:

  1. Query the database to find a post and see if it has been 'flagged'
  2. Query a Whitelist to see if a different part of the post has been 'flagged'
  3. Query an API to see if they find this user in their system
  4. 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


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:

<% cache @post.cache_key do %>
  <%= @post.formatted_content %>
  ...
<% end %>

Other things to consider

  • Setup server to send cache headers with images, JS & CSS
  • Check out barista to reduce requests & bundle assets
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜