Class: CacheValue::CacheMachine

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/cache_value/cache_machine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#caching_method_names, #hex_digest, #logger

Constructor Details

#initialize(object, method, options, arguments) ⇒ CacheMachine

Returns a new instance of CacheMachine.



30
31
32
33
34
35
# File 'lib/cache_value/cache_machine.rb', line 30

def initialize(object, method, options, arguments)
  self.object = object
  self.cached_method = method
  self.options = options
  self.arguments = arguments
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



28
29
30
# File 'lib/cache_value/cache_machine.rb', line 28

def arguments
  @arguments
end

#cached_methodObject

Returns the value of attribute cached_method.



28
29
30
# File 'lib/cache_value/cache_machine.rb', line 28

def cached_method
  @cached_method
end

#objectObject

Returns the value of attribute object.



28
29
30
# File 'lib/cache_value/cache_machine.rb', line 28

def object
  @object
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/cache_value/cache_machine.rb', line 28

def options
  @options
end

Class Method Details

.cache_storeObject



19
20
21
# File 'lib/cache_value/cache_machine.rb', line 19

def cache_store
  @cache_store ||= ActiveSupport::Cache.lookup_store(:mem_cache_store)
end

.cache_store=(*store_option) ⇒ Object



15
16
17
# File 'lib/cache_value/cache_machine.rb', line 15

def cache_store=(*store_option)
  @cache_store = store_option ? ActiveSupport::Cache.lookup_store(*store_option) : nil
end

.lookup(object, method, options, arguments = nil) ⇒ Object



23
24
25
# File 'lib/cache_value/cache_machine.rb', line 23

def lookup(object, method, options, arguments = nil)
  new(object, method, options, arguments).lookup
end

Instance Method Details

#cache_keyObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cache_value/cache_machine.rb', line 43

def cache_key
  if !@cache_key
    options = process_options
    if !options[:cache_key] and !object.respond_to?(:cache_key)
      raise ConfigurationException.new("object of class #{object.class.name} does not respond to :cache_key")
    end

    cache_key = options[:cache_key] || object.cache_key
    key = cache_key.gsub('/', '_') + "_#{cached_method}"
    key << '_' + hex_digest(arguments) if arguments
    #logger.debug "cache_value: cache_key = #{key}"
    @cache_key = key
  end
  @cache_key
end

#call_and_store_valueObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cache_value/cache_machine.rb', line 75

def call_and_store_value
  without_method = caching_method_names(cached_method).first
  value = nil
  time = Benchmark.realtime do
    value = arguments ? object.send(without_method, *arguments) : object.send(without_method)
  end

  ttl = process_options[:ttl]
  self.class.cache_store.write(cache_key, value.to_yaml, :expires_in => ttl)
  logger.info "cache_value: cached #{object.class.name}##{cached_method} for #{ttl}s (will save #{(time*1000).round(1)}ms)"

  value
end

#fetch_and_parseObject



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

def fetch_and_parse
  key = cache_key
  data = nil
  time = Benchmark.realtime do
    data = self.class.cache_store.fetch(key)
  end
  if data
    @fetched = true
    logger.info "cache_value: retrieved #{key} (#{(time*1000).round(1)}ms)"
    YAML::load(data)
  else
    @fetched = false
    nil
  end
end

#lookupObject



37
38
39
40
41
# File 'lib/cache_value/cache_machine.rb', line 37

def lookup
  value = fetch_and_parse
  value = call_and_store_value unless @fetched
  value
end

#process_optionsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cache_value/cache_machine.rb', line 89

def process_options
  opts = options
  opts = object.send(:method, opts) if opts.is_a?(Symbol)
  opts = opts.call(*([object, cached_method, arguments][0,opts.arity])) if opts.respond_to?(:arity)

  if opts.respond_to?(:to_hash)
    opts = opts.to_hash
  else
    opts = { :ttl => opts, :cache_key => nil }
  end

  raise ConfigurationException.new('Options must resolve to a hash with a :ttl') unless opts[:ttl]

  opts
end