Sha256: dc0963e1ae58055a56ab6b9cf6ccb0dcddd699d63cb2eca6fdd6a8faf1f015b1

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module Asynchronic
  module DataStore
    class Redis

      TIMEOUT = 0.001
      LOCKED = 'locked'

      include Helper

      def initialize(scope, *args)
        @scope = Key.new 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 keys
        @connection.keys(@scope['*']).map { |k| Key.new(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

5 entries across 5 versions & 1 rubygems

Version Path
asynchronic-1.4.0 lib/asynchronic/data_store/redis.rb
asynchronic-1.3.1 lib/asynchronic/data_store/redis.rb
asynchronic-1.3.0 lib/asynchronic/data_store/redis.rb
asynchronic-1.2.2 lib/asynchronic/data_store/redis.rb
asynchronic-1.2.1 lib/asynchronic/data_store/redis.rb