Module: Card::View::Cache
- Included in:
- Card::View
- Defined in:
- lib/card/view/cache.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #array_for_cache_key(array) ⇒ Object
-
#cache_active? ⇒ true/false
Is there already a view cache in progress on which this one depends?.
-
#cache_fetch ⇒ Object
If view is cached, retrieve it.
-
#cache_key ⇒ Object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ VIEW CACHE KEY.
-
#cache_render ⇒ String (usually)
Fetch view via cache and, when appropriate, render its stubs.
-
#caching ⇒ Object
keep track of nested cache fetching.
-
#fetch(&block) ⇒ rendered view or stub
Support context-aware card view caching.
- #hash_for_cache_key(hash) ⇒ Object
- #option_for_cache_key(key, value) ⇒ Object
- #option_value_to_string(value) ⇒ Object
- #options_for_cache_key ⇒ Object
-
#register_cache_key ⇒ Object
Registers the cached view for later clearing in the event of related card changes.
Instance Method Details
#array_for_cache_key(array) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/card/view/cache.rb', line 86 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.
42 43 44 |
# File 'lib/card/view/cache.rb', line 42 def cache_active? deep_root? ? false : self.class.caching? end |
#cache_fetch ⇒ Object
If view is cached, retrieve it. Otherwise render and store it. Uses the primary cache API.
48 49 50 51 52 53 54 55 |
# File 'lib/card/view/cache.rb', line 48 def cache_fetch caching do self.class.cache.fetch cache_key do register_cache_key yield end end end |
#cache_key ⇒ Object
VIEW CACHE KEY
65 66 67 68 69 |
# File 'lib/card/view/cache.rb', line 65 def cache_key @cache_key ||= [ card.key, format.class, format.nest_mode, ].map(&:to_s).join "-" end |
#cache_render ⇒ String (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.
30 31 32 33 |
# File 'lib/card/view/cache.rb', line 30 def cache_render cached_view = cache_fetch { yield } cache_active? ? cached_view : format.stub_render(cached_view) end |
#caching ⇒ Object
keep track of nested cache fetching
58 59 60 |
# File 'lib/card/view/cache.rb', line 58 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
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
80 81 82 83 84 |
# File 'lib/card/view/cache.rb', line 80 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
94 95 96 |
# File 'lib/card/view/cache.rb', line 94 def option_for_cache_key key, value "#{key}:#{option_value_to_string value}" end |
#option_value_to_string(value) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/card/view/cache.rb', line 98 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_key ⇒ Object
76 77 78 |
# File 'lib/card/view/cache.rb', line 76 def hash_for_cache_key() + hash_for_cache_key(viz_hash) end |
#register_cache_key ⇒ Object
Registers the cached view for later clearing in the event of related card changes
72 73 74 |
# File 'lib/card/view/cache.rb', line 72 def register_cache_key card.register_view_cache_key cache_key end |