Sha256: f1075273851428817432cf636d1858e4675aa2c25bc382db77b7bd4e275ea005

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Asynchronic
  module DataStore
    class Redis

      attr_reader :connection

      def initialize(*args)
        @connection = ::Redis.new *args
      end

      def get(key)
        value = connection.get root[key]
        value ? Marshal.load(value) : nil
      end

      def set(key, value)
        connection.set root[key], Marshal.dump(value)
      end

      def merge(key, hash)
        scoped_key = Key.new key
        hash.each do |k,v|
          set scoped_key[k], 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)
        keys = key ? connection.keys("#{root[key]}*") : connection.keys
        keys.map { |k| k[(root.size + 1)..-1] }
      end

      def clear(key=nil)
        keys(key).each { |k| connection.del root[k] }
      end

      private

      def root
        Key.new :asynchronic
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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