Module: Vident::ViewComponent::Caching

Extended by:
ActiveSupport::Concern
Defined in:
lib/vident/view_component/caching.rb,
lib/vident/view_component/caching/railtie.rb,
lib/vident/view_component/caching/version.rb

Overview

Rails fragment caching works by either expecting the cached key object to respond to ‘cache_key` or for that object to be an array or hash.

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#cache_key_modifierObject



112
113
114
# File 'lib/vident/view_component/caching.rb', line 112

def cache_key_modifier
  ENV["RAILS_CACHE_ID"]
end

#cache_keys_for_sources(key_attributes) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/vident/view_component/caching.rb', line 116

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

#cacheable?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/vident/view_component/caching.rb', line 108

def cacheable?
  respond_to? :cache_key
end

#component_modified_timeObject

Component modified time which is combined with other cache key attributes to generate cache key for an instance



104
105
106
# File 'lib/vident/view_component/caching.rb', line 104

def component_modified_time
  self.class.component_modified_time
end

#generate_cache_key(index) ⇒ Object

Raises:

  • (StandardError)


136
137
138
139
140
141
142
# File 'lib/vident/view_component/caching.rb', line 136

def generate_cache_key(index)
  key_attributes = self.class.named_cache_key_attributes[index]
  return nil unless key_attributes
  key = "#{self.class.name}/#{cache_keys_for_sources(key_attributes).join("/")}"
  raise StandardError, "Cache key for key #{key} is blank!" if key.blank?
  @cache_key[index] = cache_key_modifier.present? ? "#{key}/#{cache_key_modifier}" : key
end

#generate_item_cache_key_from(item) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vident/view_component/caching.rb', line 124

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