Basic usage of the redis-cli for the supported data structures.
redis-cli
127.0.0.1:6379> SET a 1
OK
127.0.0.1:6379> GET a
"1"
127.0.0.1:6379> INCR a
(integer) 2
127.0.0.1:6379> DECR a
(integer) 1
127.0.0.1:6379> EXISTS a
(integer) 1
127.0.0.1:6379> EXISTS b
(integer) 0
127.0.0.1:6379> DEL a
(integer) 1
127.0.0.1:6379> FLUSHALL
OK
We can set expiration time to key values to not keep the values as permanent and also check how much time we have till the expiration (ttl will return -2 if expiration has passed and -1 if there is not timer set-persisted)
127.0.0.1:6379> TTL food
(integer) -1
127.0.0.1:6379> EXPIRE food 30
(integer) 1
127.0.0.1:6379> TTL food
(integer) 24
127.0.0.1:6379> SETEX foo 10 bar
OK
127.0.0.1:6379> SET food pizza
OK
127.0.0.1:6379> RENAME food foods
OK
127.0.0.1:6379> GET food
(nil)
127.0.0.1:6379> GET foods
"pizza"
Iterates through a set of keys that match a specific pattern Not viable for production environments because it returns everything at once
127.0.0.1:6379> KEYS *
1) "foods"
Iterates through a set of keys based on the index and a counter (offset).
127.0.0.1:6379> SCAN 0 COUNT 4
1) "5"
2) 1) "e"
2) "b"
3) "a"
4) "foods"
127.0.0.1:6379> SCAN 5 COUNT 4
1) "0"
2) 1) "f"
2) "c"
3) "d"
Can be used stand alone or by getting any specified parameter:
server | clients | memory | persistence | stats | replication | cpu | commandstats | cluster | all | default
127.0.0.1:6379> INFO clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
127.0.0.1:6379> SET "super pizza" "panepistimiou 32"
OK
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32"
127.0.0.1:6379> MSET "best burger" "lampraki 3" "second best burger" "athinwn 12" "sushi" "somewhere 12"
OK
127.0.0.1:6379> MGET "best burger" "sushi" "whatever"
1) "lampraki 3"
2) "somewhere 12"
3) (nil)
127.0.0.1:6379> STRLEN "super pizza"
(integer) 16
127.0.0.1:6379> SET "yummy pizza" "stadiou 2" NX
OK
127.0.0.1:6379> SET "yummy pizza" "stadiou 2" NX
(nil)
127.0.0.1:6379> SETNX "yummy pizza" "stadiou 2"
(integer) 0
127.0.0.1:6379> APPEND "super pizza" " 10010"
(integer) 22
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32 10010"
127.0.0.1:6379> SETRANGE "super pizza" 16 " Athens "
(integer) 24
127.0.0.1:6379> GET "super pizza"
"panepistimiou 32 Athens "
127.0.0.1:6379> GETRANGE "super pizza" 0 12
"panepistimiou"
Redis uses three different encodings to store string objects and will decide the encoding automatically per string value:
- int: For strings representing 64-bit signed integers
- embstr: For strings whose length is less or equal to 44 bytes this type of encoding is more efficient in memory usage and performance
- raw: For strings whose length is greater than 44 bytes
127.0.0.1:6379> SET myKey 12345
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"int"
127.0.0.1:6379> SET myKey "a string"
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"embstr"
127.0.0.1:6379> SET myKey "a long string whose length is more than 39 bytes"
OK
127.0.0.1:6379> OBJECT ENCODING myKey
"raw"
127.0.0.1:6379> SET a 10
OK
127.0.0.1:6379> GET a
"10"
127.0.0.1:6379> INCRBY a 30
(integer) 40
127.0.0.1:6379> INCR a
(integer) 41
127.0.0.1:6379> DECR a
(integer) 40
127.0.0.1:6379> DECRBY a 35
(integer) 5
127.0.0.1:6379> LPUSH fav_foods pizza burger fruits
(integer) 3
127.0.0.1:6379> RPUSH fav_foods spaghetti steak potatoes
(integer) 6
Notice the order of the items based on the previous insertion commands.
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "fruits"
2) "burger"
3) "pizza"
4) "spaghetti"
5) "steak"
6) "potatoes"
127.0.0.1:6379> LINSERT fav_foods BEFORE burger salad
(integer) 7
127.0.0.1:6379> LINSERT fav_foods AFTER burger "ice cream"
(integer) 8
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "fruits"
2) "salad"
3) "burger"
4) "ice cream"
5) "pizza"
6) "spaghetti"
7) "steak"
8) "potatoes"
127.0.0.1:6379> LINDEX fav_foods 0
"fruits"
127.0.0.1:6379> LINDEX fav_foods -2
"steak
127.0.0.1:6379> LPUSHX ad "value"
(integer) 0
127.0.0.1:6379> LPUSHX fav_foods lasanga
(integer) 9
Removes the first element from the left or right end of the list, and returns its value. Executing LPOP or RPOP on a non_existent key will return (nil)
127.0.0.1:6379> LPOP fav_foods
"lasanga"
127.0.0.1:6379> LPOP fav_foods2
(nil)
Used to remove multiple elements in a list while only keeping the range of the list specified by start and end indexes
127.0.0.1:6379> LTRIM fav_foods 3 -1
OK
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "ice cream"
2) "pizza"
3) "spaghetti"
4) "steak"
5) "potatoes"
127.0.0.1:6379> LSET fav_foods 0 salad
OK
127.0.0.1:6379> LRANGE fav_foods 0 -1
1) "salad"
2) "pizza"
3) "spaghetti"
4) "steak"
5) "potatoes"
127.0.0.1:6379> HSET some_pizza address "some random address 122"
(integer) 1
127.0.0.1:6379> HGET some_pizza address
"some random address 122"
127.0.0.1:6379> HGET some_pizza menu
(nil
127.0.0.1:6379> HMSET "dream pizza" "address" "stadiou 34" "phone" "2107777777" "rating" "4.5"
OK
127.0.0.1:6379> HMGET "dream pizza" "address" "phone" "rating"
1) "stadiou 34"
2) "2107777777"
3) "4.5"
127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "4.5"
127.0.0.1:6379> HSET "dream pizza" rating 3.2
(integer) 0
127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "3.2"
127.0.0.1:6379> HSETNX "dream pizza" "test5" "test3"
(integer) 1
127.0.0.1:6379> HGETALL "dream pizza"
1) "address"
2) "stadiou 34"
3) "phone"
4) "2107777777"
5) "rating"
6) "3.2"
7) "test5"
8) "test3"
127.0.0.1:6379> HSETNX "dream pizza" "test5" "test3"
(integer) 0
127.0.0.1:6379> SADD "pepperoni pizza" "spicy" "affordable" "great taste"
(integer) 3
127.0.0.1:6379> SISMEMBER "pepperoni pizza" "family"
(integer) 0
127.0.0.1:6379> SISMEMBER "pepperoni pizza" "great taste"
(integer) 1
127.0.0.1:6379> SREM "pepperoni pizza" "affordable" "spicy"
(integer) 2
127.0.0.1:6379> SCARD "pepperoni pizza"
(integer) 1
127.0.0.1:6379> SADD "pepperoni pizza" "spicy" "affordable" "great taste"
(integer) 2
127.0.0.1:6379> SMEMBERS "pepperoni pizza"
1) "affordable"
2) "spicy"
3) "great taste"
Redis provides a group of commands for union (SUNION, SUNIONSTORE), intersection (SINTER, SINTERSTORE) and difference (SDIFF, SDIFFSTORE) operations between sets
127.0.0.1:6379> SADD numbers 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD numbers2 3 4 5 6 7
(integer) 5
127.0.0.1:6379> SINTER numbers numbers2
1) "3"
2) "4"
3) "5"
127.0.0.1:6379> SINTERSTORE common numbers numbers2
(integer) 3
127.0.0.1:6379> SMEMBERS common
1) "3"
2) "4"
3) "5"
127.0.0.1:6379> SUNION numbers numbers2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
127.0.0.1:6379> SUNIONSTORE all numbers numbers2
(integer) 7
127.0.0.1:6379> SMEMBERS all
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
127.0.0.1:6379> SDIFF numbers numbers2 # missing from second set
1) "1"
2) "2"
127.0.0.1:6379> SDIFFSTORE insecond numbers numbers2
(integer) 2
127.0.0.1:6379> SMEMBERS insecond
1) "1"
2) "2"
127.0.0.1:6379> SADD food tomato potato
(integer) 2
127.0.0.1:6379> SADD shop bread
(integer) 1
127.0.0.1:6379> SMOVE food shop tomato
(integer) 1
127.0.0.1:6379> SMEMBERS food
1) "potato"
127.0.0.1:6379> SMEMBERS shop
1) "bread"
2) "tomato"
127.0.0.1:6379> SRANDMEMBER shop
"bread"
127.0.0.1:6379> SADD shop "towel" "meat" "fish"
(integer) 3
127.0.0.1:6379> SPOP shop
"meat"
127.0.0.1:6379> SPOP shop 2
1) "tomato"
2) "fish"
Sorted sets are similar to sets but every member has a “score”.
Score:
- Required
- Must be numeric (represented as float)
- Is not unique
127.0.0.1:6379> ZADD ranking:food 100 pizza 75 spagheti 99 burger 10 rice 55 tuna
(integer) 5
127.0.0.1:6379> ZREVRANGE ranking:food 0 -1
1) "pizza"
2) "burger"
3) "spagheti"
4) "tuna"
5) "rice"
127.0.0.1:6379> ZREVRANGE ranking:food 0 -1 WITHSCORES
1) "pizza"
2) "100"
3) "burger"
4) "99"
5) "spagheti"
6) "75"
7) "tuna"
8) "55"
9) "rice"
10) "10"
127.0.0.1:6379> ZINCRBY ranking:food 1 "rice"
"11"
127.0.0.1:6379> ZINCRBY ranking:food -2 "rice"
"9"
127.0.0.1:6379> ZREVRANK ranking:food pizza
(integer) 0
127.0.0.1:6379> ZREVRANK ranking:food burger
(integer) 1
127.0.0.1:6379> ZSCORE ranking:food burger
"99"
127.0.0.1:6379> ZADD ranking:drinks 100 beer 71 water 99 vodka 10 tequila 55 soda
(integer) 5
127.0.0.1:6379> ZADD ranking:food 100 pizza 75 spagheti 99 burger 10 rice 55 tuna
(integer) 5
127.0.0.1:6379> ZUNIONSTORE "total rankings" 2 ranking:drinks ranking:food
(integer) 10
127.0.0.1:6379> ZREVRANGE "total rankings" 0 -1 WITHSCORES
1) "pizza"
2) "100"
3) "beer"
4) "100"
5) "vodka"
6) "99"
7) "burger"
8) "99"
9) "spagheti"
10) "75"
11) "water"
12) "71"
13) "tuna"
14) "55"
15) "soda"
16) "55"
17) "tequila"
18) "10"
19) "rice"
20) "9"
127.0.0.1:6379> ZADD some 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD some2 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZUNIONSTORE "some3" 2 some some2
(integer) 3
127.0.0.1:6379> ZREVRANGE "some3" 0 -1 WITHSCORES
1) "c"
2) "6"
3) "b"
4) "4"
5) "a"
6) "2"
127.0.0.1:6379> ZADD test 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> ZADD test2 1 a 2 d 3 e
(integer) 3
127.0.0.1:6379> ZINTERSTORE "some3" 2 test test2
(integer) 1
127.0.0.1:6379> ZREVRANGE "some3" 0 -1 WITHSCORES
1) "a"
2) "2"
127.0.0.1:6379> ZSCAN test 0
1) "0"
2) 1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> GEOADD landmarks 37.970833 23.726110 acropolis 37.970833 23.72611 philopapou 37.6775239 23.937746 poseidon
(integer) 3
127.0.0.1:6379> GEOPOS landmarks acropolis
1) 1) "37.97083407640457153"
2) "23.7261091308044314"
When the coordinates are set via GEOADD, they will be internally converted into a 52-bit GEOHASH, which is a Geo-encoding system that is commonly accepted. You should take into consideration that there is a slight difference between the Geo stored and the coordinates returned by GEOPOS, so you should not expect the two to be exactly the same.
127.0.0.1:6379> GEODIST landmarks philopapou poseidon
"38008.7042"
The Geo set is actually stored as a sorted set (zset in Redis) and all the commands in a sorted set can be applied to the Geo data type. For example, removing a Geo index from a Geo set can be done by ZREM, and retrieving all the members of a Geo set can be achieved by ZRANGE.
127.0.0.1:6379> ZRANGE landmarks 0 -1
1) "acropolis"
2) "philopapou"
3) "poseidon"
Counting distinct values is a common task in various kinds of daily data processing scenarios. In Redis, while it's fine and sometimes necessary to implement distinct counting using set, memory consumption and performance degradation should be taken into consideration when the size of the set increases to tens of millions
127.0.0.1:6379> PFADD "visit:super pizza:20200101" "1" "2" "3" "4"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200102" "1" "5" "6" "7"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200103" "5" "2" "12" "31"
(integer) 1
127.0.0.1:6379> PFADD "visit:super pizza:20200104" "1" "122" "321" "432"
(integer) 1
127.0.0.1:6379> PFMERGE "visit:super pizza:firstweek" "visit:super pizza:20200101" "visit:super pizza:20200102" "visit:super pizza:20200103" "visit:super pizza:20200104"
OK
127.0.0.1:6379> PFCOUNT "visit:super pizza:firstweek"
(integer) 12
127.0.0.1:6379>