Sha256: 0bc0be13f96d0e287b87b1a998e6e8961d3fed1be2bbca923b8087a5308714cb

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module Asynchronic
  module DataStore
    class Redis

      TIMEOUT = 0.001
      LOCKED = 'locked'

      include Helper

      def initialize(scope, *args)
        @scope = Key[scope]
        @connection = ::Redis.new(*args)
      end

      def [](key)
        value = @connection.get @scope[key]
        value ? Marshal.load(value) : nil
      rescue => ex
        Asynchronic.logger.warn('Asynchronic') { ex.message }
        value
      end

      def []=(key, value)
        @connection.set @scope[key], Marshal.dump(value)
      end

      def delete(key)
        @connection.del @scope[key]
      end

      def delete_cascade(key)
        @connection.del @scope[key]
        @connection.keys(@scope[key]['*']).each { |k| @connection.del k }
      end

      def keys
        @connection.keys(@scope['*']).map { |k| Key[k].remove_first }
      end

      def synchronize(key)
        while @connection.getset(@scope[key][LOCKED], LOCKED) == LOCKED
          sleep TIMEOUT
        end
        yield
      ensure
        @connection.del @scope[key][LOCKED]
      end

      def connection_args
        [@scope, @connection.client.options]
      end

      def self.connect(*args)
        new(*args)
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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