Class: HasCache::Cache

Inherits:
Object
  • Object
show all
Extended by:
Utilities
Includes:
Utilities
Defined in:
lib/has_cache/cache.rb

Overview

The cache class proxies calls to the original cache_target, caching the results, and returns the cached results if passed the same cache_target and arguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

extract_result, merged_options

Constructor Details

#initialize(cache_target, options = {}) ⇒ Cache

Returns a new instance of Cache.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/has_cache/cache.rb', line 44

def initialize(cache_target, options = {})
  @cache_target = cache_target
  @cache_root = []
  @cache_options = merged_options(cache_target, options)
  if cache_target.is_a?(Class)
    @cache_root = [cache_target.name, :class]
  else
    @cache_root = [cache_target.class.name, :instance]
    unless cache_target.respond_to?(:has_cache_key)
      if cache_target.class.respond_to?(:primary_key)
        @cache_root << cache_target.send(cache_target.class.send(:primary_key).to_sym)
      elsif cache_target.respond_to?(:id)
        @cache_root << cache_target.send(:id)
      elsif cache_target.respond_to?(:name)
        @cache_root << cache_target.send(:name)
      else
        # rubocop:disable LineLength, StringLiterals
        fail ArgumentError, "Could not find key for instance of `#{cache_target.class.name}`, must call with `instance.cached(key: some_unique_key).method`"
        # rubocop:enable LineLength, StringLiterals
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



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

def method_missing(method, *args)
  key = generate_cache_key
  unless cache_options.delete(:canonical_key)
    key += [method]
    key += args unless args.empty?
  end
  if cache_options.delete(:delete)
    Rails.cache.delete(key)
  else
    Rails.cache.fetch(key, cache_options) do
      extract_result(cache_target.send(method, *args))
    end
  end
end

Instance Attribute Details

#cache_optionsObject

Returns the value of attribute cache_options.



10
11
12
# File 'lib/has_cache/cache.rb', line 10

def cache_options
  @cache_options
end

#cache_rootObject

Returns the value of attribute cache_root.



10
11
12
# File 'lib/has_cache/cache.rb', line 10

def cache_root
  @cache_root
end

#cache_targetObject

Returns the value of attribute cache_target.



10
11
12
# File 'lib/has_cache/cache.rb', line 10

def cache_target
  @cache_target
end

Class Method Details

.new(*args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/has_cache/cache.rb', line 12

def self.new(*args, &block)
  o = allocate
  o.__send__(:initialize, *args, &block)
  return o unless block_given?

  key = o.generate_cache_key
  begin
    block_source = block.to_source(ignore_nested: true)
  rescue => e
    if key == o.cache_root
      # rubocop:disable LineLength, StringLiterals
      raise ArgumentError, "Could not generate key from block, must call with `.cached(key: some_unique_key) { block }` (#{e})"
      # rubocop:enable LineLength, StringLiterals
    end
  end
  unless o.cache_options.delete(:canonical_key) || block_source.nil?
    key += [block_source]
  end
  if o.cache_options.delete(:delete)
    Rails.cache.delete(key)
  else
    Rails.cache.fetch(key, o.cache_options) do
      if o.cache_target.is_a?(Class)
        result = o.cache_target.class_eval(&block)
      else
        result = o.cache_target.instance_eval(&block)
      end
      extract_result(result)
    end
  end
end

Instance Method Details

#generate_cache_keyObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/has_cache/cache.rb', line 68

def generate_cache_key
  key = cache_root
  if cache_options.key?(:key)
    options_key = cache_options.delete(:key)
    if options_key.is_a?(Proc)
      if cache_target.is_a?(Class)
        key += Array.wrap(cache_target.class_eval(&options_key))
      else
        key += Array.wrap(cache_target.instance_eval(&options_key))
      end
    else
      key += Array.wrap(options_key)
    end
  elsif cache_target.respond_to?(:has_cache_key)
    key += cache_target.send(:has_cache_key)
  end
  key
end