Sha256: 85b06fe0caf3cb13e30efb795342b01e0a86251d15dc267539699bfd8cd5b379

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 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"
        command_args = [ :string, key, :size_t, key.size, :string, 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 = ::FFI::HiredisVip::Core.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.pre1 lib/ffi/hiredis_vip/set.rb