Module: Vident::Caching

Extended by:
ActiveSupport::Concern
Defined in:
lib/vident/caching.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.

Instance Method Summary collapse

Instance Method Details

#cache_key_modifierObject



84
# File 'lib/vident/caching.rb', line 84

def cache_key_modifier = ENV["RAILS_CACHE_ID"]

#cache_keys_for_sources(key_attributes) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/vident/caching.rb', line 86

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)


82
# File 'lib/vident/caching.rb', line 82

def cacheable? = respond_to?(:cache_key)

#component_modified_timeObject

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



80
# File 'lib/vident/caching.rb', line 80

def component_modified_time = self.class.component_modified_time

#generate_cache_key(index) ⇒ Object

Raises:

  • (StandardError)


106
107
108
109
110
111
112
# File 'lib/vident/caching.rb', line 106

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vident/caching.rb', line 94

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