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

Instance Method Details

#active_cache_actionSymbol

Returns:

  • (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_settingSymbol

determine the cache action from the cache setting (assuming cache status is "active")

Returns:

  • (Symbol)

    cache action



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

Returns:

  • (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 normalized_options[:skip_perms]
  active_cache_permissible?
end

#active_cache_permissible?Boolean

apply any permission checks required by view. (do not cache views with nuanced permissions)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/card/view/cache_action.rb', line 79

def active_cache_permissible?
  case permission_task
  when :none                  then true
  when parent.permission_task then true
  when Symbol                 then card.anyone_can?(permission_task)
  else                             false
  end
end

#cache_actionSymbol

course of action based on config/status/options

Returns:

  • (Symbol)

    :yield, :cache_yield, or



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

Returns:

  • (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_settingSymbol

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.

Returns:

  • (Symbol)

    :standard, :always, or :never



126
127
128
# File 'lib/card/view/cache_action.rb', line 126

def cache_setting
  format.view_cache_setting requested_view
end

#cache_statusSymbol

Returns :off, :active, or :free.

Returns:

  • (Symbol)

    :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

Returns:

  • (True/False)


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_actionSymbol

Returns:

  • (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

Returns:

  • (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_actionObject



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_actionObject

always skip all the magic



42
43
44
# File 'lib/card/view/cache_action.rb', line 42

def off_cache_action
  :yield
end

#permission_taskObject

task directly associated with the view in its definition via the "perms" directive



90
91
92
# File 'lib/card/view/cache_action.rb', line 90

def permission_task
  @permission_task ||= Card::Format.perms[requested_view] || :read
end