Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created January 12, 2015 16:24
Show Gist options
  • Save jeffdeville/5666e1b391b30de13c4e to your computer and use it in GitHub Desktop.
Save jeffdeville/5666e1b391b30de13c4e to your computer and use it in GitHub Desktop.
caching method calls
cache :read,
invalidate_on: [:update, :update_cloud_director_contact, :update_cloud_service, :update_cc_payment_info],
key: :account_id
# Module overrides read to look like this:
module Cacheable
def cache(methods_to_cache, methods_to_invalidate, key)
methods_to_cache.each do |method|
define_method method do |*args|
cache_the_shit(*args)
end
end
end
end
def read(*params)
cached_id_value = params[cache_id]
cache_result = cache.fetch(cached_id_value)
return cache_result if cache_result
cache[cache_id] = super
end
def update(*params)
cached_id_value = params[cache_id]
cache.clear(cached_id_value)
super
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment