Sha256: 1aeb3e096c2bf8ea8eeffd13b3110f2a6d58d477321d11c8cea36f681df14b14

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module Asynchronic
  module DataStore
    class Redis

      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 Asynchronic.redis_data_store_sync_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

6 entries across 6 versions & 1 rubygems

Version Path
asynchronic-1.6.3 lib/asynchronic/data_store/redis.rb
asynchronic-1.6.2 lib/asynchronic/data_store/redis.rb
asynchronic-1.6.1 lib/asynchronic/data_store/redis.rb
asynchronic-1.6.0 lib/asynchronic/data_store/redis.rb
asynchronic-1.5.2 lib/asynchronic/data_store/redis.rb
asynchronic-1.5.1 lib/asynchronic/data_store/redis.rb