Skip to content

Instantly share code, notes, and snippets.

@leshill
Created November 7, 2008 18:23
Show Gist options
  • Save leshill/22931 to your computer and use it in GitHub Desktop.
Save leshill/22931 to your computer and use it in GitHub Desktop.
module AVCacheTTL
def self.included(base)
base.alias_method_chain :cache, :ttl
end
def cache_with_ttl(name = {}, options = nil, &block)
options = (options || {}).dup
return block.call if options.delete(:ignore_cache)
time = options.delete(:ttl)
time = time.from_now if time.respond_to?(:from_now)
cache_store = controller.cache_store
key = controller.fragment_cache_key(name)
expiry_key = "expiry/#{key}"
cached_time = cache_store.read(expiry_key, options)
if !cached_time || Time.at(cached_time.to_i).utc <= Time.now.utc
cache_store.delete(key, options)
cache_store.write(expiry_key, time.to_i.to_s, options)
end
cache_without_ttl(name, options, &block)
end
end
ActionView::Helpers::CacheHelper.send(:include, AVCacheTTL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment