Sha256: 05d648c1e807a73c9c9753d1bf2346bd558501acc0c3dcbe6d157e573eab9121

Contents?: true

Size: 901 Bytes

Versions: 1

Compression:

Stored size: 901 Bytes

Contents

module Asynchronic
  module DataStore
    class InMemory

      def initialize
        @hash = {}
        @mutex = Mutex.new
      end

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

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

      def merge(key, hash)
        scoped_key = Key.new key
        hash.each do |k,v|
          set scoped_key[k].to_s, v
        end
      end

      def to_hash(key)
        children_key = "#{key}:"
        keys(children_key).inject({}) do |hash, k|
          hash[k[children_key.size..-1]] = get k
          hash
        end
      end

      def keys(key=nil)
        key ? keys.select { |k| k.start_with? key.to_s } : @hash.keys
      end

      def clear(key=nil)
        if key
          @hash.delete_if { |k,v| k.start_with? key.to_s }
        else
          @hash.clear
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asynchronic-0.1.0 lib/asynchronic/data_store/in_memory.rb