Module: ActiveMatrix::Cacheable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/active_matrix/cacheable.rb
Overview
Provides caching functionality for Matrix objects Handles serialization/deserialization to work with Rails.cache
Instance Method Summary collapse
-
#cache_attributes ⇒ Object
Override in each class to specify what to cache.
-
#cache_id ⇒ Object
Override in each class if ID method is different.
-
#cache_key(*suffixes) ⇒ Object
Generate a cache key for this object.
-
#from_cache? ⇒ Boolean
Check if this object was loaded from cache.
-
#to_cache ⇒ Object
Convert object to cacheable hash.
Instance Method Details
#cache_attributes ⇒ Object
Override in each class to specify what to cache
45 46 47 48 49 50 51 52 53 |
# File 'lib/active_matrix/cacheable.rb', line 45 def cache_attributes if respond_to?(:attributes) attributes elsif respond_to?(:to_h) to_h else {} end end |
#cache_id ⇒ Object
Override in each class if ID method is different
62 63 64 |
# File 'lib/active_matrix/cacheable.rb', line 62 def cache_id respond_to?(:id) ? id : object_id end |
#cache_key(*suffixes) ⇒ Object
Generate a cache key for this object
56 57 58 59 |
# File 'lib/active_matrix/cacheable.rb', line 56 def cache_key(*suffixes) base_key = "#{self.class.name.underscore}:#{cache_id}" suffixes.any? ? "#{base_key}:#{suffixes.join(':')}" : base_key end |
#from_cache? ⇒ Boolean
Check if this object was loaded from cache
67 68 69 |
# File 'lib/active_matrix/cacheable.rb', line 67 def from_cache? @cached_at.present? end |
#to_cache ⇒ Object
Convert object to cacheable hash
34 35 36 37 38 39 40 41 42 |
# File 'lib/active_matrix/cacheable.rb', line 34 def to_cache data = cache_attributes.merge( _cache_class: self.class.name, _cached_at: Time.current ) # Ensure we only cache serializable data data.deep_stringify_keys end |