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)


70
71
72
73
74
# File 'lib/card/view/cache_action.rb', line 70

def active_cache_action
  validate_active_cache_action do
    active_cache_ok? ? active_cache_action_from_setting : :stub
  end
end

#active_cache_action_from_settingSymbol

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

Returns:

  • (Symbol)

    cache action



111
112
113
114
# File 'lib/card/view/cache_action.rb', line 111

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)


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

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)


94
95
96
97
98
99
100
101
# File 'lib/card/view/cache_action.rb', line 94

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



141
142
143
# File 'lib/card/view/cache_action.rb', line 141

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)


148
149
150
151
152
153
154
# File 'lib/card/view/cache_action.rb', line 148

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
60
61
62
# File 'lib/card/view/cache_action.rb', line 57

def free_cache_ok?
  cache_setting != :never &&
    foreign_live_options.empty? &&
    clean_enough_to_cache?
  # note: foreign options are a problem in the free cache, because
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



105
106
107
# File 'lib/card/view/cache_action.rb', line 105

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

#validate_active_cache_actionObject

catch recursive views and invalid stubs



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

def validate_active_cache_action
  return :yield if ok_view == :too_deep
  #FIXME - this allows "too deep" error to be cached inside another view. may need a "raise" cache action?
  action = yield
  validate_stub! if action == :stub
  action
end