Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created November 10, 2015 23:33
Show Gist options
  • Select an option

  • Save ahoward/4ef83e8d574d2be6fb1c to your computer and use it in GitHub Desktop.

Select an option

Save ahoward/4ef83e8d574d2be6fb1c to your computer and use it in GitHub Desktop.
model =
SomeActiveRecordModel
conditions =
[
# ...
]
cache_key =
md5(conditions.to_json)
records_json =
Rails.cache.fetch(cache_key) do
records = query.where(conditions)
records.map{|record| record.attributes}.to_json
end
records_data =
JSON.parse(records_json)
records_array =
records_data.map{|attributes| model.instantiate(attributes)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment