Created
July 25, 2015 00:45
-
-
Save debajit/40f51f15d82e1b23681e to your computer and use it in GitHub Desktop.
Cache render JSON in a Rails controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution works. Is there a better way of doing this?