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
66 67 68 |
# File 'lib/card/view/cache_action.rb', line 66 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")
96 97 98 99 |
# File 'lib/card/view/cache_action.rb', line 96 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
71 72 73 74 75 |
# File 'lib/card/view/cache_action.rb', line 71 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)
79 80 81 82 83 84 85 86 |
# File 'lib/card/view/cache_action.rb', line 79 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
37 38 39 |
# File 'lib/card/view/cache_action.rb', line 37 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.
126 127 128 |
# File 'lib/card/view/cache_action.rb', line 126 def cache_setting format.view_cache_setting requested_view end |
#cache_status ⇒ Symbol
Returns :off, :active, or :free.
23 24 25 26 27 28 29 |
# File 'lib/card/view/cache_action.rb', line 23 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
133 134 135 136 137 138 139 |
# File 'lib/card/view/cache_action.rb', line 133 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
52 53 54 |
# File 'lib/card/view/cache_action.rb', line 52 def free_cache_action free_cache_ok? ? :cache_yield : :yield end |
#free_cache_ok? ⇒ True/False
57 58 59 |
# File 'lib/card/view/cache_action.rb', line 57 def free_cache_ok? cache_setting != :never && clean_enough_to_cache? end |
#log_cache_action ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/card/view/cache_action.rb', line 14 def log_cache_action action = yield if false # TODO: make configurable puts "VIEW CACHE [#{action}] (#{card.name}##{requested_view})" end action end |
#off_cache_action ⇒ Object
always skip all the magic
42 43 44 |
# File 'lib/card/view/cache_action.rb', line 42 def off_cache_action :yield end |