Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache render :json

I have a controller index action which returns json output.

render :json => my_array.to_json

What type of caching do I have to use here. Does 'page caching' make sense for this.

Or do I have to do action caching like below

caches_action :index
like image 465
user290870 Avatar asked May 12 '10 06:05

user290870


1 Answers

Either action caching or page caching would work fine; page caching would have the benefit of never calling the Rails stack, but it depends on whether you need to control who accesses that Json feed.

I'm a big fan of using page caching if you can get away with it - there are big savings on system resources to be had. :)


EDIT: Page caching example, in case there was any confusion:

class SomeController < ApplicationController
  caches_page :index
  def index
    render :json => my_array.to_json
  end
end

Unless I've misunderstood something, that should be all you need to do.

like image 179
robotmay Avatar answered Oct 12 '22 01:10

robotmay