I have an application that is setup to cache a json call. I’ve tried various code syntaxes and it seems to only cache correctly when I have the following:
caches_page :index, :gzip => true
cache_sweeper :institutes_sweeper
respond_to :json, :js
def index
@institutes = Institute.select([:id, :name]).all
render :json => { :institutes => @institutes }
end
Now when this gets hit, it renders an html file and the caching works as expected. The Rails stack doesn’t get hit.
If I would throw a format block and have the render respond to json, the caching will create the files; however, it will create another file the next time around and ignore the file already there.
Does apache have to be configured differently to work as expected?
Also, I would like to use public/cache/ as the directory to store the cache. I have rails configured correctly; however, I was wondering how to configure apache to serve up those cached files (the same problem above happens where the files get created, but not served).
Thanks for all your help!
Just wanted to leave the solution to my problem here.
Apache needs to be told where the cached files reside. The reason it works when just stored in the public area is because that is how apache acts by default. Add the following config to whereever you define your virtualhost
Now, tackling the problem of the json rendering html, I figured out that it was because of my calls in the application controller which execute before the actual function executes. If I use a skip_before_filter on those functions, I get one step closer.
I also figured out that it would be better to wrap the response in a format block.
This will result in the json being cache and the server config will result in it being served correctly.
I believe for those using Nginx, the following config should be used (not 100% sure though)
If anyone has any feedback on the code above, I’d love to hear it. Thanks!