Module: Card::View::CacheAction
- Included in:
- Card::View
- Defined in:
- lib/card/view/cache_action.rb
Constant Summary collapse
- ACTIVE_CACHE_LEVEL =
{ always: :cache_yield, # read/write cache specifically for this view standard: :yield, # render view; it will only be cached within active view never: :stub # render a stub }.freeze
Instance Method Summary collapse
- #active_cache_action ⇒ Symbol
-
#active_cache_action_from_setting ⇒ Symbol
determine the cache action from the cache setting (assuming cache status is "active").
- #active_cache_ok? ⇒ True/False
-
#active_cache_permissible? ⇒ Boolean
apply any permission checks required by view.
-
#cache_action ⇒ Symbol
course of action based on config/status/options.
- #cache_on? ⇒ True/False
-
#cache_setting ⇒ Symbol
Mod developers can configure cache directives on view definitions.
-
#cache_status ⇒ Symbol
:off, :active, or :free.
-
#clean_enough_to_cache? ⇒ True/False
altered view requests and altered cards are not cacheable.
- #free_cache_action ⇒ Symbol
- #free_cache_ok? ⇒ True/False
- #log_cache_action ⇒ Object
-
#off_cache_action ⇒ Object
always skip all the magic.
-
#permission_task ⇒ Object
task directly associated with the view in its definition via the "perms" directive.
Instance Method Details
#active_cache_action ⇒ Symbol
63 64 65 |
# File 'lib/card/view/cache_action.rb', line 63 def active_cache_action active_cache_ok? ? active_cache_action_from_setting : :stub end |
#active_cache_action_from_setting ⇒ Symbol
determine the cache action from the cache setting (assuming cache status is "active")
95 96 97 98 |
# File 'lib/card/view/cache_action.rb', line 95 def active_cache_action_from_setting level = ACTIVE_CACHE_LEVEL[cache_setting] level || raise("unknown cache setting: #{cache_setting}") end |
#active_cache_ok? ⇒ True/False
68 69 70 71 72 73 |
# File 'lib/card/view/cache_action.rb', line 68 def active_cache_ok? return false unless parent && clean_enough_to_cache? return true if [:skip_perms] active_cache_permissible? end |
#active_cache_permissible? ⇒ Boolean
apply any permission checks required by view. (do not cache views with nuanced permissions)
77 78 79 80 81 82 83 84 |
# File 'lib/card/view/cache_action.rb', line 77 def active_cache_permissible? case when :none then true when parent. then true when Symbol then card.anyone_can?() else false end end |
#cache_action ⇒ Symbol
course of action based on config/status/options
8 9 10 11 12 |
# File 'lib/card/view/cache_action.rb', line 8 def cache_action log_cache_action do send "#{cache_status}_cache_action" end end |
#cache_on? ⇒ True/False
35 36 37 |
# File 'lib/card/view/cache_action.rb', line 35 def cache_on? Card.config.view_cache && format.class.view_caching? end |
#cache_setting ⇒ Symbol
Mod developers can configure cache directives on view definitions. eg: view :myview, cache: :standard do ...
There are three possible values for those rules.
- standard (default) cache when possible, but avoid double caching (caching one view while already caching another)
- always cache whenever possible, even if that means double caching
- never don't ever cache this view
Of these, "never" is most often used explicitly, usually in places where the view can be altered by things other than simple related card changes. It is important to note that to use "never", a view MUST be stubbable (ie, no foreign options). Otherwise the rendering may be involved in an active cache, reach an uncacheable view, attempt to stub it, and fail.
125 126 127 |
# File 'lib/card/view/cache_action.rb', line 125 def cache_setting format.view_cache_setting requested_view end |
#cache_status ⇒ Symbol
Returns :off, :active, or :free.
22 23 24 25 26 27 28 |
# File 'lib/card/view/cache_action.rb', line 22 def cache_status case when !cache_on? then :off # view caching is turned off, format- or system-wide when cache_active? then :active # another view cache is in progress; this view is inside it else :free # no other cache in progress end end |
#clean_enough_to_cache? ⇒ True/False
altered view requests and altered cards are not cacheable
131 132 133 134 135 136 137 |
# File 'lib/card/view/cache_action.rb', line 131 def clean_enough_to_cache? requested_view == ok_view && !card.unknown? && !card.db_content_changed? # FIXME: might consider other changes as disqualifying, though # we should make sure not to disallow caching of virtual cards end |
#free_cache_action ⇒ Symbol
49 50 51 |
# File 'lib/card/view/cache_action.rb', line 49 def free_cache_action free_cache_ok? ? :cache_yield : :yield end |
#free_cache_ok? ⇒ True/False
54 55 56 |
# File 'lib/card/view/cache_action.rb', line 54 def free_cache_ok? cache_setting != :never && clean_enough_to_cache? end |
#log_cache_action ⇒ Object
14 15 16 17 18 19 |
# File 'lib/card/view/cache_action.rb', line 14 def log_cache_action action = yield # TODO: make configurable puts "VIEW CACHE [#{action}] (#{card.name}##{requested_view})" if false action end |
#off_cache_action ⇒ Object
always skip all the magic
40 41 42 |
# File 'lib/card/view/cache_action.rb', line 40 def off_cache_action :yield end |