Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created October 12, 2010 09:51
Show Gist options
  • Save fujimura/621946 to your computer and use it in GitHub Desktop.
Save fujimura/621946 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment