Module: Card::View::Cache

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

Overview

cache mechanics for view caching

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#cache_fetchObject

adds tracking, mapping, and stub handling to standard cache fetching



6
7
8
9
10
11
12
13
14
15
# File 'lib/card/view/cache.rb', line 6

def cache_fetch
  cached_view = caching do
    self.class.cache.fetch cache_key do
      card.register_view_cache_key cache_key
      yield
    end
  end

  caching? ? cached_view : format.stub_render(cached_view)
end

#cache_keyObject



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

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

#cachingObject

tracks that a cache fetch is in progress



18
19
20
# File 'lib/card/view/cache.rb', line 18

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

#caching?Boolean

answers: should this cache fetch depend on one already in progress? Note that if you create a brand new format object (ie, not a subformat) midrender, (eg card.format...), it needs to be treated as unrelated to any caching in progress.

Returns:

  • (Boolean)


26
27
28
# File 'lib/card/view/cache.rb', line 26

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

#deep_root?Boolean

neither view nor format has a parent

Returns:

  • (Boolean)


31
32
33
# File 'lib/card/view/cache.rb', line 31

def deep_root?
  !parent && !format.parent
end

#hash_for_cache_key(hash) ⇒ Object



49
50
51
52
53
# File 'lib/card/view/cache.rb', line 49

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/card/view/cache.rb', line 55

def option_for_cache_key key, value
  string_value =
    case value
    when Hash then "{#{hash_for_cache_key value}}"
    when Array then
      # TODO: needs better handling of edit_structure
      #       currently we pass complete structure as nested array
      value.map do |item|
        item.is_a?(Array) ? item.join(":") : item.to_s
      end.sort.join ","
    else value.to_s
    end
  "#{key}:#{string_value}"
end

#options_for_cache_keyObject



45
46
47
# File 'lib/card/view/cache.rb', line 45

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

#rootObject



35
36
37
# File 'lib/card/view/cache.rb', line 35

def root
  @root = parent ? parent.root : self
end