Sha256: ffbf3be94c9e778835c28affc3c11a1693fa5c4636f47b9d84c2eeac1cc81bd2

Contents?: true

Size: 1022 Bytes

Versions: 2

Compression:

Stored size: 1022 Bytes

Contents

module IdempotentRequest
  class RedisStorage
    attr_reader :redis, :namespace, :expire_time

    def initialize(redis, config = {})
      @redis = redis
      @namespace = config.fetch(:namespace, 'idempotency_keys')
      @expire_time = config[:expire_time]
    end

    def lock(key)
      setnx_with_expiration(lock_key(key), Time.now.to_f)
    end

    def unlock(key)
      redis.del(lock_key(key))
    end

    def read(key)
      redis.get(namespaced_key(key))
    end

    def write(key, payload)
      setnx_with_expiration(namespaced_key(key), payload)
    end

    private

    def setnx_with_expiration(key, data)
      redis.set(
        key,
        data,
        {}.tap do |options|
          options[:nx] = true
          options[:ex] = expire_time.to_i if expire_time.to_i > 0
        end
      )
    end

    def lock_key(key)
      namespaced_key("lock:#{key}")
    end

    def namespaced_key(key)
      [namespace, key.strip]
        .compact
        .join(':')
        .downcase
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
idempotent-request-0.1.6 lib/idempotent-request/redis_storage.rb
idempotent-request-0.1.5 lib/idempotent-request/redis_storage.rb