Sha256: 86a9f3e710b18539e45ee3364ef9307c8805cf92edded859eb7d4c49141b8302

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require "concurrent/map"

module ROM
  # Thread-safe cache used by various rom components
  #
  # @api private
  class Cache
    attr_reader :objects

    # @api private
    class Namespaced
      # @api private
      attr_reader :cache

      # @api private
      attr_reader :namespace

      # @api private
      attr_reader :parent

      # @api private
      def initialize(cache, namespace, parent:)
        @cache = cache
        @namespace = namespace
        @parent = parent
      end

      # @api private
      def namespaced(namespace)
        parent.namespaced(namespace)
      end

      # @api private
      def [](key)
        cache[[namespace, key].hash]
      end

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

      # @api private
      def size
        cache.size
      end

      # @api private
      def inspect
        %(#<#{self.class} size=#{size}>)
      end
    end

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

    # @api private
    def [](key)
      cache[key]
    end

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

    # @api private
    def size
      objects.size
    end

    # @api private
    def namespaced(namespace)
      @namespaced[namespace] ||= Namespaced.new(objects, namespace, parent: self)
    end

    # @api private
    def inspect
      %(#<#{self.class} size=#{size} namespaced=#{@namespaced.inspect}>)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-6.0.0.alpha1 lib/rom/cache.rb