Module: Vident::Capabilities::Caching

Extended by:
ActiveSupport::Concern
Defined in:
lib/vident/capabilities/caching.rb

Overview

Fragment-caching opt-in. Include into a component to get ‘cacheable?`, `cache_key`, and the `with_cache_key` / `depends_on` class helpers. `cache_component_modified_time` must be implemented by the adapter base class.

Instance Method Summary collapse

Instance Method Details

#cache_key_modifierObject



66
# File 'lib/vident/capabilities/caching.rb', line 66

def cache_key_modifier = ENV["RAILS_CACHE_ID"]

#cache_keys_for_sources(key_attributes) ⇒ Object



68
69
70
71
# File 'lib/vident/capabilities/caching.rb', line 68

def cache_keys_for_sources(key_attributes)
  sources = key_attributes.flat_map { |n| n.is_a?(Proc) ? instance_eval(&n) : send(n) }
  sources.compact.filter_map { |item| generate_item_cache_key_from(item) unless item == self }
end

#cacheable?Boolean

Returns:

  • (Boolean)


64
# File 'lib/vident/capabilities/caching.rb', line 64

def cacheable? = respond_to?(:cache_key)

#component_modified_timeObject



62
# File 'lib/vident/capabilities/caching.rb', line 62

def component_modified_time = self.class.component_modified_time

#generate_cache_key(index) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vident/capabilities/caching.rb', line 85

def generate_cache_key(index)
  key_attributes = self.class.named_cache_key_attributes[index]
  return nil unless key_attributes
  sources = cache_keys_for_sources(key_attributes)
  if sources.empty?
    raise ::Vident::ConfigurationError,
      "no cache key sources resolved for #{self.class.name} — ensure `with_cache_key` attributes return non-nil values"
  end
  key = "#{self.class.name}/#{sources.join("/")}"
  @cache_key[index] = cache_key_modifier.present? ? "#{key}/#{cache_key_modifier}" : key
end

#generate_item_cache_key_from(item) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vident/capabilities/caching.rb', line 73

def generate_item_cache_key_from(item)
  if item.respond_to?(:cache_key_with_version)
    item.cache_key_with_version
  elsif item.respond_to?(:cache_key)
    item.cache_key
  elsif item.is_a?(String)
    Digest::SHA1.hexdigest(item)
  else
    Digest::SHA1.hexdigest(Marshal.dump(item))
  end
end