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)


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_settingSymbol

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

Returns:

  • (Symbol)

    cache action



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

Returns:

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


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

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)


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_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



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

def cache_setting
  format.view_cache_setting requested_view
end

#cache_statusSymbol

Returns :off, :active, or :free.

Returns:

  • (Symbol)

    :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

Returns:

  • (True/False)


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_actionSymbol

Returns:

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

Returns:

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



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_actionObject

always skip all the magic



40
41
42
# File 'lib/card/view/cache_action.rb', line 40

def off_cache_action
  :yield
end

#permission_taskObject

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



88
89
90
# File 'lib/card/view/cache_action.rb', line 88

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