# Fresh\_when/stale? usage

**URL:** <https://www.ruby-forum.com/t/fresh-when-stale-usage/195195>\
**Category:** Rails\
**Created:** [September 19, 2010, 2:48am UTC](https://www.ruby-forum.com/t/fresh-when-stale-usage/195195 "2010-09-19T02:48:55Z")\
**Posts on this page:** 3\
**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:** [September 19, 2010, 2:48am UTC](https://www.ruby-forum.com/t/fresh-when-stale-usage/195195/1 "2010-09-19T02:48:55Z")

</div>

In my post\_index action, I generate different kinds of “@posts” like…

def index  
case params[:listing\_type]  
when “all”  
@posts = get\_all\_post\_from\_memcached  
when “most\_popular”  
@posts = get\_all\_most\_popular\_from\_memcached

```
      respond_to do |format|
           format.html
           format.js #for ajax reqeusts
           format.xml #for rss etc

```

end

From what I understand fresh\_when takes a etag, and it is to be used  
if there is no difference in different kinds of rendered (in my case  
the rendering is different based on html or ajax)

and

stale? also takes an etag and surrounds the respond\_to block.

In this case the etag will be different based on the different listing  
types. But it seems there isn’t much flexibility in the way fresh\_when  
or stale? can be used here?

Thanks

---

<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:** [September 19, 2010, 4:19am UTC](https://www.ruby-forum.com/t/fresh-when-stale-usage/195195/2 "2010-09-19T04:19:02Z")

</div>

##updated

```
    def index
         case params[:listing_type]
              when "all"
                    #the key here is teh same key I used for

```

memcached  
if stale?(:etag =\> ‘all\_posts\_key’)  
@posts = get\_all\_post\_from\_memcached  
else  
head :not\_modified and return  
end  
when “most\_popular”  
if stale?(:etag =\> ‘most\_popular\_key’)  
@posts = get\_all\_most\_popular\_from\_memcached  
else  
head :notified and return  
end  
respond\_to do |format|  
format.html  
format.js #for ajax reqeusts  
format.xml #for rss etc  
end

update. I changed the original block a little and now get a double  
render error what am I doing wrong, should “head :notified and return”  
just return the header and not touch the respond\_to block?

---

<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:** [September 30, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/fresh-when-stale-usage/195195/3 "2010-09-30T14:24:58Z")

</div>

The stale & fresh blocks already will send not modified back to the  
client. Since you are also sending it, you get a double render.
