Monday, October 4, 2010

[Rails] http caching a dynamic page

Is it possible to take advantage of http caching/proxy caching with
dynamic pages? i.e. pages with section/part that can change over time
but certain part of the page remain the same. I would rather not keep
re-rendering the static part but insure the dynamic part are rendered
with fresh data.

I am using memcached mostly as an object store that I can minimize db
hits but I still am rendering a bunch of views over and over again.

What's the general best practice to deal with something like this..

##Post controller

def show
@post = get_from_memcache
end

##show.html.erb
<div><%= post.body%></div>
<div><%= post.created_at%></div>
<div><%= post.category%></div>
<div><%= Post.favorites_count%></div>

The get_from_memcache return a @post object from a cache that
basically doesn't expire, because once a post is created, it's body,
category, created at etc remains the same.

However, in my view I do call another method favorites_count which
collects the posts's favorites count from memcache and this favorite
keeps changing.

This is a simplication of course, there are a few fields that do
change.

Now If I were to implement some sort of http caching then I would need
to do a fresh_when or stale? in my controller method (show), which
would essentially not render the views and hence the updated favorites
count, unless I use a etag that encompassed the favorite count and
other dynamic fields, in which case it kinda defeats the purpose
because those fields change regularly.

How do I manage this situation? So I can take advantage of a proxy but
keep certain dynamic fields in the page updated? One things I can
think of is ajax calls to other controller actions to update those
fields after page load, but that might ugly.

Any other recommendations?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate