How to cache JSON in ruby on rails?

I am confused about how to use json in my rails (2.3.5) app. These are the elements that I need:

  1. html template
  2. json data
  3. javascript to render data in the template (I use PURE)

json data, calculated using helper methods (rails and my own ones), is quite big and it doesn't change often (in general it's footer, header and some other parts of the page), so it should be cached. What is the right way to do it?

I tried 3 approaches.

A.

 - html template and javascript to render data placed in the view html.erb
 - method to get json data placed in the helper
    

Problem: how to cache json data?

B.

 - html template and javascript to render data placed in the view html.erb
 - method to get json data is a controller action
     def footer
        expires_in(4.hours)
        render :json => { calculated_data }
     end
 - ajax request in javascript
      $.ajax({
        type: "GET",
        url: "<%=footer_path %>",
        success: function(data){
          render json data in html template
        }
      });

Problem: second request send to the server...

C.

 - html template and javascript to render data placed in the view html.erb
 - method to get json data is a controller action
     def footer
        expires_in(4.hours)
        render :json => { calculated_data }
     end
 - data in javascript
    

Problems: how can I pass result of that script to a variable which I can use in the javascript that renders data in the template?

In case of B and C there is a need to include helpers in the controller (methods for calculating json data), which is quite ugly solution.

What should be the correct solution for this? Which approach is appropiate for this needs?

Thanks for all suggestions.

6
задан santuxus 19 May 2011 в 12:42
поделиться