Sha256: a9a76a30269031dfd8df9adc78076b1699a3f185c259ccb75b587ce03b4f9b7e

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

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

      def mget(*keys)
        reply = nil
        keys = keys.flatten
        command = "MGET"
        command_args = []
        keys.each do |key|
          command << " %b"
          command_args << :string << key << :size_t << key.size
        end

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

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

        # TODO: more error checking here? what is correct response on nothing?
        case reply[:type]
        when :REDIS_REPLY_ARRAY
          mget_results_to_array(reply)
        else
          []
        end
      end

      private

      def mget_results_to_array(array_reply)
        mget_results = []

        0.upto(array_reply[:elements] - 1) do |element_number|
          result = ::FFI::HiredisVip::Core.redisReplyElement(array_reply, element_number)

          case result[:type]
          when :REDIS_REPLY_STRING
            mget_results << result[:str]
          when :REDIS_REPLY_NIL
            mget_results << nil
          end
        end

        mget_results
      end

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

    end # class Mget
  end # module HiredisVip
end # module FFI

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffi-hiredis_vip-0.1.0.pre2 lib/ffi/hiredis_vip/mget.rb