The basic 5 data structures of redis study notes

table of Contents

String

Commonly used commands are as follows

basic component

List

Common commands

basic component:

Dictionary (hash):

 Commonly used commands:

  basic component:

Set:

   Commonly used commands:

  basic component:

Ordered set (zset):

Commonly used commands:

basic component

Recommended reading

Redis has 5 basic data structures, namely: string, list, dictionary, set, and ordered set. Next, we will briefly explain the 5 data structures.

  • String

  • Commonly used commands are as follows

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello  
(nil)
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
"103"
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
"102"
127.0.0.1:6379> mset boy name1 girlsname2
127.0.0.1:6379> mget boy girl
1) "name1"
2) "name2"
  • basic component

    Redis string is composed of simple dynamic string (simple dynamic string, SDS)

  • List

  • Common commands

#队列:右进左出
127.0.0.1:6379> rpush IT python java go js
(integer) 4
127.0.0.1:6379> llen IT
(integer) 4
127.0.0.1:6379> lpop IT 
"python"
127.0.0.1:6379> lpop IT 
"java"
127.0.0.1:6379> lpop IT 
"go"
127.0.0.1:6379> lpop IT 
(nil)

#栈:右进右出
127.0.0.1:6379> rpush IT python java go js
(integer) 4
127.0.0.1:6379> rpop IT 
"python"
127.0.0.1:6379> rpop IT 
"java"
127.0.0.1:6379> rpop IT 
"go"
127.0.0.1:6379> rpop IT 
(nil)

#根据下标取值
127.0.0.1:6379> rpush IT python java go js
(integer) 4
127.0.0.1:6379> lindex IT 1
"java"
#根据范围取
127.0.0.1:6379> lrange IT 0 -1
1)"python"
2)"java"
3)"go"
4)"js"
  • basic component:

         The bottom layer of the list is composed of a linked list (linkedlist) and a compressed list (ziplist) (the latter is composed of quicklist), how to switch between the linked list and the compressed list? Use compressed list when the following two points are met, otherwise use linked list:

      1- The length of all string elements stored in the list object is less than  64 bytes;

      2- The number of elements stored in the list object is less than  512 one;

  • Dictionary (hash):

  •  Commonly used commands:

127.0.0.1:6379> hset boy name lj
(integer) 1
127.0.0.1:6379> hset boy email [email protected]
(integer) 1
127.0.0.1:6379> hgetall boy
1) "name"
2) "lj"
3) "email"
4) "[email protected]"
127.0.0.1:6379> hget boy user
(nil)
127.0.0.1:6379> hget boy name
"lj"
  •   basic component:

    The dictionary is composed of a compressed list (ziplist) and a hashtable. How do you switch between the hashtable and the compressed list? Use compressed list when the following two points are met, otherwise use hashtable:

     1- The string length of the key and value of all key-value pairs stored in the hash object is less than  64 bytes;

     2- The number of key-value pairs saved by the hash object is less than  512 one

  • Set:

  •    Commonly used commands:

127.0.0.1:6379> sadd name tom jack
(integer) 3
127.0.0.1:6379> SMEMBERS name  #set是无序的,插入顺序和查询顺序并不一致
1) "jack"
2) "tom"
127.0.0.1:6379> SISMEMBER name  
(integer) 1
127.0.0.1:6379>scard name  
(integer) 2
127.0.0.1:6379>spop name 
"tom"
  •   basic component:

       A set is composed of an integer set (intset) and a hashtable. How do you switch between hashtable and integer set? Use integer sets when the following two points are met, otherwise use hashtable:

       1- All elements saved by the collection object are integer values;

       2- The number of elements stored in the collection object does not exceed  512 one;

  • Ordered set (zset):

  • Commonly used commands:

127.0.0.1:6379> zadd myscoreset 100 hao 90 xiaohao
(integer) 2
127.0.0.1:6379> ZRANGE myscoreset 0 -1
1) "xiaohao"
2) "hao"
127.0.0.1:6379> ZSCORE myscoreset hao
"100"
  • basic component

        The bottom layer of an ordered set is composed of a skiplist and a ziplist. How do you switch between the skiplist and the compressed list? Use compressed list when the following two points are met, otherwise use skip list:

     1- The number of elements stored in the ordered set is less than  128 one;

     2- The length of all element members stored in the ordered set are less than  64 bytes;

Recommended reading

Common commands can refer to: rookie tutorial or redis official document

Data structure of redis study notes ----- skiplist (skip list)

The data structure of redis study notes ----- zipset (compressed list), intset (set of integers), listpack (compact list)

The data structure of redis study notes-SDS (simple dynamic string)

The data structure of redis study notes ----- dict (dictionary)

Guess you like

Origin blog.csdn.net/lin_keys/article/details/105882476