Sha256: 21dfcaf13306bade06cdfe1268df753dc4dbf6cb33004f922db2e962de858897

Contents?: true

Size: 852 Bytes

Versions: 3

Compression:

Stored size: 852 Bytes

Contents

require 'concurrent/map'

module ROM
  # @api private
  class Cache
    attr_reader :objects

    class Namespaced
      attr_reader :cache

      attr_reader :namespace

      def initialize(cache, namespace)
        @cache = cache
        @namespace = namespace.to_sym
      end

      def [](key)
        cache[[namespace, key].hash]
      end

      def fetch_or_store(*args, &block)
        cache.fetch_or_store([namespace, args.hash].hash, &block)
      end
    end

    # @api private
    def initialize
      @objects = Concurrent::Map.new
      @namespaced = {}
    end

    def [](key)
      cache[key]
    end

    # @api private
    def fetch_or_store(*args, &block)
      objects.fetch_or_store(args.hash, &block)
    end

    def namespaced(namespace)
      @namespaced[namespace] ||= Namespaced.new(objects, namespace)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-core-4.0.0.beta3 lib/rom/cache.rb
rom-core-4.0.0.beta2 lib/rom/cache.rb
rom-core-4.0.0.beta1 lib/rom/cache.rb