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
67
68
69
70
71
72
73
74
75
76
77
78
# 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]

    return if cache_target.respond_to?(:has_cache_key)

    if cache_target.class.respond_to?(:primary_key)
      primary_key = cache_target.class.send(:primary_key)

      # Support composite primary keys
      # TODO: spec this
      case primary_key
      when String
        @cache_root << cache_target.send(primary_key.to_sym)
      when Array
        @cache_root << primary_key.map { |k| cache_target.send(k.to_sym) }
      else
        fail "Unknown primary key type: #{primary_key.class}"
      end
    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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/has_cache/cache.rb', line 101

def method_missing(method, *args)
  key = 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.



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

def cache_options
  @cache_options
end

#cache_rootObject

Returns the value of attribute cache_root.



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

def cache_root
  @cache_root
end

#cache_targetObject

Returns the value of attribute cache_target.



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

def cache_target
  @cache_target
end

Class Method Details

.new(*args, &block) ⇒ Object



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 13

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

  key = o.cache_key
  unless o.cache_options.delete(:canonical_key)
    begin
      block_source = block.to_source(ignore_nested: true)
    rescue Exception => 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
    key += [block_source] unless block_source.nil?
  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)
        extract_result(o.cache_target.class_eval(&block))
      else
        extract_result(o.cache_target.instance_eval(&block))
      end
    end
  end
end

Instance Method Details

#cache_keyObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/has_cache/cache.rb', line 80

def 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