instructions

My original – > finch: www.yuque.com/docs/share/…

All commands of Redis are available in redis official documents. If English is not good, you can check the Chinese official website of Redis, but the access may be slow

Redis commands refer to this site, which categorizes and summarizes redis related commands for us, as well as code examples, which are very friendly for beginners

In this article, I’ve summarized only a few common commands. For more commands, please refer to the two sites mentioned above

Note: The redis command is case insensitive

If you find this article helpful, please follow, like, comment and forward

Redis data type

Five basic data types

  • String
  • List
  • Set
  • Hash
  • Zset

Three special data types

  • geospatial
  • hyperloglog
  • bitmaps

Each Data type in Redis has its special functions. These Data types are detailed in the official document Data Types to meet daily needs. Next, we will learn these Data types while learning commands, because each Data type has its corresponding command

Redis database

Redis has 16 databases by default. The concept of a database is similar to that of mysql. The number of databases can be changed in the configuration fileAs stated clearly in the comments, the default database is database 0, which can be switched using the select command

Since Redis does not support custom database names, each database is named by a number. Developers need to keep their own records of how the stored data corresponds to the database.

Database and keys commands

select

Switch database, default is database 0

127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> SELECT 15 OK 127.0.0.1:6379[15]> select 16 Error: ERR DB index is out of rangeCopy the code

Mysql > alter database 127.0.0.1:6379 mysql > alter database 127.0.0.1:6379 mysql > alter database 127.0.0.1:6379

dbsize

Check the number of keys in the current database

127.0.0.1:6379> dbsize # No value (integer) 0 127.0.0.1:6379> set hello world OK 127.0.0.1:6379> set nihao shijie # insert key OK 127.0.0.1:6379> dbsize # insert key (integer) 2Copy the code

keys

Check the current database key, traversing the entire database, if the number of keys will block for a while, because Redis is single-threaded

127.0.0.1:6379> set hello world OK 127.0.0.1:6379> set nihao shijie OK 127.0.0.1:6379> keys * # Check all keys 1) "nihao" 2) "Hello" 127.0.0.1:6379> keys ni* #Copy the code

scan

The function of the keys command is the same as that of the keys command, except that scan can limit the scan scope

127.0.0.1:6379> keys * 1) "girl2" 2) "boy3" 3) "boy1" 4) "boy2" 5) "girl1" 6) "girl3" 127.0.0.1:6379> scan 0 # 0 Girl2 = girl2 = girl2 = girl2 = girl2 = girl2 Girl2) "girl1" 3) "boy3" 4) "girl3" 5) "boy1" 6) "boy2" 127.0.0.1:6379> Scan 0 match girl* count 4 # 2) 1) "girl2" 2) "girl1" 3) "girl3"Copy the code

The following commands are all similar, except where to scan:

  • The SCAN command is used to iterate over the database keys in the current database.

  • The SSCAN command is used to iterate over elements in a collection key.

  • The HSCAN command is used to iterate over the key-value pairs in a hash key.

  • The ZSCAN command is used to iterate over elements (including element members and element scores) in an ordered collection.

del

Delete one or more keys

127.0.0.1:6379> set a 1
OK

127.0.0.1:6379> set b 2
OK

127.0.0.1:6379> set c 3
OK

127.0.0.1:6379> del a b c
(integer) 3

127.0.0.1:6379> keys *
(empty list or set)
Copy the code

flushdb

Delete all keys in the current database

127.0.0.1:6379> keys *
1) "nihao"
2) "hello"

127.0.0.1:6379> flushdb
OK

127.0.0.1:6379> keys *
(empty list or set)
Copy the code

flushall

Delete keys from all databases

127.0.0.1:6379> set hello world OK 127.0.0.1:6379> keys * 1) "hello" 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> set Nihao shijie OK 127.0.0.1:6379[1]> keys * (empty) "nihao" 127.0.0.1:6379[1]> flushall OK 127.0.0.1:6379[1]> keys * (empty List or set) 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> keys * (empty list or set)Copy the code

exists

Check whether the key exists

127.0.0.1:6379> exists hello
(integer) 0

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> exists hello
(integer) 1
Copy the code

move

Move a key to another database

127.0.0.1:6379> keys *
1) "hello"

127.0.0.1:6379> move hello 1
(integer) 1

127.0.0.1:6379> keys *
(empty list or set)

127.0.0.1:6379> select 1
OK

127.0.0.1:6379[1]> keys *
1) "hello"
Copy the code

expire

Set the expiration time of the key

127.0.0.1:6379> set hello world OK 127.0.0.1:6379> keys * 1) "hello" 127.0.0.1:6379> expire hello 10 (integer) 1 = = = = = = = = = = = = 10 seconds after the = = = = = = = = = = = = = = 127.0.0.1:6379 > keys * (the empty list or set)Copy the code

ttl

Check the remaining time of the key

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> ttl hello
(integer) -1

127.0.0.1:6379> expire hello 10
(integer) 1

127.0.0.1:6379> ttl hello
(integer) 6
Copy the code
  • The remaining time of the key without the expiration time is -1

type

Check the data type of the key

127.0.0.1:6379> set Hello world OK 127.0.0.1:6379> type Hello String 127.0.0.1:6379> lpush aaa BBB # list (INTEGER) 1 127.0.0.1:6379> type AAA listCopy the code

String (String)

The official instructions

The String data type is the most commonly used data type in our development. It is binary safe, which means it can hold any type of data, such as a JPEG image or a serialized Ruby object

The maximum value of a String is 512MB

You can do a lot of interesting things with String in Redis, for example

  • Use String as an atomic-type counter through the INCR family of commands: INCR, DECR, INCRBY
  • Append the string to value using the append command
  • Use String as a randomly accessed vector through the getRange and setRange commands
  • Encode a lot of data in a small space, or use getBit and setBit to create a Redis-enabled Bloom filter.

set

Set a key/value of type string

127.0.0.1:6379> set hello world
OK
Copy the code

get

Gets the value of the String key

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> get hello
"world"
Copy the code

getset

Gets the old value and updates the new value

127.0.0.1:6379> set num 1
OK

127.0.0.1:6379> getset num 2
"1"

127.0.0.1:6379> get num
"2"
Copy the code

strlen

Check the string length of the value corresponding to the key

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> strlen hello
(integer) 5
Copy the code

append

Appends a string to value

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> append hello 123
(integer) 8

127.0.0.1:6379> get hello
"world123"
Copy the code

incr

Increments the value of value by one and returns the final value. Unlike i++ in Java, incr in redis is an atomic operation

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> incr hello
(error) ERR value is not an integer or out of range

127.0.0.1:6379> set number 1
OK

127.0.0.1:6379> incr number
(integer) 2

127.0.0.1:6379> get number
"2"
Copy the code

Note: The incr command can be used only when value is integer

decr

Subtracting value by one and returning the final value is also an atomic operation

127.0.0.1:6379> set number 1
OK

127.0.0.1:6379> decr number
(integer) 0
Copy the code

incrby

Adds a specified value to value and returns the final value

127.0.0.1:6379> set number 1
OK

127.0.0.1:6379> incrby number 4
(integer) 5
Copy the code

decrby

Subtract a specified value from value and return the final value

127.0.0.1:6379> set number 1
OK

127.0.0.1:6379> decrby number 20
(integer) -19
Copy the code

getRange

Gets the specified portion of the string that key stores

127.0.0.1:6379> set num abcdefg OK 127.0.0.1:6379> getrange num 1 3 # Obtain the first to third characters "BCD" 127.0.0.1:6379> getrange num 1 127.0.0.1:6379> getrange num 0 -1Copy the code

Notice that the string starts at bit 0

setRange

Inserts a string after the specified position in the string stored in key

127.0.0.1:6379> set num abcdefg
OK

127.0.0.1:6379> setrange num 1 123
(integer) 7

127.0.0.1:6379> get num
"a123efg"
Copy the code

setex

Set with expire, sets the key value to value and sets the expiration time

127.0.0.1:6379> setex redis 10 123 # Set the key value to 123 and the expiration time to 10s OK 127.0.0.1:6379> TTL redis (integer) 6Copy the code

setnx

Set no exist, if the key does not exist, create a key/value. If the key exists, it is not created. Returns 1 on success or 0 on failure

127.0.0.1:6379> setnx nihao Shijie (integer) 1 127.0.0.1:6379> keys * 1) "nihao" 127.0.0.1:6379> setnx nihao redis (INTEGER) 0 127.0.0.1:6379> Get nihaoCopy the code

mset

Create keys and values in batches

127.0.0.1:6379> mset a 1 b 2 c 3
OK

127.0.0.1:6379> keys *
1) "a"
2) "c"
3) "b"
Copy the code

msetnx

Create keys/values in batches. If there are no identical keys, this is an atomic operation, and both keys will succeed or fail

127.0.0.1:6379> mset a 1 b 2 c 3 OK 127.0.0.1:6379> msetnx d 4 e 5 (INTEGER) 1 127.0.0.1:6379> keys * 1) "d" 2) "b" 3) "a" 4) "c" 5) "e" 127.0.0.1:6379> msetnx e 55 f 6 g 7 # (INTEGER) 0 127.0.0.1:6379> keys * 1) "d" 2) "b" 3) "a" 4) "C" 5) "e"Copy the code

rename

Rename existing keys

127.0.0.1:6379> set hello world
OK

127.0.0.1:6379> keys *
1) "hello"

127.0.0.1:6379> rename hello newkey
OK

127.0.0.1:6379> keys *
1) "newkey"
Copy the code

List (linked List)

The official instructions

The Redis list data type is a linked list of strings sorted by the order in which the elements are inserted. Redis List supports inserting elements to the top and bottom of lists

Lpush inserts elements to the head of the list and rpush inserts elements to the tail of the list. When either of these commands is executed in an empty key, a new list is created. When the key is empty after the list-related command is executed, it is removed from the database. These are very understandable semantics, because executing a list command on a key that is an empty list is the same as executing a list command on a key that does not exist

Here are some list commands and the results of their execution

lpush mylist a   # now the list is "a"
lpush mylist b   # now the list is "b","a"
rpush mylist c   # now the list is "b","a","c"
Copy the code

Lists can have up to 2^32-1 elements (4294967295, each list can have more than 4 billion elements)

From a time complexity perspective, the main feature of the Redis list is the ability to insert and delete elements near the top and bottom of the list for a duration, even if there are millions of inserted items. Accessing elements at both ends of a list is fast, but accessing the middle of a long list is slow because it’s an O(n) algorithm

You can do a lot of things with Redis List, for example

  • Modeling the timeline in the socket network, using lpush command to add a new element in the user’s timeline, using lrange to retrieve some recently inserted elements
  • You can use the lpush and ltrim commands to create a list that never exceeds a given number of elements, but only remembers the last N elements
  • Lists can be used for messaging, such as the famous Resque Ruby library to create background jobs
  • You can do a lot of things with list. The data type supports many commands, including blocking commands like BLPOP

lpush

Inserts one or more elements from the head of the list. The elements in the list can be repeated

127.0.0.1:6379> lpush myList a (integer) 1 127.0.0.1:6379> lpush myList b (integer) 2 127.0.0.1:6379> lpush myList C d e  f (integer) 6Copy the code

lrange

View the element at the specified position in the list, starting at bit 0

127.0.0.1:6379> lrange myList 0 -1 # Query all elements in the list 1) "f" 2) "e" 3) "d" 4) "c" 5) "b" 6) "a" 127.0.0.1:6379> lrange myList 1 1) "e" 2) "d" 3) "c"Copy the code

lpushx

Insert elements into the head of the list while it exists, otherwise nothing is done, okay

127.0.0.1:6379> lPUSHx myList G (integer) 7 127.0.0.1:6379> lrange myList 0-1 1) "g" 2) "f" 3) "e" 4) "d" 5) "c" 6) "b" 7) "a" 127.0.0.1:6379> lpushx list aaa # select * from list (integer) 0 127.0.0.1:6379> keys * 1Copy the code

rpush

Insert an element to the end of the list

127.0.0.1:6379> rpush myList 1 (integer) 8 127.0.0.1:6379> lrange MyList 0-1 1) "g" 2) "f" 3) "e" 4) "D" 5) "c" 6) "b" 7) "a" 8) "1" 127.0.0.1:6379> rpush myList 2 3 (integer) 10 127.0.0.1:6379> lrange myList 0-1 1) "g" 2) "f" 3) "e" 4) "d" 5) "c" 6) "b" 7) "a" 8) "1" 9) "2" 10) "3"Copy the code

rpushx

Similar to lpushx, insert a value to the end of a list where the key exists, and then do nothing if the key does not exist

127.0.0.1:6379> rpushx myList 4 (integer) 11 127.0.0.1:6379> lrange myList 0-1 # myList exists, Insert data to the list the tail (1) "g" (2) "f" 3) "e" 4) "c" "d" 5) 6) "b" 7) "a" 8) "1" 9) 10) "2", "3" 11) "4" 127.0.0.1:6379 > rpushx list 1 # (integer) 0 127.0.0.1:6379> keys * 1) "myList"Copy the code

lpop

Pop up an element from the head of the list, and the number of list elements decreases by one

127.0.0.1:6379> lrange myList 0-1 1) "4" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> lpop MyList "4" 127.0.0.1:6379> lrange mylist 0 -1 1) "3" 2) "2" 3) "1"Copy the code

rpop

An element pops up from the end of the list and the number of list elements decreases by one

127.0.0.1:6379> lrange myList 0 -1 1) "3" 2) "2" 3) "1" 127.0.0.1:6379> rpop myList "1" 127.0.0.1:6379> lrange myList 0 1) "2" 2) "2"Copy the code

rpoplpush

Popup elements from the end of the list to add to the head of the new list

127.0.0.1:6379> lrange myList 0-1 1) "3" 2) "2" 127.0.0.1:6379> rpoplpush myList list "2" 127.0.0.1:6379> lrange myList 0-1 1) "3" 127.0.0.1:6379> lrange list 0-1 1) "2"Copy the code

lrem

Deletes the specified element from the linked list

127.0.0.1:6379> lrange myList 0 -1 1) "3" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> lrem myList 2 3 (integer) 2 127.0.0.1:6379> lrange myList 0-1 1) "2" 2) "1" 127.0.0.1:6379> lrem myList 12 1 127.0.0.1:6379> lrange myList 0 -1 1) "1"Copy the code

llen

Check the number of list elements

127.0.0.1:6379> lrange MyList 0-1 1) "4" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> llen MyList (integer) 4Copy the code

lindex

Gets the element at the specified index, starting at 0

127.0.0.1:6379> lrange myList 0-1 1) "4" 2) "3" 3) "2" 4) "1" 127.0.0.1:6379> lindex myList 2" 2" 127.0.0.1:6379> lindex mylist 1 "3"Copy the code

linsert

Inserts elements before or after the specified element

127.0.0.1:6379> lrange myList 0-1 1) "d" 2) "c" 3) "b" 4) "a" 127.0.0.1:6379> linsert myList before a 1 # insert after the specified value (INTEGER) 5 127.0.0.1:6379> lrange myList 0-1 1) "D" 2) "C" 3) "b" 4) "1" 5) "a" 127.0.0.1:6379> linsert myList after c 2 # Insert (integer) 6 127.0.0.1:6379> lrange MyList 0-1 1) "d" 2) "c" 3) "2" 4) "b" 5) "1" 6) "a"Copy the code

lset

Update the value of list at the specified index location

127.0.0.1:6379> lrange myList 0-1 1) "d" 2) "c" 3) "2" 4) "b" 5) "1" 6) "a" 127.0.0.1:6379> lset myList 3 1.5 OK 127.0.0.1:6379 > lrange mylist 0 and 1, 1) "c" "d" 2) 3) 4) "1.5" 5 "2"), "1" 6) "a"Copy the code

ltrim

Truncate the value of the specified index interval. Unlike lrange, ltrim removes other elements from the list.

127.0.0.1:6379> lrange myList 0 -1 1) "d" 2) "c" 3) "2" 4) "1.5" 5) "1" 6) "a" 127.0.0.1:6379> ltrim myList 2 4 OK 127.0.0.1:6379> lrange myList 0-1 1) "2" 2) "1.5" 3) "1" 127.0.0.1:6379> ltrim myList 0-1 127.0.0.1:6379> lrange myList 0 -1 1) "2" 2) "1.5" 3) "1"Copy the code

blpop

Eject an element from the head of the list, or block until the wait times out or until eject elements are found. This command can pass in multiple keys, and eject the first key with an element in the table in front of the back order. The return value of the first key with an element in the table is the eject element’s key and the eject value

127.0.0.1:6379> lrange myList 0 -1 1) "2" 2) "1.5" 3) "1" 127.0.0.1:6379> blPOP myList 10 Maximum 10 seconds 1) "myList "2) "2"Copy the code
127.0.0.1:6379> lrange myList 0-1 1) "1.5" 2) "1" # hello is empty linked list 127.0.0.1:6379> blPOP Hello myList list 10 # 1) "myList" 2) "1.5"Copy the code
127.0.0.1:6379> blPOP Hello 10 # No element, block 10s (nil) (10.08s)Copy the code

brpop

Similar to the blPOP element, if there is an element in the list, the pop-up is the element at the end of the list

127.0.0.1:6379> lrange myList 0-1 1) "2" 2) "1.5" 3) "1" 127.0.0.1:6379> brPOP myList 10 Max. 10 seconds 1) "myList" 2) "1"Copy the code
127.0.0.1:6379> lrange myList 0-1 1) "2" 2) "1.5" # hello is empty list 127.0.0.1:6379> brpop hello myList list 10 # 1) "myList" 2) "1.5"Copy the code
127.0.0.1:6379> brpop Hello 10 # No element, block 10s (nil) (10.08s)Copy the code

brpoplpush

A pop-up element from the end of one list is inserted into the head of another list, or blocked if there is no data at the end

127.0.0.1:6379> lrange myList 0-1 1) "1" 2) "2" 127.0.0.1:6379> brpoplpush myList hello 10 "2" 127.0.0.1:6379> lrange Mylist 0-1 1) "1" 127.0.0.1:6379> lrange hello 0-1 1) "2" 127.0.0.1:6379> brpoplpush AAA hello 10 (nil) (10.06s)Copy the code

Set

The official instructions

The Redis Set data type is an unordered collection of strings that can add, remove, and test for the presence of elements with O(1) time complexity (constant no matter how many elements are in the collection)

The Redis Set data type has the property that the element is not duplicated, and the result of adding the same element multiple times is that only one element exists in the collection. In practice, this means that you don’t need to check to see if an element exists before you add it

One very interesting thing about Redis collections is that it supports many server commands to compute collections from existing collections, so you can do union sets, intersection sets, and different sets in a very short time

The maximum number of elements that can be stored in a collection is 2^ 32-1 (4294967295, over 4 billion elements per collection)

You can do a lot of interesting things using Redis collections, such as:

  • You can use Redis collections to track unique things. Want to know all the individual ips that visit a blog post? Simply use the sadd command every time you process a web page, and you can be sure that duplicate IP addresses will not be added

  • Redis sets are a good representation of relationships. You can use Redis to create a tag system where a collection represents a tag. You can then use the sadd command to add all the ids of all the tagged objects to a collection to represent this particular tag. Do you want to get all the ids of all the objects that have three different labels at the same time? Use the sinter command

  • You can use the collection’s spop and srandmember commands to randomly fetch elements

sadd

Adds one or more new elements to the collection

127.0.0.1:6379> sadd myset a
(integer) 1

127.0.0.1:6379> sadd myset b c
(integer) 2

127.0.0.1:6379> sadd myset c
(integer) 0

127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
Copy the code

smembers

Gets all elements in the collection

127.0.0.1:6379> sadd myset a b c d
(integer) 4

127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
4) "d"
Copy the code

scard

Gets the number of elements in the collection

127.0.0.1:6379> sadd myset a b c d
(integer) 4

127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
4) "d"

127.0.0.1:6379> scard myset
(integer) 4
Copy the code

sdiff

Subtracting one set from another, subtracting the existing elements, doesn’t change the original set

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> sdiff set1 set2 1) "1" 127.0.0.1:6379> smembers set1 1) "1" 2) "2" 3) "3"Copy the code

sdiffstore

One set minus another set, the result is in a third set

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> sdiffStore set3 Set1 set2 (integer) 1 127.0.0.1:6379> smembers set3 1) "1"Copy the code

sinter

Find the intersection of two sets

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> sinter set1 set2 1) "2" 2) "3"Copy the code

sinterstore

Take the intersection of two sets and store it in a third set

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> SinterStore set4 Set4 (integer) 2 127.0.0.1:6379> smembers set4 1) "2" 2) "3"Copy the code

sunion

You take the union of two sets, you don’t change the original set

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> sUnion set1 set2 1) "1" 2) "2" 3) "3" 4) "4"Copy the code

sunionstore

Take the union of two sets and store it in the third set

127.0.0.1:6379> sadd set1 12 3 (integer) 3 127.0.0.1:6379> sadd set2 2 3 4 (integer) 3 127.0.0.1:6379> sunionStore set3 Set3 (integer) 4 127.0.0.1:6379> smembers set3 1) "1" 2) "2" 3) "3" 4) "4"Copy the code

sismember

To determine whether an element exists, return 1 for existence and 0 for nonexistence

127.0.0.1:6379> smember set1 1) "1" 2) "2" 3) "3" sismember set1 4 (integer) 0Copy the code

smove

Move an element from one set to another

127.0.0.1:6379> smembers set1 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> smembers set2 1) "2" 2) "3) "4" Smove set1 set2 1 (integer) 1 127.0.0.1:6379> smembers set2 1) "1" 2) "2" 3) "3" 4) "4" "2" 2) "3"Copy the code

spop

Random popup of one or more elements in the collection, return popup of n elements

127.0.0.1:6379> smembers set2 1) "1" 2) "2" 3) "3" 4) "4" 127.0.0.1:6379> spop set2 2) "4" 2) "2" smembers set2 1) "1" 2) "3"Copy the code

srandmember

Gets several elements of the collection at random

127.0.0.1:6379> smembers set1 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> srandMember set1 2 1) "1" 2) "1" 127.0.0.1:6379> smembers set1 1) "1" 2) "2" 3) "3"Copy the code

srem

To remove one or more values, unlike the return value of the SPOp command, sREM returns the number of values removed

127.0.0.1:6379> smembers set1 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> srem set1 12 (integer) 2 127.0.0.1:6379> smembers set1 1) "3"Copy the code

Hashes

The official instructions

The Redis Hash data type is a mapping between string attributes and string values, so it’s a perfect data type for representing objects (for example, a user object with attributes like name, surname, age, etc.)

HMSET user:1000 username antirez password P1pp0 age 34
HGETALL user:1000
HSET user:1000 password 12345
HGETALL user:1000
Copy the code

A hash with a small number of fields (a small number means around 100 fields) is stored in a way that takes up very little space, so you can store millions of objects in a small Redis instance

Although hashes are primarily used to represent objects, they can also store many elements, so you can use them for many other tasks as well

Each hash can store 2^32-1 key/value pairs (over 4 billion)

hset

Add a key-value pair to the hash

127.0.0.1:6379> hset user1 name zhangsan
(integer) 1

127.0.0.1:6379> hset user1 age 22
(integer) 1
Copy the code

hsetnx

If the specified field does not exist in the hash, add it, otherwise do nothing

127.0.0.1:6379> hsetnx apple color red
(integer) 1

127.0.0.1:6379> hsetnx apple color greed
(integer) 0

127.0.0.1:6379> hsetnx apple weight 12
(integer) 1
Copy the code

hget

Gets the value of a key map in the hash

127.0.0.1:6379> hset user1 name zhangsan
(integer) 1

127.0.0.1:6379> hset user1 age 22
(integer) 1

127.0.0.1:6379> hget user1 name
"zhangsan"

127.0.0.1:6379> hget user1 age
"22"
Copy the code

hmset

Add multiple key-value pairs to the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK
Copy the code

hmget

Gets the value of multiple key mappings in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy OK 127.0.0.1:6379> hmget user1 name zhangsan age sex 1) "zhangsan" 2) "12" 3) "boy"Copy the code

hgetall

Gets all the keys and values in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hgetall user1
1) "name"
2) "zhangsan"
3) "age"
4) "12"
5) "sex"
6) "boy"
Copy the code

hkeys

Gets all keys in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hkeys user1
1) "name"
2) "age"
3) "sex"
Copy the code

hvals

Gets all the values in the hash

127.0.0.1:6379> hmset user1 name Zhangsan age 12 sex boy OK 127.0.0.1:6379> hset user1 1) "zhangsan" 2) "12" 3) "boy"Copy the code

hdel

Delete one or more fields from the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hdel user1 age
(integer) 1

127.0.0.1:6379> hgetall user1
1) "name"
2) "zhangsan"
3) "sex"
4) "boy"
Copy the code

hexists

Determines whether a field exists

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hexists user1 name
(integer) 1

127.0.0.1:6379> hexists user1 location
(integer) 0
Copy the code

hlen

Gets the number of fields in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hlen user1
(integer) 3
Copy the code

hstrlen

Gets the length of the string value for the field map in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hstrlen user1 name
(integer) 8
Copy the code

hincrby

Adds the specified integer to the numeric value of the field mapping in the hash

127.0.0.1:6379> hmset user1 name zhangsan age 12 sex boy
OK

127.0.0.1:6379> hincrby user1 age 2
(integer) 14

127.0.0.1:6379> hget user1 age
"14"
Copy the code

hincrbyfloat

The numeric value mapped to the field in the hash plus the number of the specified floating point type

127.0.0.1:6379> hset user1 Chinese 89.5 (integer) 1 127.0.0.1:6379> hincrByFloat user1 Chinese 0.2 "89.7" 127.0.0.1:6379> hget user1 age # where age is an integer "14" 127.0.0.1:6379> hincrByFloat user2 age 2.5 # add a float or integer "2.5" 127.0.0.1:6379> hget user1 age "14"Copy the code

Note that if the original value is an integer, then adding a floating-point number is still an integer. Adding a floating-point number only applies to numbers whose original value was a floating-point number

Sorted Set

A Redis ordered collection is similar to a Redis collection in that it does not contain the same string. The difference is that each element in an ordered set has its own score, which is used to rank the elements in the ordered set from smallest to largest. Although the elements are unique, the score can be repeated

You can add, delete, or update elements to an ordered collection in a very fast manner (O(logn)). Because it sorted elements when insertion, rather than after sorting, you can use the score to quickly get the range of elements, access to an ordered set of the middle part of elements is also very fast, so you can put the ordered set as a small list without repeating elements, you can quickly access all of the elements you need: Order elements, quickly determine whether elements exist, fast access to intermediate elements

All in all, with ordered collections, you can do a lot of high performance tasks that are hard to replicate in many other types of databases

With ordered sets, you can:

  • Create a leaderboard in a large number of online games and update it with Zadd every time a new score is generated. You can use the Zrange command to easily get the highest scoring users. You can use the Zrank command to easily get the ranking of a given user name. Using the zrank and zrange commands together, you can get other users with the same score as the given user. Everything was fast

  • Ordered collections are often used to retrieve data stored in Redis. For example, if you have many hashes to represent users, you can use an ordered set whose age field is used as a score and whose user ID is used as a value to quickly retrieve users of a given age using the ZrangebyScore command

zadd

Adds one or more values with a score to an ordered collection

127.0.0.1:6379> zadd mysort 1 one
(integer) 1

127.0.0.1:6379> zadd mysort 2 two 3 three
(integer) 2
Copy the code

zrange

Gets the elements of the specified range in the collection

127.0.0.1:6379> zadd mysort 1 one (integer) 1 127.0.0.1:6379> zadd mysort 2 two 3 three (integer) 2 127.0.0.1:6379> 127.0.0.1:6379> zrange mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 0-1 # mysort 1) "Three" 127.0.0.1:6379> zrange mysort 0-1 withscores #Copy the code

zrevrange

Gets the elements of the specified range in the collection by sorting them in descending order

127.0.0.1:6379> zadd myset 0 a 1 b 2 c 3 d
(integer) 4

127.0.0.1:6379> zrevrange myset 0 2
1) "d"
2) "c"
3) "b"

127.0.0.1:6379> zrevrange myset 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
Copy the code

zrangebyscore

Gets elements based on the scoring range

127.0.0.1:6379> zadd mset 0 a 1 b 2 c 3 d
(integer) 4

127.0.0.1:6379> zrangebyscore mset 0 3
1) "a"
2) "b"
3) "c"
4) "d"

127.0.0.1:6379> zrangebyscore mset 0 2
1) "a"
2) "b"
3) "c"
Copy the code

zrevrangebyscore

After the elements are sorted in descending order, the elements are retrieved based on the scoring range

127.0.0.1:6379> zrange mset 0-1 withscores 1) "A" 2) "3" 3) "b" 4) "5" 5) "c" 6) "6" 7) "w" 8) "11" 127.0.0.1:6379> zrevrangebyscore mset 10 5 withscores 1) "c" 2) "6" 3) "b" 4) "5"Copy the code

zrangebylex

When many elements are rated the same, by default the elements are sorted lexicographically in ascending order, and the elements are retrieved according to the rules

127.0.0.1:6379> zadd myset 0 a 0 b 0 c (integer) 3 127.0.0.1:6379> zrangebylex myset - + # - + means positive infinity 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> zrangebylex myset [b + #] "b" 2) "c" 127.0.0.1:6379> zrangebylex Myset - (c #) "a" 2) "b"Copy the code

zrevrangebylex

When many elements are rated the same, using this command causes the elements to be sorted lexicographically in descending order, and then the elements are retrieved according to the rules

127.0.0.1:6379> zadd mySet 0 a 0 b 0 c 0 d (integer) 4 127.0.0.1:6379> zRevRangebylex myset + -1) "d" 2) "c" 3) "b" 4) "A" 127.0.0.1:6379> zRevRangebylex myset [c - 1] "c" 2) "b" 3) "a" 127.0.0.1:6379> zRevrangebylex myset + [b 1) "d" 2) "c" 3) "b"Copy the code

zlexcount

When many elements are rated the same, the default elements are sorted lexicographically in ascending order, and the number of elements is obtained in accordance with the rule

127.0.0.1:6379> zadd myset 0 a 0 b 0 C (integer) 3 127.0.0.1:6379 - + (integer) 3 127.0.0.1:6379> Zlexcount myset [b + (integer) 2 127.0.0.1:6379> zlexcount myset - (c (integer) 2Copy the code

The rules in this command are the same as those in Zrangebylex, and the usage is similar

zcard

Gets the number of elements in the collection

127.0.0.1:6379> zrange mysort 0-1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> zcard mysort (integer) 3Copy the code

zcount

Gets the number of elements in a given scoring range

127.0.0.1:6379> zrange mysort 0 -1 withscores
 1) "one"
 2) "1"
 3) "two"
 4) "2"
 5) "three"
 6) "3"
 7) "four"
 8) "4"
 9) "five"
10) "5"
11) "six"
12) "6"
13) "seven"
14) "7"
15) "eight"
16) "8"
17) "nine"
18) "9"
19) "ten"
20) "10"
127.0.0.1:6379> zcount mysort 2 5
(integer) 4
Copy the code

zscore

Gets the score for the specified element

127.0.0.1:6379> zrange mset 0-1 withscores 1) "A" 2) "3" 3) "b" 4) "5" 5) "c" 6) "6" 7) "w" 8) "11" 127.0.0.1:6379> zscore mset w "11"Copy the code

zrank

Gets the ranking of elements, starting at 0

127.0.0.1:6379> zadd mset 0 a 1 b 2 c 3 d
(integer) 4

127.0.0.1:6379> zrank mset b
(integer) 1

127.0.0.1:6379> zrank mset c
(integer) 2

127.0.0.1:6379> zrank mset a
(integer) 0
Copy the code

zrevrank

Gets the rank of the elements after they are sorted in descending order

127.0.0.1:6379> zrange mset 0-1 withscores 1) "A" 2) "3" 3) "b" 4) "5" 5) "c" 6) "6" 7) "w" 8) "11" 127.0.0.1:6379> Zrevrank mset W (integer) 0 127.0.0.1:6379> zRevRank mset C (integer) 1 127.0.0.1:6379> zRevRank mset B (integer) 2 127.0.0.1:6379> zRevRank mset a (integer) 3Copy the code

zincrby

Add an increment (which can be negative) to the score of the value

127.0.0.1:6379> zrange news 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 127.0.0.1:6379> zincrby news 1 one" 3" 127.0.0.1:6379> zrange news 0-1 1) "two" 2) "one" 127.0.0.1:6379> Zincrby news -2 one # can also add a negative "1" 127.0.0.1:6379> zrange news 0-1 withscores 1) "one" 2) "1" 3) "two" 4) "2"Copy the code

zinterstore

Take the intersection of multiple ordered sets, store the intersection in a new ordered set, and the score of the intersection defaults to the sum of the scores of the two. Note: This directive needs to specify the number of intersecting sets

127.0.0.1:6379> zadd zset1 1 A 2 b 3 C (integer) 3 127.0.0.1:6379> zadd zset2 3 C 4 d 5 E (integer) 3 127.0.0.1:6379> (integer) 1 127.0.0.1:6379> zrange zset3 0 -1 withscores 1) "c" 2) "6" # The grade is the sum of the grades of the two CsCopy the code

zpopmax

Pop up one or more elements with the highest rating

127.0.0.1:6379> zadd myset 0 a 1 b 2 c 3 d (integer) 4 127.0.0.1:6379> zpopmax myset 127.0.0.1:6379> zpopmax mySet 1) "c" 2) "2" 3) "b" 4) "1"Copy the code

zpopmin

Pop up one or more elements with the lowest rating

127.0.0.1:6379> zadd mset 0 A 1 b 2 c 3 d (integer) 4 127.0.0.1:6379> zpopmin mset 1) "A" 2) "0" 127.0.0.1:6379> zpopmin  mset 2 1) "b" 2) "1" 3) "c" 4) "2"Copy the code

zrem

Deletes the specified one or more elements

127.0.0.1:6379> zadd mset 0 A 1 b 2 C 3 d (integer) 4 127.0.0.1:6379> zrem mset A (integer) 1 127.0.0.1:6379> zrem mset B c (integer) 2 127.0.0.1:6379> zrange mset 0-1 1) "d"Copy the code

zremrangebylex

When many elements have the same score, delete the elements in the specified range according to the dictionary rules

127.0.0.1:6379> zadd mset 0 a 0 b 0 c 0 d (integer) 4 127.0.0.1:6379> zrange mset 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 127.0.0.1:6379> zremrangebylex mset [b (c (integer) 1 127.0.0.1:6379> zrange mset 0-1 1) "a" 2) "c" 3) "d" 127.0.0.1:6379> zremrangebylex mset - + # Delete all elements (integer) 3 127.0.0.1:6379> zrange mset 0-1 (empty list or set)Copy the code

The rules here are the same as in the Zrangebylex command

zrangebyrank

Remove elements based on the rank range, starting at 0

127.0.0.1:6379> zadd mset 0 a 1 b 2 c 3 d (integer) 4 127.0.0.1:6379> zrange mset 0 -1 1) "a" 2) "b" 3) "c" 4) "d" 127.0.0.1:6379> zremrangeByrank mset 12 (integer) 2 127.0.0.1:6379> zrange mset 0-1 1) "a" 2) "d"Copy the code

zrangebyscore

Delete according to score range

127.0.0.1:6379> zrange mset 0 -1 withscores 1) "A" 2) "3" 3) "b" 4) "6" 5) "c" 6) "9" 7) "w" 8) "11" 127.0.0.1:6379> (integer) 2 127.0.0.1:6379> zrange mset 0-1 withscores 1) "A" 2) "3" 3) "w" 4) the "11"Copy the code

zunionstore

Multiple sets do union, and put the result in other sets, there are the same elements and their scores add up

127.0.0.1:6379> zrange mset 0-1 withscores 1) "A" 2) "3" 3) "b" 4) "5" 5) "c" 6) "6" 7) "w" 8) "11" 127.0.0.1:6379> Zrange myset 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "1" 5) "c" 6) "2" 7) "d" 8) "3" 127.0.0.1:6379> zunionStore set1 2 Mset mySet (integer) 5 127.0.0.1:6379> zrange set1 0 -1 withscores 1) "A" 2) "3" 3) "d" 4) "3" 5) "b" 6) "6" 7) "7) "c" 8) "8" 9) "w" 10) "11"Copy the code

summary

Through arduous struggle, we finally passed all these basic commands. Although these commands can be found in video tutorials or official tutorials, they are only really useful when you type them yourself and truly understand the usefulness of each data type.