Sha256: 5f4a01bca00e1eb5bd517e4aa679e21e8423707d5c4ee9c4b657b98f9e2e9e5b

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Suo
  module Client
    class Redis < Base
      OK_STR = "OK".freeze

      def initialize(key, options = {})
        options[:client] ||= ::Redis.new(options[:connection] || {})
        super
      end

      def clear
        with { |r| r.del(@key) }
      end

      private

      def with(&block)
        if @client.respond_to?(:with)
          @client.with(&block)
        else
          yield @client
        end
      end

      def get
        [with { |r| r.get(@key) }, nil]
      end

      def set(newval, _, expire: false)
        ret = with do |r|
          r.multi do |rr|
            if expire
              rr.setex(@key, @options[:ttl], newval)
            else
              rr.set(@key, newval)
            end
          end
        end

        ret && ret[0] == OK_STR
      end

      def synchronize
        with { |r| r.watch(@key) { yield } }
      ensure
        with { |r| r.unwatch }
      end

      def initial_set(val = BLANK_STR)
        set(val, nil)
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
suo-0.4.0 lib/suo/client/redis.rb