# Http caching a dynamic page

**URL:** <https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996>\
**Category:** Rails\
**Created:** [October 5, 2010, 1:53am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996 "2010-10-05T01:53:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [October 5, 2010, 1:53am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996/1 "2010-10-05T01:53:18Z")

</div>

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

\<%= post.body%\>

\<%= post.created\_at%\>

\<%= post.category%\>

\<%= Post.favorites\_count%\>

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?

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [October 5, 2010, 2:06am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996/2 "2010-10-05T02:06:45Z")

</div>

Quoting badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 

[snip]

If favorite\_count is rendered at one end or the other, you can use  
fragment  
caching and memcache for the unchanging part. Or two fragments if  
favorite\_count is in the middle.

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [October 5, 2010, 2:28am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996/3 "2010-10-05T02:28:24Z")

</div>

Yes, it seems the whole notion of etags, last\_modified is pretty  
useless for even remotely dynamic pages. For example, let’s say If am  
shows a list of posts on a Post Index action. I can may be generate  
etags based on the Post count and send a 304 back, but if that page  
has a navbar that shows the current logged in users, even that  
information is not updated, it essentially it seems is telling the  
browser load the entire page for that action from cache.

Don’t most apps similar structure (a username at the top, may be a  
badge or something that is specific to a logged in user), how would  
you etag these kind of pages?

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [October 5, 2010, 3:11am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996/4 "2010-10-05T03:11:42Z")

</div>

As you noted, etags et al doesn’t work for dynamic pages. You can use  
memcache for rendered fragments of a page as well as objects.

Jeffrey

Quoting badnaam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [October 5, 2010, 3:46am UTC](https://www.ruby-forum.com/t/http-caching-a-dynamic-page/195996/5 "2010-10-05T03:46:48Z")

</div>

Yes I guess that might be the case. the scaling rails screencasts  
about advanced http caching led me to think that most high traffic web  
sites use etag extensively for reverse proxy caching.
