Class: DefCache::CacheHandler
- Inherits:
-
Object
- Object
- DefCache::CacheHandler
- Defined in:
- lib/def_cache/cache_handler.rb
Instance Attribute Summary collapse
-
#cache_options ⇒ Object
readonly
Returns the value of attribute cache_options.
-
#cache_store ⇒ Object
readonly
Returns the value of attribute cache_store.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#miss_method ⇒ Object
readonly
Returns the value of attribute miss_method.
Instance Method Summary collapse
- #add_reference(cache_key) ⇒ Object
- #block_parameter_key(param, value, &block) ⇒ Object
- #cache_key(*values) ⇒ Object
- #default_store ⇒ Object
- #fetch_store(store, options = {}) ⇒ Object
- #flush! ⇒ Object
- #index ⇒ Object
- #index_cache_key ⇒ Object
-
#initialize(method, instance, options = {}) ⇒ CacheHandler
constructor
A new instance of CacheHandler.
- #instance_cache_key ⇒ Object
- #instance_method ⇒ Object
- #logger ⇒ Object
- #method_cache_key(*args, &block) ⇒ Object
- #opt_parameter_key(param, value) ⇒ Object
- #remove_reference(cache_key) ⇒ Object
- #req_parameter_key(param, value) ⇒ Object
- #rest_parameter_key(param, values) ⇒ Object
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, ={}) cached_target, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 @cache_options = .except(:with, :keys) @logger = [:logger] @cache_store = fetch_store [:with] @miss_method = "#{cached_target}_without_cache#{punctuation}" @method_name = method @keys = Array.wrap([:keys]) || [] @instance = instance end |
Instance Attribute Details
#cache_options ⇒ Object (readonly)
Returns the value of attribute cache_options.
4 5 6 |
# File 'lib/def_cache/cache_handler.rb', line 4 def @cache_options end |
#cache_store ⇒ Object (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 |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
4 5 6 |
# File 'lib/def_cache/cache_handler.rb', line 4 def instance @instance end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
4 5 6 |
# File 'lib/def_cache/cache_handler.rb', line 4 def keys @keys end |
#method_name ⇒ Object (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_method ⇒ Object (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_store ⇒ Object
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, = {}) (store.is_a?(Symbol) ? lookup_store(store, ) : 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 |
#index ⇒ Object
48 49 50 |
# File 'lib/def_cache/cache_handler.rb', line 48 def index cache_store.fetch(index_cache_key){[]} end |
#index_cache_key ⇒ Object
44 45 46 |
# File 'lib/def_cache/cache_handler.rb', line 44 def index_cache_key cache_key(:__index__) end |
#instance_cache_key ⇒ Object
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_method ⇒ Object
22 23 24 |
# File 'lib/def_cache/cache_handler.rb', line 22 def instance_method instance.method(miss_method) end |
#logger ⇒ Object
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 |