The XADD
command in Redis is used with the Streams data type. It appends new entries to a stream. Common use cases include:
Here's a basic usage of the XADD
command using Ruby:
require 'redis' redis = Redis.new # Add an entry to the stream 'mystream' redis.xadd('mystream', '*', 'key1', 'value1', 'key2', 'value2')
In this example, we're adding an entry to the stream named 'mystream'. The '*' generates an ID based on the current timestamp. 'key1' and 'key2' represent field names and 'value1' and 'value2' are their respective values.
To add multiple messages at once:
require 'redis' redis = Redis.new # Add multiple entries to the stream 'mystream' fields_values = ['key1', 'value1', 'key2', 'value2'] fields_values2 = ['key3', 'value3', 'key4', 'value4'] redis.xadd('mystream', '*', *fields_values) redis.xadd('mystream', '*', *fields_values2)
This will add two separate entries to 'mystream', each with their own unique IDs and key-value pairs.
Q: Can I use XADD
with other data types?
A: No, XADD
is specifically designed for use with Redis Streams.
Q: What happens if I try to add an entry to a non-existent stream? A: Redis will automatically create the stream.
Q: How many fields can I add to a stream entry? A: There's no hard limit, but remember that each additional field consumes more memory.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.