Include Root Node for as_api_response #51

Closed
smartcode opened this Issue Dec 11, 2011 · 4 comments

3 participants

@smartcode

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
       yield
    end
end

The render statement in the Controller is similar to this:

    render_for_api :template, :json => Item.for_user(@user.id)

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_#{ @user.id }") do
      #Item.for_user(@user.id).as_api_response(:template)  --> load time about 300ms
       Item.for_user(@user.id).as_api_response(:template).to_json --> json in cache - load time 3ms! :) 
 end

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?

Thanks!

@masterkain

Why can't you use caches_actionat controller level?

@smartcode

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

@smartcode

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.

@fabrik42
Owner

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