Sha256: 87e514d186ebe6565f7e1f3deadb165c5cd4d5d774d00dc7b8fe8759ba4f0987

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module FFI
  module HiredisVip
    class Set
      def initialize(client)
        @client = client
      end

      def psetex(key, value, expiry)
        set(key, value, :px => expiry)
      end

      def set(key, value, options = {})
        reply = nil
        command = "SET %b %b"
        value = value.to_s
        command_args = [ :pointer, key, :size_t, key.size, :pointer, value, :size_t, value.size ]

        if options[:ex]
          expiry = "#{options[:ex]}"
          command << " EX %b"
          command_args << :string << expiry << :size_t << expiry.size
        end

        if options[:px]
          px_expiry = "#{options[:px]}"
          command << " PX %b"
          command_args << :string << px_expiry << :size_t << px_expiry.size
        end
        
        command << " NX" if options[:nx]
        command << " XX" if options[:xx]

        synchronize do |connection|
          reply = @client.execute_command(connection, command, *command_args)
        end

        return nil if reply.nil? || reply.null?

        case reply[:type] 
        when :REDIS_REPLY_STRING
          reply[:str]
        when :REDIS_REPLY_STATUS
          reply[:str]
        when :REDIS_REPLY_NIL
          nil
        else
          ""
        end
      end

      def setex(key, value, expiry)
        set(key, value, :ex => expiry)
      end

      def setnx(key, value)
        set(key, value, :nx => true)
      end

      private

      def synchronize
        @client.synchronize do |connection|
          yield(connection)
        end
      end

    end # class Set
  end # module HiredisVip
end # module FFI

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffi-hiredis_vip-0.1.0.pre3 lib/ffi/hiredis_vip/set.rb