Sha256: 9c335983e3b3b587e7766ea935314c7f082b057d279d3d82a9b675d264fba680

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

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)
        Marshal.load(@hash[key.to_s]) if @hash.key? key.to_s
      end

      def []=(key, value)
        @mutex.synchronize { @hash[key.to_s] = Marshal.dump(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

8 entries across 8 versions & 1 rubygems

Version Path
asynchronic-3.0.3 lib/asynchronic/data_store/in_memory.rb
asynchronic-3.0.2 lib/asynchronic/data_store/in_memory.rb
asynchronic-3.0.1 lib/asynchronic/data_store/in_memory.rb
asynchronic-3.0.0 lib/asynchronic/data_store/in_memory.rb
asynchronic-2.0.1 lib/asynchronic/data_store/in_memory.rb
asynchronic-2.0.0 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.6.3 lib/asynchronic/data_store/in_memory.rb
asynchronic-1.6.2 lib/asynchronic/data_store/in_memory.rb