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



88
89
90
# File 'lib/vident/caching.rb', line 88

def cache_key_modifier
  ENV["RAILS_CACHE_ID"]
end

#cache_keys_for_sources(key_attributes) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/vident/caching.rb', line 92

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)


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

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



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

def component_modified_time
  self.class.component_modified_time
end

#generate_cache_key(index) ⇒ Object

Raises:

  • (StandardError)


112
113
114
115
116
117
118
# File 'lib/vident/caching.rb', line 112

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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vident/caching.rb', line 100

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