-
-
Save kennyj/1955808 to your computer and use it in GitHub Desktop.
memoize with memcache store in Rails
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
# Usage: | |
# | |
# module ApplicationHelper | |
# extend MemcacheMemoize | |
# | |
# def bar(count = 10) | |
# "BAR" * count | |
# end | |
# memcache_memoize :bar, :expires_in => 10 | |
# end | |
# | |
module MemcacheMemoize | |
def memcache_memoize(method, options) | |
define_method(:"#{method}_with_memcache_memoize") do |*args| | |
key = "#{self.class.name}##{method}(#{args.map(&:to_s).join(',')})" | |
Rails.cache.fetch(key) { __send__(:"#{method}_without_memcache_memoize", *args) } | |
end | |
alias_method_chain method, :memcache_memoize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment