Created
January 12, 2015 16:24
-
-
Save jeffdeville/5666e1b391b30de13c4e to your computer and use it in GitHub Desktop.
caching method calls
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
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