Module: Card::View::Cache

Included in:
Card::View
Defined in:
lib/card/view/cache.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#array_for_cache_key(array) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/card/view/cache.rb', line 85

def array_for_cache_key array
  # TODO: needs better handling of edit_structure
  #  currently we pass complete structure as nested array
  array.map do |item|
    item.is_a?(Array) ? item.join(":") : item.to_s
  end.sort.join ","
end

#cache_active?true/false

Is there already a view cache in progress on which this one depends?

Note that if you create a brand new independent format object (ie, not a subformat) its activity will be treated as unrelated to this caching/rendering.

Returns:

  • (true/false)


39
40
41
# File 'lib/card/view/cache.rb', line 39

def cache_active?
  deep_root? ? false : self.class.caching?
end

#cache_fetchObject

If view is cached, retrieve it. Otherwise render and store it. Uses the primary cache API.



45
46
47
48
49
50
51
52
# File 'lib/card/view/cache.rb', line 45

def cache_fetch
  caching do
    self.class.cache.fetch cache_key do
      register_cache_key
      yield
    end
  end
end

#cache_keyObject



64
65
66
67
68
# File 'lib/card/view/cache.rb', line 64

def cache_key
  @cache_key ||= [
    card.key, format.class, format.nest_mode, options_for_cache_key
  ].map(&:to_s).join "-"
end

#cache_renderString (usually)

Fetch view via cache and, when appropriate, render its stubs

If this is a free cache action (see CacheAction), we go through the stubs and render them now. If the cache is active (ie, we are inside another view), we do not worry about stubs but keep going, because the free cache we're inside will take care of those stubs.

Returns:

  • (String (usually))

    rendered view



28
29
30
31
# File 'lib/card/view/cache.rb', line 28

def cache_render
  cached_view = cache_fetch { yield }
  cache_active? ? cached_view : format.stub_render(cached_view)
end

#cachingObject

keep track of nested cache fetching



55
56
57
# File 'lib/card/view/cache.rb', line 55

def caching
  self.class.caching(self) { yield }
end

#fetch(&block) ⇒ rendered view or stub

Support context-aware card view caching.

render or retrieve view (or stub) with current options

Returns:

  • (rendered view or stub)


13
14
15
16
17
18
19
# File 'lib/card/view/cache.rb', line 13

def fetch &block
  case cache_action
    when :yield       then yield                # simple render
    when :cache_yield then cache_render(&block) # render to/from cache
    when :stub        then stub                 # render stub
  end
end

#hash_for_cache_key(hash) ⇒ Object



79
80
81
82
83
# File 'lib/card/view/cache.rb', line 79

def hash_for_cache_key hash
  hash.keys.sort.map do |key|
    option_for_cache_key key, hash[key]
  end.join ";"
end

#option_for_cache_key(key, value) ⇒ Object



93
94
95
# File 'lib/card/view/cache.rb', line 93

def option_for_cache_key key, value
  "#{key}:#{option_value_to_string value}"
end

#option_value_to_string(value) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/card/view/cache.rb', line 97

def option_value_to_string value
  case value
    when Hash then "{#{hash_for_cache_key value}}"
    when Array then array_for_cache_key(value)
    else value.to_s
  end
end

#options_for_cache_keyObject



75
76
77
# File 'lib/card/view/cache.rb', line 75

def options_for_cache_key
  hash_for_cache_key(live_options) + hash_for_cache_key(viz_hash)
end

#register_cache_keyObject

Registers the cached view for later clearing in the event of related card changes



71
72
73
# File 'lib/card/view/cache.rb', line 71

def register_cache_key
  card.register_view_cache_key cache_key
end