开发者

How to cache render :json

开发者 https://www.devze.com 2022-12-30 04:06 出处:网络
I have a controller 开发者_如何学JAVAindex action which returns json output. render :json => my_array.to_json

I have a controller 开发者_如何学JAVAindex 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


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.


Same considerations should apply to JSON as any other output. If you need to validate access to the data for the user, then action caching is the way to go, otherwise page caching should be fine.

If the data changes due to logic in your app, then both forms of caching are problematic and you are better off using something else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号