Sha256: a2e88ca3b09aa008bdd35aa24294944d17b3094d56f6b43b611df8b1583c9bda

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

class Redis
  class Store < self
    module Ttl
      def set(key, value, options = nil)
        if ttl = expires_in(options)
          setex(key, ttl.to_i, value, :raw => true)
        else
          super(key, value, options)
        end
      end

      def setnx(key, value, options = nil)
        if ttl = expires_in(options)
          setnx_with_expire(key, value, ttl.to_i, options)
        else
          super(key, value)
        end
      end

      protected
        def setnx_with_expire(key, value, ttl, options = {})
          with_multi_or_pipelined(options) do |transaction|
            if transaction.is_a?(Redis::Store) # for redis < 4.6
              setnx(key, value, :raw => true)
              expire(key, ttl)
            else
              transaction.setnx(key, value)
              transaction.expire(key, ttl)
            end
          end
        end

      private
        def expires_in(options)
          if options
            # Rack::Session           Merb                    Rails/Sinatra
            options[:expire_after] || options[:expires_in] || options[:expire_in]
          end
        end

        def with_multi_or_pipelined(options, &block)
          return pipelined(&block) if options.key?(:cluster) || options[:avoid_multi_commands]
          multi(&block)
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
redis-store-1.11.0 lib/redis/store/ttl.rb
redis-store-1.10.0 lib/redis/store/ttl.rb
redis-store-pika-1.9.2.1 lib/redis/store/ttl.rb
redis-store-1.9.2 lib/redis/store/ttl.rb