Sha256: 53ea22abba8d524a6d9188b7e94895613eda46ce81e70fd47117d2e695695373

Contents?: true

Size: 613 Bytes

Versions: 6

Compression:

Stored size: 613 Bytes

Contents

# frozen_string_literal: true

module ActiveRecordDataLoader
  module ActiveRecord
    class PerRowValueCache
      class << self
        def [](key)
          caches[key] ||= new
        end

        def clear
          @caches = {}
        end

        private

        def caches
          @caches ||= clear
        end
      end

      def initialize
        @row_caches = Hash.new { |hash, key| hash[key] = {} }
      end

      def get_or_set(model:, row:)
        @row_caches[model.name].shift if @row_caches[model.name].size > 1

        @row_caches[model.name][row] ||= yield
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
active_record_data_loader-1.3.1 lib/active_record_data_loader/active_record/per_row_value_cache.rb
active_record_data_loader-1.3.0 lib/active_record_data_loader/active_record/per_row_value_cache.rb
active_record_data_loader-1.2.0 lib/active_record_data_loader/active_record/per_row_value_cache.rb
active_record_data_loader-1.1.0 lib/active_record_data_loader/active_record/per_row_value_cache.rb
active_record_data_loader-1.0.2 lib/active_record_data_loader/active_record/per_row_value_cache.rb
active_record_data_loader-1.0.1 lib/active_record_data_loader/active_record/per_row_value_cache.rb