Class: Eigenjoy::MethodCache::Proxy
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.
Class Method Summary collapse
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.
19 20 21 22 23 |
# File 'lib/method_cache/proxy.rb', line 19 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.
10 11 12 |
# File 'lib/method_cache/proxy.rb', line 10 def args @args end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
10 11 12 |
# File 'lib/method_cache/proxy.rb', line 10 def method_name @method_name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
10 11 12 |
# File 'lib/method_cache/proxy.rb', line 10 def opts @opts end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
10 11 12 |
# File 'lib/method_cache/proxy.rb', line 10 def target @target end |
Class Method Details
.asciify(s) ⇒ Object
13 14 15 16 17 |
# File 'lib/method_cache/proxy.rb', line 13 def self.asciify(s) s.gsub(/([^\x20-\x7F]+)/) do '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase end.tr(' ', '_') end |
Instance Method Details
#bind(target, args) ⇒ Object
25 26 27 |
# File 'lib/method_cache/proxy.rb', line 25 def bind(target, args) self.clone.bind!(target, args) end |
#bind!(target, args) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/method_cache/proxy.rb', line 29 def bind!(target, args) @target = target @args = args @key = nil self end |
#cache ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/method_cache/proxy.rb', line 111 def cache if @cache.nil? @cache = opts[:cache] || Eigenjoy::MethodCache.default_cache @cache = Memcache.pool[@cache] if @cache.kind_of?(Symbol) if not @cache.respond_to?(:[]) and @cache.respond_to?(:get) @cache..module_eval do define_method :[] do |key| get(key) end end end end @cache end |
#cached? ⇒ Boolean
53 54 55 |
# File 'lib/method_cache/proxy.rb', line 53 def cached? not cache[key].nil? end |
#clone? ⇒ Boolean
130 131 132 |
# File 'lib/method_cache/proxy.rb', line 130 def clone? !!opts[:clone] end |
#context ⇒ Object
45 46 47 |
# File 'lib/method_cache/proxy.rb', line 45 def context opts[:context] end |
#counter_method(method_name) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/method_cache/proxy.rb', line 92 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
36 37 38 39 40 41 42 43 |
# File 'lib/method_cache/proxy.rb', line 36 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
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/method_cache/proxy.rb', line 134 def key if @key.nil? arg_string = ([method_name, target] + args).collect do |arg| self.class.asciify(object_key(arg)) end.join('|') @key = ['m', version, arg_string].compact.join('|') @key = "m|#{Digest::SHA1.hexdigest(@key)}" if @key.length > 250 puts "cache key: #{@key}" if Eigenjoy::MethodCache.verbose? end @key end |
#local? ⇒ Boolean
126 127 128 |
# File 'lib/method_cache/proxy.rb', line 126 def local? cache.kind_of?(Hash) end |
#method_name_without_caching ⇒ Object
104 105 106 107 108 109 |
# File 'lib/method_cache/proxy.rb', line 104 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
84 85 86 87 88 89 90 |
# File 'lib/method_cache/proxy.rb', line 84 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
57 58 59 60 61 |
# File 'lib/method_cache/proxy.rb', line 57 def update value = block_given? ? yield(cache[key]) : target.send(method_name_without_caching, *args) write_to_cache(key, value) value end |
#value ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/method_cache/proxy.rb', line 63 def value value = opts[:counter] ? cache.count(key) : cache[key] unless Eigenjoy::MethodCache.disabled? value = nil unless valid?(:load, value) if value.nil? puts "cache miss: #{key}" if Eigenjoy::MethodCache.verbose? 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) else puts "cache hit: #{key}" if Eigenjoy::MethodCache.verbose? end value = nil if value == NULL if clone? and value value.clone else value end end |
#version ⇒ Object
49 50 51 |
# File 'lib/method_cache/proxy.rb', line 49 def version dynamic_opt(:version) end |