Sha256: b49556a65523450a57e1b86cb27ba7a46e20464e7068835f933fcc79f658f9c6

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

module Asynchronic
  module DataStore
    class Redis

      LOCKED = 'locked'

      include Helper

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

      def initialize(scope, options={})
        @scope = Key[scope]
        @options = options
      end

      def [](key)
        value = redis.call! 'GET', scope[key]
        value ? Marshal.load(value) : nil
      rescue => ex
        Asynchronic.logger.warn('Asynchronic') { ex.message }
        value
      end

      def []=(key, value)
        redis.call! 'SET', scope[key], Marshal.dump(value)
      end

      def delete(key)
        redis.call! 'DEL', scope[key]
      end

      def delete_cascade(key)
        redis.call! 'DEL', scope[key]
        redis.call!('KEYS', scope[key]['*']).each { |k| redis.call! 'DEL', k }
      end

      def keys
        redis.call!('KEYS', scope['*']).map { |k| Key[k].remove_first }
      end

      def synchronize(key)
        while redis.call!('GETSET', scope[key][LOCKED], LOCKED) == LOCKED
          sleep Asynchronic.redis_data_store_sync_timeout
        end
        yield
      ensure
        redis.call! 'DEL', scope[key][LOCKED]
      end

      def connection_args
        [scope, options]
      end

      private

      attr_reader :scope, :options

      def redis
        @redis ||= Asynchronic.establish_redis_connection options
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
asynchronic-4.0.3 lib/asynchronic/data_store/redis.rb
asynchronic-4.0.2 lib/asynchronic/data_store/redis.rb
asynchronic-4.0.1 lib/asynchronic/data_store/redis.rb
asynchronic-4.0.0 lib/asynchronic/data_store/redis.rb