Sha256: c406e73cf2c2224ac75fbf2d05d20b2c8d1ab7e4048e6504270c916d8b3a341a

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Autobots
  module Helpers
    module Caching
      extend ActiveSupport::Concern

      def initialize(_, options = {})
        super
        self.cache = options[:cache] if options.has_key?(:cache)
      end

      def self.prepended(klass)
        klass.class_eval do
          class_attribute :cache
        end
      end
      
      def data
        return @data if defined?(@data)

        if cache
          key_proc = options.fetch(:cache_key) do
            method(:cache_key)
          end
          identifiers = objects.inject({}) do |acc, obj|
            acc[key_proc.call(obj, self)] = obj
            acc
          end

          if options[:force_reload]
            identifiers.each do |key, _|
              cache.delete key
            end
          end

          # misses: { key => obj }
          @data = BulkCacheFetcher.new(cache).fetch(identifiers) do |misses|
            roll_out(misses.values)
          end
        else
          @data = super
        end
        @data
      end

      protected

      def serializer_cache_key
        return @serializer_cache_key if defined?(@serializer_cache_key)
        @serializer_cache_key = [serializer.name, Digest::SHA256.hexdigest(serializer._attributes.keys.to_s + serializer._associations.keys.to_s)[0..12]]
      end

      def cache_key(object, _)
        [object.cache_key, *self.serializer_cache_key, 'serializable-hash']
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
autobots-0.2.3 lib/autobots/helpers/caching.rb