Module: Vident2::Caching
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/vident2/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` lives on the adapter base class (Phlex: `.rb` mtime; VC: sidecar template + `.rb` mtime).
Instance Method Summary collapse
- #cache_key_modifier ⇒ Object
- #cache_keys_for_sources(key_attributes) ⇒ Object
- #cacheable? ⇒ Boolean
- #component_modified_time ⇒ Object
- #generate_cache_key(index) ⇒ Object
- #generate_item_cache_key_from(item) ⇒ Object
Instance Method Details
#cache_key_modifier ⇒ Object
66 |
# File 'lib/vident2/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/vident2/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
64 |
# File 'lib/vident2/caching.rb', line 64 def cacheable? = respond_to?(:cache_key) |
#component_modified_time ⇒ Object
62 |
# File 'lib/vident2/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 |
# File 'lib/vident2/caching.rb', line 85 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vident2/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 |