Skip to content

Instantly share code, notes, and snippets.

@debajit
Created July 25, 2015 00:45
Show Gist options
  • Save debajit/40f51f15d82e1b23681e to your computer and use it in GitHub Desktop.
Save debajit/40f51f15d82e1b23681e to your computer and use it in GitHub Desktop.
Cache render JSON in a Rails controller
def show
if stale?(@model, public: true)
respond_to do |format|
format.json do
full_structure_json = Rails.cache.fetch("#{@model.cache_key}") do
MyCustomSerializer.new(@model).to_json
end
render json: full_structure_json
end
end
end
end
@debajit
Copy link
Author

debajit commented Jul 25, 2015

This solution works. Is there a better way of doing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment