Skip to content

Instantly share code, notes, and snippets.

@antirez
Created September 4, 2017 14:33
Show Gist options
  • Save antirez/dd49bfd0b411c6eea11e05b3c419d747 to your computer and use it in GitHub Desktop.
Save antirez/dd49bfd0b411c6eea11e05b3c419d747 to your computer and use it in GitHub Desktop.
# Let's add a few entries in our stream "mystream". Every entry in a stream is like
# an hash, composed of fields and values, but every entry can have different fields.
# XADD appends a new entry in a stream, and returns an unique ID which is guaranteed
# be incrementing.
# The first part of the ID is the millisecond unixtime during the
# addition (or the time of the last entry in the stream, if greater than the current time).
# The second part of an entry is a 64 bit counter for entries added in the same ms.
127.0.0.1:6379> XADD mystream name pamela nicknake kill-9
1504535264879.0
127.0.0.1:6379> XADD mystream name salvatore nickname antirez age 40
1504535276439.0
127.0.0.1:6379> XADD mystream field1 A field2 B
1504535283624.0
# A foundamental operation in streams is range queries. Here we get all the elements
# inside the stream:
127.0.0.1:6379> XRANGE mystream - +
1) 1) 1504535264879.0
2) 1) "name"
2) "pamela"
3) "nicknake"
4) "kill-9"
2) 1) 1504535276439.0
2) 1) "name"
2) "salvatore"
3) "nickname"
4) "antirez"
5) "age"
6) "40"
3) 1) 1504535283624.0
2) 1) "field1"
2) "A"
3) "field2"
4) "B"
# We could also use a millisecond time to get a range starting at a given time.
# Anyway - and + are the minimum and maximum entry inside the stream.
127.0.0.1:6379> XRANGE mystream 1504535276450 +
1) 1) 1504535283624.0
2) 1) "field1"
2) "A"
3) "field2"
4) "B"
# Of course it is possible to get a given item and nothing more:
127.0.0.1:6379> XRANGE mystream 1504535276439.0 1504535276439.0
1) 1) 1504535276439.0
2) 1) "name"
2) "salvatore"
3) "nickname"
4) "antirez"
5) "age"
6) "40"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment