Sha256: 72a56263a7f28abb88abe588ecb94ef851bf13280b2245a058f267221e0b70b6

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 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
            setnx(key, value, :raw => true)
            expire(key, ttl)
          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 & 1 rubygems

Version Path
redis-store-1.9.0 lib/redis/store/ttl.rb
redis-store-1.8.2 lib/redis/store/ttl.rb
redis-store-1.8.1 lib/redis/store/ttl.rb
redis-store-1.8.0 lib/redis/store/ttl.rb