Sha256: 9a01be1f6cf440fffe61be08104222b84d3a32603a9c13a20d2c71ecc6699c6a

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

class Redis
  class MarshaledClient < self
    def marshalled_set(key, val, options = nil)
      val = marshal_value(val, options)
      if ttl = expires_in(options)
        setex key, ttl, val
      else
        set key, val
      end
    end

    def marshalled_setnx(key, val, options = nil)
      val = marshal_value(val, options)
      if expires_in = expires_in(options)
        setnx_with_expire key, val, expires_in
      else
        setnx key, val
      end
    end

    def setnx_with_expire(key, value, ttl)
      multi do
        setnx(key, val)
        expire(key, expires_in)
      end
    end

    def marshalled_get(key, options = nil)
      result = @client.call(:get, key)
      result = Marshal.load result if unmarshal?(result, options)
      result
    end

    def to_s
      "Redis Client connected to #{@client.host}:#{@client.port} against DB #{@client.db}"
    end

    private
      def marshal_value(val, options)
        raw?(options) ? val : Marshal.dump(val)
      end

      def unmarshal?(result, options)
        result && result.size > 0 && !raw?(options)
      end

      def raw?(options)
        options && options[:raw]
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis-store-1.0.0.beta1 lib/redis/marshaled_client.rb