Class: DefCache::CacheHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/def_cache/cache_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, instance, options = {}) ⇒ CacheHandler

Returns a new instance of CacheHandler.



6
7
8
9
10
11
12
13
14
15
# File 'lib/def_cache/cache_handler.rb', line 6

def initialize(method, instance, options={})
  cached_target, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
  @cache_options             = options.except(:with, :keys)
  @logger                    = options[:logger]
  @cache_store               = fetch_store options[:with]
  @miss_method               = "#{cached_target}_without_cache#{punctuation}"
  @method_name               = method
  @keys                      = Array.wrap(options[:keys]) || []
  @instance                  = instance
end

Instance Attribute Details

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def cache_options
  @cache_options
end

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def cache_store
  @cache_store
end

#instanceObject (readonly)

Returns the value of attribute instance.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def instance
  @instance
end

#keysObject (readonly)

Returns the value of attribute keys.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def keys
  @keys
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def method_name
  @method_name
end

#miss_methodObject (readonly)

Returns the value of attribute miss_method.



4
5
6
# File 'lib/def_cache/cache_handler.rb', line 4

def miss_method
  @miss_method
end

Instance Method Details

#add_reference(cache_key) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/def_cache/cache_handler.rb', line 57

def add_reference(cache_key)
  unless index.include? cache_key
    logger.info "adding cache ref: #{cache_key}"
    new_index = index << cache_key
    cache_store.write index_cache_key, new_index
  end
end

#block_parameter_key(param, value, &block) ⇒ Object



95
96
97
# File 'lib/def_cache/cache_handler.rb', line 95

def block_parameter_key(param, value, &block)
  "&#{param}(#{Digest::MD5.hexdigest(block.source)})" if block_given?
end

#cache_key(*values) ⇒ Object



17
18
19
20
# File 'lib/def_cache/cache_handler.rb', line 17

def cache_key(*values)
  key_values = keys.map { |key| instance.send(key) || '*' }
  [instance_cache_key, *key_values, method_name, *values].select(&:present?).join('/')
end

#default_storeObject



79
80
81
# File 'lib/def_cache/cache_handler.rb', line 79

def default_store
  defined?(Rails) ? Rails.cache : ActiveSupport::Cache.lookup_store(:memory_store)
end

#fetch_store(store, options = {}) ⇒ Object



75
76
77
# File 'lib/def_cache/cache_handler.rb', line 75

def fetch_store(store, options = {})
  (store.is_a?(Symbol) ? lookup_store(store, options) : store) || default_store
end

#flush!Object



65
66
67
68
69
70
71
# File 'lib/def_cache/cache_handler.rb', line 65

def flush!
  index.each do |subkey|
    logger.info "removing cache ref: #{subkey}"
    cache_store.delete subkey
    remove_reference subkey
  end
end

#indexObject



48
49
50
# File 'lib/def_cache/cache_handler.rb', line 48

def index
  cache_store.fetch(index_cache_key){[]}
end

#index_cache_keyObject



44
45
46
# File 'lib/def_cache/cache_handler.rb', line 44

def index_cache_key
  cache_key(:__index__)
end

#instance_cache_keyObject



26
27
28
29
30
31
# File 'lib/def_cache/cache_handler.rb', line 26

def instance_cache_key
  instance.cache_key
rescue NoMethodError
  klass = (instance.is_a?(Module) ? self : instance.class)
  klass.name || klass.object_id
end

#instance_methodObject



22
23
24
# File 'lib/def_cache/cache_handler.rb', line 22

def instance_method
  instance.method(miss_method)
end

#loggerObject



33
34
35
# File 'lib/def_cache/cache_handler.rb', line 33

def logger
  @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

#method_cache_key(*args, &block) ⇒ Object



37
38
39
40
41
42
# File 'lib/def_cache/cache_handler.rb', line 37

def method_cache_key(*args, &block)
  params = instance_method.parameters.each_with_index do |(req, param), n|
    send "#{req}_parameter_key", param, args[n], &block
  end.compact.join(',')
  cache_key *params
end

#opt_parameter_key(param, value) ⇒ Object



87
88
89
# File 'lib/def_cache/cache_handler.rb', line 87

def opt_parameter_key(param, value)
  "#{param}=#{value.inspect}" if value.present?
end

#remove_reference(cache_key) ⇒ Object



52
53
54
55
# File 'lib/def_cache/cache_handler.rb', line 52

def remove_reference(cache_key)
  new_index = index.delete(cache_key)
  cache_store.write index_cache_key, new_index
end

#req_parameter_key(param, value) ⇒ Object



83
84
85
# File 'lib/def_cache/cache_handler.rb', line 83

def req_parameter_key(param, value)
  "#{param}=#{value.inspect}"
end

#rest_parameter_key(param, values) ⇒ Object



91
92
93
# File 'lib/def_cache/cache_handler.rb', line 91

def rest_parameter_key(param, values)
  "*#{param}=#{values.map(&:inspect)}" if values.present?
end