[Redis series] Redis commonly used data type command complete, 6K words bursting and sorting, not to be missed when passing by

Popular series:

Program life, exciting first look

[Distributed Series] Principles of Distributed Locks-Redisson, Zookeeper, Database

[Java Interview Series] Redis Interview Questions


1 Introduction

Recently, I often operate Redis, but some commands are somewhat vague. When you want to use it, you have to Baidu or flip notes. . In this article, I will sort out the most comprehensive redis commonly used commands ~~~~

High IQ has already been collected in the " one-click three-link " first, and novices are still declining~~~~


2. Text

2.1 Five data types of redis

Redis五种数据类型主要为:
\mathrm{​{\color{DarkRed} String}}

\mathrm{​{\color{DarkRed} Hash}}

\mathrm{​{\color{DarkRed} List}}

\mathrm{​{\color{DarkRed} Set}}

\mathrm{​{\color{DarkRed} SortedSet}}

2.2 Common commands

type of datacommandDescriptionExample
String typesetset key value value value:
set key-value

set testKey 123aaa

Return: OK

String type get get get key:
Get the value corresponding to the key

get testKey

Return: "123aaa"

String typeincrincr key value:
increment the corresponding value by 1

 incr testNum

Returns: (integer) 1

String typedecr decr key value:
decrement the corresponding value by 1
String typedeldel key value:
delete the specified key value

 del testNum
returns: (integer) 1

String typemset

mset key1 value1 key2 value2 ...:

Set key-value pairs in batches

mset keyStr1 213 keyStr2 aaa keyStr3 11eee
return: OK

String typemget

mget key1 key2 key3...:

Get the value corresponding to the specified key in batch

mget keyStr1 keyStr2 keyStr3

Returns: 1) "213"
           2) "aaa"
           3) "11eee"

String typesetnx

setnx key value value value:

Set the key-value, but when the set key does not exist, the setting can be successful; otherwise, the setting is invalid!

127.0.0.1:6379> setnx nxKey nxValue
returns: (integer) 1
127.0.0.1:6379> setnx nxKey 123
returns: (integer) 0
127.0.0.1:6379> get nxKey
returns: "nxValue"

hash typehset hset key fields values:
add content fileds-value to the key value in the hash

127.0.0.1:6379> hset mapKey field_name value_jack
returns: (integer) 1
127.0.0.1:6379> hset mapKey field_age value_25
returns: (integer) 1
127.0.0.1:6379> hset mapKey sex man
returns: (integer) 1

hash typehget

HGET key field:

Returns the value of the given field in the hash table key.

hexists mapKey field_age
returns: (integer) 1

hash typehkeys hkeys key value:
view all field values ​​under the key value

hkeys mapKey
1) "field_name"
2) "field_age"
3) "sex"

hash typehvals hvals key value:
view all value values ​​under the key value

hvals mapKey
1) "value_jack"
2) "value_25"
3) "man"

hash typehgetall hgetall key value:
View all field-value key-value pairs under the key value

 hgetall mapKey
1) "field_name"
2) "value_jack"
3) "field_age"
4) "value_25"
5) "sex"
6) "man"

hash typehdel hdel key value field value:
delete the field value under the specified key value

hdel mapKey sex
return: (integer) 1

hash typehexists

HEXISTS key field:

Check whether the given field field exists in the hash table key.

 hexists mapKey field_name
returns: (integer) 1

 hexists mapKey sex
returns: (integer) 0

hash typehmset

HMSET key field value [field value ...]:

Set multiple field-value (domain-value) pairs to the hash table key at the same time.

This command will overwrite the existing fields in the hash table.

If the key does not exist, an empty hash table is created and the hmset operation is performed.

 hmset mapKey habit sing company alibaba
returns: OK

hash typehmget

HMGET key field [field ...]:

Returns the value of one or more given fields in the hash table key.

If the given field does not exist in the hash table, then a nil value is returned.

hmget mapKey habit company girlfriend
1) "sing"
2) "alibaba"
3) (nil)

List typelpush 

LPUSH key value [value ...]:

Insert one or more values ​​into the header of the list key

If there are multiple value values, then each value value is inserted into the header of the table in order from left to right

lpush listKey 123 abc helloworld
returns: (integer) 3

List typerpush

RPUSH key value [value ...]:

Insert one or more values ​​at the end of the list key (far right).

If there are multiple value values, then each value value is inserted to the end of the table in order from left to right

rpush listKey1 111 222 333
returns: (integer) 3

List typelrange 

LRANGE key start stop:

Returns the elements in the specified interval in the list key, and the interval is specified by offsets start and stop.

The index parameters start and stop both have 0 as the base, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.

lrange listKey1 0 0
1) "111"

lrange listKey1 0 2
1) "111"
2) "222"
3) "333"

List typelindex

LINDEX key index:

Returns the element of the list key whose index is index.

The index parameters start and stop both have 0 as the base, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on.

lindex listKey1 1
returns: "222"

List typelset

LSET key index value:

Set the value of the element whose key index is index in the list to value.

When the index parameter is out of range, or an empty list (key does not exist) is LSET, an error is returned.

lset listKey1 1 444
returns: OK

List typelpop

LPOP key:

Remove and return the head element of the list key.

lpop listKey1
returns: "111"

List typerpop

RPOP key:

Remove and return the last element of the list key.

 rpop listKey1
returns: "333"

List typelrem

LREM key count value:

According to the value of the parameter count, remove the elements from the list that are equal to the parameter value.

The value of count can be one of the following:

  • count> 0: Search from the head of the table to the end of the table, remove the elements equal to value, and the number is count.
  • count <0: Search from the end of the table to the head of the table, remove the elements equal to value, and the number is the absolute value of count.
  • count = 0: Remove all values ​​equal to value in the table.

lrem newList 2 777
returns: (integer) 1

Set typesadd

SADD key member [member ...]:

Add one or more member elements to the set key, and the member elements that already exist in the set will be ignored.

If the key does not exist, create a collection that only contains member elements as members.

When the key is not a collection type, an error is returned.

 sadd setKey 123 345 456 123
Return: (integer) 3

Set typescard

SCARD key:

Returns the cardinality of the collection key (the number of elements in the collection).

scard setKey
returns: (integer) 3

Set typesmembers

SMEMBERS key

Returns all members in the set key.

A key that does not exist is considered an empty set.

 smembers setKey
1) "123"
2) "345"
3) "456"

Set typesdiff

SDIFF key [key ...]:

Returns all members of a set, which is the difference between all the given sets.

A key that does not exist is considered an empty set.

sdiff setKey newSet
1) "345"
2) "456"

Set typesdiffstore

SDIFFSTORE destination key [key ...]:

The effect of this command is similar to SDIFF, but it saves the result to the destination set instead of simply returning the result set.

If the destination collection already exists, it will be overwritten.

The destination can be the key itself.

Set typesinter

SINTER key [key ...]:

Returns all members of a set that is the intersection of all given sets.

A key that does not exist is considered an empty set.

sinter setKey tempSet
returns: 1) "123"

Set typesunion

SUNION key [key ...]:

Returns all members of a set that is the union of all given sets.

A key that does not exist is considered an empty set.

sunion setKey tempSet
1) "haha"
2) "345"
3) "123"
4) "456"
5) "abc"

Set typesrandmember

SRANDMEMBER key [count]:

If only the key parameter is provided when the command is executed, then a random element in the set is returned.

Starting from Redis 2.6, the SRANDMEMBER command accepts an optional count parameter:

  • If count is positive and less than the base of the collection, the command returns an array containing count elements, and the elements in the array are different . If count is greater than or equal to the base of the collection, then the entire collection is returned.
  • If count is negative, the command returns an array, the elements in the array may repeat multiple times , and the length of the array is the absolute value of count.

srandmember tempSet
returns: "abc"

srandmember tempSet
returns: "123"

Set typesrem

SREM key member [member ...]:

Remove one or more member elements from the set key. Non-existent member elements will be ignored.

When the key is not a collection type, an error is returned.

srem tempSet 123 abc
returns: (integer) 2

Set typesmove

SMOVE source destination member:

Move the member element from the source collection to the destination collection.

SMOVE is an atomic operation .

If the source set does not exist or does not contain the specified member element, the SMOVE command does nothing and only returns 0. Otherwise, the member element is removed from the source collection and added to the destination collection.

When the destination set already contains the member element, the SMOVE command simply deletes the member element in the source set.

When source or destination is not a collection type, an error is returned.

smove newSet tempSet 123
returns: (integer) 1

SortedSet typezadd

ZADD key score member [[score member] [score member] ...]:

Add one or more member elements and their score values ​​to the ordered set key.

If a member is already a member of an ordered set, update the score value of this member and reinsert the member element to ensure that the member is in the correct position.

The score value can be an integer value or a double-precision floating point number.

If the key does not exist, an empty ordered set is created and the ZADD operation is performed.

When the key exists but is not an ordered set type, an error is returned.

 zadd ssetKey 10 xiaoming
returns: (integer) 1

SortedSet typezcard

ZCARD key:

Returns the cardinality of the ordered set key.

 zcard ssetKey
returns: (integer) 2

SortedSet typezrange

ZRANGE key start stop [WITHSCORES]:

Returns the members in the specified range in the ordered set key.

The positions of the members are sorted by increasing score value (from small to large).

Members with the same score value are arranged in lexicographical order.

If you need members to be ranked in decreasing score value (from largest to smallest), please use the ZREVRANGE command.

zrange ssetKey 0 -1
1) "xiaoming"
2) "123"
3) "erha"

SortedSet typezrevrange

ZREVRANGE key start stop [WITHSCORES]:

Returns the members of the ordered set key in the specified range.

The positions of the members are arranged according to the decreasing score value (from large to small).
Members with the same score value are arranged in reverse lexicographical order.
Except that the members are arranged in the descending order of the score value, other aspects of the ZREVRANGE command are the same as the ZRANGE command.

SortedSet typezrank

ZRANK key member:

Returns the rank of member member in the ordered set key. The members of the ordered set are arranged in the order of increasing score value (from small to large).

The ranking is based on 0, that is, the member with the smallest score is ranked 0.

Use the ZREVRANK command to get the ranking of members in descending order of score (from largest to smallest).

zrank ssetKey xiaoming
returns: (integer) 0

zrevrank ssetKey xiaoming
returns: (integer) 2

SortedSet typezrem

ZREM key member [member ...]:

Remove one or more members from the ordered set key, and the non-existent members will be ignored.

When the key exists but is not an ordered set type, an error is returned.

zrem ssetKey 123
returns: (integer) 1


3. Summary

A thousand miles begins with a single step. . Hurry up, pay attention, collect, and get started! ! !

(PS: The redis command is not case sensitive. So, the case is the same!!)

Interested friends, welcome to collect; if there are deficiencies, you can leave a message to discuss, and the message must be returned! ! !