Class: MethodCache::Proxy
- Inherits:
-
Object
- Object
- MethodCache::Proxy
- Defined in:
- lib/method_cache/proxy.rb
Constant Summary collapse
- NULL =
'NULL'
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #bind(target, args) ⇒ Object
- #bind!(target, args) ⇒ Object
- #cache ⇒ Object
- #cached? ⇒ Boolean
- #clone? ⇒ Boolean
- #context ⇒ Object
- #counter_method(method_name) ⇒ Object
-
#initialize(method_name, opts) ⇒ Proxy
constructor
A new instance of Proxy.
- #invalidate ⇒ Object
- #key ⇒ Object
- #local? ⇒ Boolean
- #method_name_without_caching ⇒ Object
- #method_with_caching ⇒ Object
- #update ⇒ Object
- #value ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(method_name, opts) ⇒ Proxy
Returns a new instance of Proxy.
8 9 10 11 12 |
# File 'lib/method_cache/proxy.rb', line 8 def initialize(method_name, opts) opts[:cache] ||= :counters if opts[:counter] @method_name = method_name @opts = opts end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
5 6 7 |
# File 'lib/method_cache/proxy.rb', line 5 def args @args end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
5 6 7 |
# File 'lib/method_cache/proxy.rb', line 5 def method_name @method_name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
5 6 7 |
# File 'lib/method_cache/proxy.rb', line 5 def opts @opts end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
5 6 7 |
# File 'lib/method_cache/proxy.rb', line 5 def target @target end |
Instance Method Details
#bind(target, args) ⇒ Object
14 15 16 |
# File 'lib/method_cache/proxy.rb', line 14 def bind(target, args) self.clone.bind!(target, args) end |
#bind!(target, args) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/method_cache/proxy.rb', line 18 def bind!(target, args) @target = target @args = args @key = nil self end |
#cache ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/method_cache/proxy.rb', line 97 def cache if @cache.nil? @cache = opts[:cache] || MethodCache.default_cache @cache = Memcache.pool[@cache] if @cache.kind_of?(Symbol) end @cache end |
#cached? ⇒ Boolean
42 43 44 |
# File 'lib/method_cache/proxy.rb', line 42 def cached? not cache[key].nil? end |
#clone? ⇒ Boolean
109 110 111 |
# File 'lib/method_cache/proxy.rb', line 109 def clone? !!opts[:clone] end |
#context ⇒ Object
34 35 36 |
# File 'lib/method_cache/proxy.rb', line 34 def context opts[:context] end |
#counter_method(method_name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/method_cache/proxy.rb', line 78 def counter_method(method_name) proxy = self # Need access to the proxy in the closure. lambda do |*args| if args.last.kind_of?(Hash) and args.last.keys == [:by] amount = args.last[:by] args.pop end proxy.bind(self, args).send(method_name, amount || 1) end end |
#invalidate ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/method_cache/proxy.rb', line 25 def invalidate if block_given? # Only invalidate if the block returns true. value = cache[key] return if value and not yield(value) end cache.delete(key) end |
#key ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/method_cache/proxy.rb', line 113 def key if @key.nil? arg_string = ([method_name, target] + args).collect do |arg| object_key(arg) end.join('|') @key = ['m', version, arg_string].compact.join('|') @key = "m|#{Digest::SHA1.hexdigest(@key)}" if @key.length > 250 end @key end |
#local? ⇒ Boolean
105 106 107 |
# File 'lib/method_cache/proxy.rb', line 105 def local? cache.kind_of?(Hash) end |
#method_name_without_caching ⇒ Object
90 91 92 93 94 95 |
# File 'lib/method_cache/proxy.rb', line 90 def method_name_without_caching @method_name_without_caching ||= begin base_name, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1 "#{base_name}_without_caching#{punctuation}" end end |
#method_with_caching ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/method_cache/proxy.rb', line 70 def method_with_caching proxy = self # Need access to the proxy in the closure. lambda do |*args| proxy.bind(self, args).value end end |
#update ⇒ Object
46 47 48 49 50 |
# File 'lib/method_cache/proxy.rb', line 46 def update value = block_given? ? yield(cache[key]) : target.send(method_name_without_caching, *args) write_to_cache(key, value) value end |
#value ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/method_cache/proxy.rb', line 52 def value value = opts[:counter] ? cache.count(key) : cache[key] unless MethodCache.disabled? value = nil unless valid?(:load, value) if value.nil? value = target.send(method_name_without_caching, *args) raise "non-integer value returned by counter method" if opts[:counter] and not value.kind_of?(Fixnum) write_to_cache(key, value) if valid?(:save, value) end value = nil if value == NULL if clone? and value value.clone else value end end |
#version ⇒ Object
38 39 40 |
# File 'lib/method_cache/proxy.rb', line 38 def version dynamic_opt(:version) end |