Class: CacheValue::CacheMachine

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

Class Method Summary collapse

Methods included from Util

caching_method_names, hex_digest

Class Method Details

.cache_key(object, method, arguments = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/cache_value/cache_machine.rb', line 39

def cache_key(object, method, arguments = nil)
  if !object.respond_to?(:cache_key)
    raise ConfigurationException.new("object of class #{object.class.name} does not respond to :cache_key")
  end
  
  key = object.cache_key.gsub('/', '_') + "_#{method}"
  key << '_' + hex_digest(arguments) if arguments
  key
end

.cache_storeObject



17
18
19
# File 'lib/cache_value/cache_machine.rb', line 17

def cache_store
  @cache_store ||= ActiveSupport::Cache.lookup_store(:file_store, default_storage_dir)
end

.cache_store=(*store_option) ⇒ Object



13
14
15
# File 'lib/cache_value/cache_machine.rb', line 13

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

.cached_value_is_still_valid?(value, cached_age, object, method, options) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'lib/cache_value/cache_machine.rb', line 66

def cached_value_is_still_valid?(value, cached_age, object, method, options)
  options = object.send(:method, options) if options.is_a?(Symbol)
  if options.respond_to?(:arity)          
    options.call(*([cached_age, object, method][0,options.arity]))
  else
    cached_age > Time.now - options
  end
end

.call_and_store_value(object, method, arguments = nil) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/cache_value/cache_machine.rb', line 58

def call_and_store_value(object, method, arguments = nil)
  without_method = caching_method_names(method).first
  value = arguments ? object.send(without_method, *arguments) : object.send(without_method)
  cache_store.write(cache_key(object, method, arguments), [value, Time.now].to_yaml)
  
  value
end

.default_storage_dirObject



21
22
23
24
25
26
27
28
# File 'lib/cache_value/cache_machine.rb', line 21

def default_storage_dir
  if !defined?(RAILS_ROOT)
    raise ConfigurationException.new('Not running under rails. Set the cache_store type ' +
                                     'and location manually using CacheValue::CacheMachine.cache_store=')
  end
  
  File.join(RAILS_ROOT, 'tmp', 'cache_value_caches')
end

.fetch_and_parse(object, method, arguments = nil) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/cache_value/cache_machine.rb', line 49

def fetch_and_parse(object, method, arguments = nil)
  data = cache_store.fetch(cache_key(object, method, arguments))
  if data
    YAML::load(data)
  else
    [nil, nil]
  end
end

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



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

def lookup(object, method, options, arguments = nil)
  value, last_cached = fetch_and_parse(object, method, arguments)
  if !last_cached or !cached_value_is_still_valid?(value, last_cached, object, method, options)
    value = call_and_store_value(object, method, arguments)
  end
  
  value
end