So I save the time cached in the cache itself.
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
class ApplicationController < ActionController::Base | |
helper_method :count_something | |
def count_something | |
cache_of_today "count-of-something" do | |
Something.count | |
end | |
end | |
def cache_of_today(key, &block) | |
cached = Rails.cache.read key | |
if !cached || cached[:created_at] < 1.day.ago | |
Rails.cache.write key, :created_at => Time.now, :value => block.call | |
cached = Rails.cache.read key | |
end | |
cached[:value] | |
end | |
end |