This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

Introduction to Redis data structures

To Redis, all keys are strings. When we talk about basic data structures, we are talking about the data types that store values, including five common data types: String, List, Set, Zset, and Hash.

Structure type Structure to store values Structure literacy
String String It can be a string, integer, or floating point number Operations on the entire string or part of a string; Incrementing or decrement an integer or floating-point number;
List the List A linked list in which each node contains a string Push and pop operations on both ends of the list to read single or multiple elements; Find or delete elements by value;
The Set collection An unordered collection containing strings A collection of strings containing basic methods such as add, get, and delete; It also includes computing intersection, union and difference sets
Hash Hash An unordered hash table containing key-value pairs Include methods include adding, getting, and deleting individual elements
Zset ordered set Like a hash, used to store key-value pairs An ordered mapping between string members and floating point fractions; The order of elements is determined by the size of the fraction; Include methods include adding, getting, deleting individual elements, and getting elements based on a range of points or members

Basic data structure details

String String

String is the most basic data type in Redis. Each key corresponds to a value.

The String type is binary safe, meaning that a Redis String can contain any data. Such as numbers, strings, JPG images or serialized objects.

  • Command to use
The command The paper use
GET Gets the value stored in the given key GET name
SET Sets the value stored in the given key SET name value
DEL Deletes the value stored in the given key DEL name
INCR Increment the value stored by the key by 1 INCR key
DECR Decrement the value stored by the key by 1 DECR key
INCRBY Adds an integer to the value stored by the key INCRBY key amount
DECRBY Subtract the integer from the value stored by the key DECRBY key amount
  • Command execution
127.0.0.1:6379 >setHello 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 counterinteger) 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 counterinteger) 102
127.0.0.1:6379> get counter
"102"
Copy the code
  • Actual combat scene
    • Cache: The classic usage scenario, the common information, strings, pictures or videos and other information into Redis, Redis as the cache layer, mysql as the persistence layer, reduce mysql read and write pressure.
    • Counters: Redis is a single-threaded model, one command is executed before the next, and data can be dropped to other data sources.
    • Session: Common scheme Spring Session + Redis realizes session sharing.

List the List

The List in Redis is actually a linked List.

Using the List structure, we can easily implement the latest message queuing function (such as sina Weibo TimeLine). Another use of lists is message queues, where tasks can be stored in a List using a PUSH operation and then pulled out by a worker thread for execution using a POP operation.

  • Command to use
The command The paper use
RPUSH Pushes the given value to the right end of the list RPUSH key value
LPUSH Pushes the given value to the left end of the list LPUSH key value
RPOP Eject a value from the right end of the list and return the ejected value RPOP key
LPOP Pops a value from the left end of the list and returns the pop-up value LPOP key
LRANGE Gets all the values of a list in a given range LRANGE key 0 -1
LINDEX Gets the elements in the list by index. You can also use negative subscripts, -1 for the last element of the list, -2 for the next-to-last element of the list, and so on. LINEX key index
  • The technique of using lists
    • Lpush + lpop = Stack (Stack)
    • Lpush +rpop=Queue
    • Lpush + LTRIM =Capped Collection
    • Lpush + brPOP =Message Queue
  • Command execution
127.0.0.1:6379> lpush myList 12 ll ls mem (integer) 5 127.0.0.1:6379> lrange myList 0-1 1)"mem"
2) "ls"
3) "ll"
4) "2"
5) "1"127.0.0.1:6379 > lindex mylist - 1"1"127.0.0.1:6379 > lindex mylist 10# index is not in the range of myList
(nil)
Copy the code
  • Actual combat scene
    • TimeLine: Someone posts a tweet and adds it to the TimeLine with lpush to show new list information.
    • The message queue

The Set collection

Redis’ Set is an unordered collection of type String. Collection members are unique, which means that no duplicate data can occur in the collection.

The collection in Redis is realized by hash table, so the complexity of adding, deleting and searching is O(1).

  • Command to use
The command The paper use
SADD Adds one or more members to a collection SADD key value
SCARD Gets the number of members of the collection SCARD key
SMEMBER Returns all members of the collection SMEMBER key member
SISMEMBER Check whether the member element is a member of the collection key SISMEMBER key member

Some other set operations, please refer to www.runoob.com/redis/redis here…

  • Command execution
127.0.0.1:6379> sadd myset ycf ycf1 xiao ycf
(integer) 3
127.0.0.1:6379> smember myset
1) "xiao"
2) "ycf1"
3) "ycf"
127.0.0.1:6379> sismember myset ycf
(integer1)Copy the code
  • Actual combat scene
    • Tag, you can tag the user, or the user can tag the message, so that the same tag or similar tag can be used to recommend things or people to follow.
    • Click like, or click on, favorites, etc., can be put into the set to achieve

Hash Hash

Redis hash is a mapping of string fields and values, and hash is especially useful for storing objects.

  • Command to use
The command The paper use
HSET Add key-value pairs HSET hash-key sub-key1 value1
HGET Gets the value of the specified hash key HGET hash-key key1
HGETALL Gets all key-value pairs contained in the hash HGETALL hash-key
HDEL If a given key exists in a hash, the key is removed HDEL hash-key sub-key1
  • Command execution
127.0.0.1:6379> hset user name1 ycf
(integer) 1
127.0.0.1:6379> hset user email1 [email protected]
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "ycf"
3) "email1"
4) "[email protected]"
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
"ycf"
127.0.0.1:6379> hset user name2 xiaoycf
(integer) 1
127.0.0.1:6379> hset user email2 [email protected]
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "ycf"
3) "email1"
4) "[email protected]"
5) "name2"
6) "xiaoycf"
7) "email2"
8) "[email protected]"
Copy the code
  • Actual combat scene
    • Cache: intuitive, more space saving than String, the maintenance of cache information, such as user information, video information, etc.

Zset ordered set

Redis ordered collections, like collections, are collections of string elements and do not allow duplicate members. The difference is that each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest.

The members of an ordered set are unique, but the score can be repeated. Collections are implemented by hashing tables, so adding, deleting, and searching are O(1) complexity.

  • Command to use
The command The paper use
ZADD Adds a member with a given score to the ordered collection ZADD zset-key 178 member1
ZRANGE Retrieves multiple elements from an ordered collection based on their position in the ordered collection ZRANGE zset-key 0-1 withccores
ZREM If a given element member exists in an ordered collection, the element is removed ZREM zset-key member1

More order please refer to www.runoob.com/redis/redis here…

  • Command execution
127.0.0.1:6379> zadd myscoreset 100 ycf 90 xiaoycf
(integer) 2 127.0.0.1:6379> ZRANGE myscoreset 0-1 1)"xiaoycf"
2) "ycf"127.0.0.1:6379 > ZSCORE myscoreset ycf"100"
Copy the code
  • Actual combat scene
    • Leaderboards: An ordered collection of classic usage scenarios. For example, websites such as novel videos need to make a ranking of novel videos uploaded by users. The ranking can be scored according to the number of users’ attention, update time, word count and so on.

Refer to the article

  • www.cnblogs.com/haoprogramm…
  • www.pianshen.com/article/647…
  • www.runoob.com/redis/redis…