Class: Eigenjoy::MethodCache::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/method_cache/proxy.rb

Constant Summary collapse

NULL =
'NULL'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, opts) ⇒ Proxy

Returns a new instance of Proxy.



13
14
15
16
17
# File 'lib/method_cache/proxy.rb', line 13

def initialize(method_name, opts)
  opts[:cache] ||= :counters if opts[:counter]
  @method_name = method_name
  @opts        = opts      
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/method_cache/proxy.rb', line 10

def args
  @args
end

#method_nameObject (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

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/method_cache/proxy.rb', line 10

def opts
  @opts
end

#targetObject (readonly)

Returns the value of attribute target.



10
11
12
# File 'lib/method_cache/proxy.rb', line 10

def target
  @target
end

Instance Method Details

#bind(target, args) ⇒ Object



19
20
21
# File 'lib/method_cache/proxy.rb', line 19

def bind(target, args)
  self.clone.bind!(target, args)
end

#bind!(target, args) ⇒ Object



23
24
25
26
27
28
# File 'lib/method_cache/proxy.rb', line 23

def bind!(target, args)
  @target = target
  @args   = args
  @key    = nil
  self
end

#cacheObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/method_cache/proxy.rb', line 102

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.metaclass.module_eval do
        define_method :[] do |key|
          get(key)
        end
      end
    end
  end
  @cache
end

#cached?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/method_cache/proxy.rb', line 47

def cached?
  not cache[key].nil?
end

#clone?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/method_cache/proxy.rb', line 121

def clone?
  !!opts[:clone]
end

#contextObject



39
40
41
# File 'lib/method_cache/proxy.rb', line 39

def context
  opts[:context]
end

#counter_method(method_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/method_cache/proxy.rb', line 83

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

#invalidateObject



30
31
32
33
34
35
36
37
# File 'lib/method_cache/proxy.rb', line 30

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

#keyObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/method_cache/proxy.rb', line 125

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
  puts "cache key: #{@key}" if Eigenjoy::MethodCache.verbose?
  @key
end

#local?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/method_cache/proxy.rb', line 117

def local?
  cache.kind_of?(Hash)
end

#method_name_without_cachingObject



95
96
97
98
99
100
# File 'lib/method_cache/proxy.rb', line 95

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_cachingObject



75
76
77
78
79
80
81
# File 'lib/method_cache/proxy.rb', line 75

def method_with_caching
  proxy = self # Need access to the proxy in the closure.

  lambda do |*args|
    proxy.bind(self, args).value
  end
end

#updateObject



51
52
53
54
55
# File 'lib/method_cache/proxy.rb', line 51

def update
  value = block_given? ? yield(cache[key]) : target.send(method_name_without_caching, *args)
  write_to_cache(key, value)
  value
end

#valueObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/method_cache/proxy.rb', line 57

def value
  value = opts[:counter] ? cache.count(key) : cache[key] unless Eigenjoy::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

#versionObject



43
44
45
# File 'lib/method_cache/proxy.rb', line 43

def version
  dynamic_opt(:version)
end