Include Root Node for as_api_response #51

smartcode opened this Issue Dec 11, 2011 · 4 comments

3 participants


Hello i currently working on the performance of my Rails 3.1 app and I added a lot of caching for some of the JSON i am rendering using acts_as_api

What works for me is this:

def around_api_response(api_template)
    Rails.cache.fetch("api_response_#{self.class.to_s}_#{id}_#{api_template.to_s}", :expires_in => 1.hour) do

The render statement in the Controller is similar to this:

    render_for_api :template, :json => Item.for_user(

I found this code in one of the other issues and it reduces the time for computing the response to a quater of the time without caching. Which was about 100-200ms in my case (about 98% of this time is used for view rendering).

So i tried to do some more optimization and I did something like this

@cache = Rails.cache.fetch("cache_data_for_user_#{ }") do
      #Item.for_user(  --> load time about 300ms
       Item.for_user( --> json in cache - load time 3ms! :) 

My problem is that if i don't use the the render_for_api method the root element "Items" is not included. My object mapper on the client depends on this root element.

I tried

ActiveRecord::Base.include_root_in_json = true   # i also think it should be true by default

but it didn't help
Can i force the inclusion of the root element in some way?



Why can't you use caches_actionat controller level?


caches_action performs not that good and causes bad performance with different action parameters


Does somebody have an idea how to solve this? it would allow me to decrease the rendering time of a api request by the factor 10.


Closing this for now. Please re-open if issue is still present.

@fabrik42 fabrik42 closed this Feb 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment