Method: Redis::Commands::Streams#xadd

Defined in:
lib/redis/commands/streams.rb

#xadd(key, entry, approximate: nil, maxlen: nil, minid: nil, nomkstream: nil, id: '*') ⇒ String

Add new entry to the stream.

Examples:

Without options

redis.xadd('mystream', f1: 'v1', f2: 'v2')

With options

redis.xadd('mystream', { f1: 'v1', f2: 'v2' }, id: '0-0', maxlen: 1000, approximate: true, nomkstream: true)

Parameters:

  • key (String)

    the stream key

  • entry (Hash)

    one or multiple field-value pairs

  • opts (Hash)

    several options for ‘XADD` command

Returns:

  • (String)

    the entry id



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/redis/commands/streams.rb', line 50

def xadd(key, entry, approximate: nil, maxlen: nil, minid: nil, nomkstream: nil, id: '*')
  args = [:xadd, key]
  args << 'NOMKSTREAM' if nomkstream
  if maxlen
    raise ArgumentError, "can't supply both maxlen and minid" if minid

    args << "MAXLEN"
    args << "~" if approximate
    args << maxlen
  elsif minid
    args << "MINID"
    args << "~" if approximate
    args << minid
  end
  args << id
  args.concat(entry.flatten)
  send_command(args)
end