Sha256: 636623e6a822b6b9ea972eecf74f0726bde5ee192c13602c32390e291af4f5cd

Contents?: true

Size: 1008 Bytes

Versions: 5

Compression:

Stored size: 1008 Bytes

Contents

module Asynchronic
  module DataStore
    class InMemory

      include Helper

      def initialize(hash={})
        @hash = {}
        @mutex = Mutex.new
        @keys_mutex = Hash.new { |h,k| h[k] = Mutex.new }
        self.class.connections[object_id] = self
      end

      def [](key)
        @hash[key.to_s]
      end

      def []=(key, value)
        @mutex.synchronize { @hash[key.to_s] = value }
      end

      def delete(key)
        @hash.delete key.to_s
      end

      def delete_cascade(key)
        keys = self.keys.select { |k| k.sections.first == key }
        keys.each { |k| delete k }
      end

      def keys
        @hash.keys.map { |k| Key[k] }
      end

      def synchronize(key, &block)
        @keys_mutex[key].synchronize(&block)
      end

      def connection_args
        [object_id]
      end

      def self.connect(object_id)
        connections[object_id]
      end

      private

      def self.connections
        @connections ||= {}
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asynchronic-1.6.1 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.6.0 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.5.2 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.5.1 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.5.0 lib/asynchronic/data_store/in_memory.rb