Class: Props::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/extensions/cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Cache

Returns a new instance of Cache.



7
8
9
10
# File 'lib/props_template/extensions/cache.rb', line 7

def initialize(context)
  @context = context
  @results = {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



23
24
25
# File 'lib/props_template/extensions/cache.rb', line 23

def context
  @context
end

#resultsObject (readonly)

Returns the value of attribute results.



5
6
7
# File 'lib/props_template/extensions/cache.rb', line 5

def results
  @results
end

Class Method Details

.normalize_options(options, item = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/props_template/extensions/cache.rb', line 12

def self.normalize_options(options, item = nil)
  key, rest = [*options]
  rest ||= {}

  if item && ::Proc === key
    key = key.call(item)
  end

  [key, rest]
end

Instance Method Details

#cache(key = nil, options = {}) ⇒ Object

The below was copied from the wonderful jbuilder library Its also MIT licensed, so no issues there. Thanks to the jbuilder authors!



66
67
68
69
70
71
72
73
74
# File 'lib/props_template/extensions/cache.rb', line 66

def cache(key = nil, options = {})
  if controller.perform_caching
    cache_fragment_for(key, options) do
      yield
    end
  else
    yield
  end
end

#cache_fragment_for(key, options, &block) ⇒ Object



76
77
78
79
80
81
# File 'lib/props_template/extensions/cache.rb', line 76

def cache_fragment_for(key, options, &block)
  return results[key] if results[key]

  key = cache_key(key, options)
  read_fragment_cache(key, options) || write_fragment_cache(key, options, &block)
end

#cache_key(key, options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/props_template/extensions/cache.rb', line 97

def cache_key(key, options)
  name_options = options.slice(:skip_digest, :virtual_path)
  key = @context.cache_fragment_name(key, **name_options)

  if @context.respond_to?(:combined_fragment_cache_key)
    key = @context.combined_fragment_cache_key(key)
  elsif ::Hash === key
    key = url_for(key).split("://", 2).last
  end

  ::ActiveSupport::Cache.expand_cache_key(key, :props)
end

#load_cache(keys, options = {}) ⇒ Object



59
60
61
# File 'lib/props_template/extensions/cache.rb', line 59

def load_cache(keys, options = {})
  @results = results.merge multi_fetch(keys, options)
end

#multi_fetch(keys, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/props_template/extensions/cache.rb', line 25

def multi_fetch(keys, options = {})
  result = {}
  key_to_ckey = {}
  ckeys = []

  keys.each do |k|
    ckey = cache_key(k, options)
    ckeys.push(ckey)
    key_to_ckey[k] = ckey
  end

  payload = {
    controller_name: controller.controller_name,
    action_name: controller.action_name
  }

  read_caches = {}

  ActiveSupport::Notifications.instrument("read_multi_fragments.action_view", payload) do |payload|
    read_caches = ::Rails.cache.read_multi(*ckeys, options)
    payload[:read_caches] = read_caches
  end

  keys.each do |k|
    ckey = key_to_ckey[k]

    if read_caches[ckey]
      result[k] = read_caches[ckey]
    end
  end

  result
end

#read_fragment_cache(key, options = nil) ⇒ Object



83
84
85
86
87
# File 'lib/props_template/extensions/cache.rb', line 83

def read_fragment_cache(key, options = nil)
  controller.instrument_fragment_cache :read_fragment, key do
    ::Rails.cache.read(key, options)
  end
end

#write_fragment_cache(key, options = nil) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/props_template/extensions/cache.rb', line 89

def write_fragment_cache(key, options = nil)
  controller.instrument_fragment_cache :write_fragment, key do
    yield.tap do |value|
      ::Rails.cache.write(key, value, options)
    end
  end
end