Home  >  Article  >  Database  >  Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

WBOY
WBOYforward
2022-11-07 17:15:501928browse

This article brings you relevant knowledge about Redis, which mainly introduces the relevant content about common data structures. There are five most commonly used ones, namely string, hash, Let’s take a look at lists, sets and ordered sets. I hope it will be helpful to everyone.

Commonly used data structures in Redis (organized and shared)

Recommended learning: Redis video tutorial

Redis common data structures

Redis provides some data structures for When we access data in Redis, there are five most commonly used ones: String, Hash, list, set, and ordered set (ZSET).

String (String)

The string type is the most basic data structure of Redis. First of all, the keys are all string types, and several other data structures are built on the basis of the string type, so the string type can lay the foundation for the learning of the other four data structures. The value of the string type can actually be a string (simple string, complex string (such as JSON, XML)), number (integer, floating point number), or even binary (picture, audio, video), but the value is the largest Cannot exceed 512MB.

(Although Redis is written in C, there are strings in C which are essentially implemented using char arrays. However, due to various considerations, Redis still implements the string type itself)

Operation command

set set value

set key valueCommonly used data structures in Redis (organized and shared)

The set command has several options:

ex seconds: Set the second-level expiration time for the key.

px milliseconds: Set the millisecond expiration time for the key.

nx: The key must not exist before it can be set successfully and is used for adding (commonly used for distributed locks).

xx: Contrary to nx, the key must exist before it can be set successfully and used for update.

Commonly used data structures in Redis (organized and shared)

From the execution effect, the ex parameter is basically the same as the expire command. Another thing that needs special attention is that if a string has an expiration time set, and then you call the set method to modify it, its expiration time will disappear.

The execution effects of nx and xx are as follows

Commonly used data structures in Redis (organized and shared)

In addition to the set option, Redis also provides two commands: setex and setnx:

setex key seconds value

setnx key value

The functions of setex and setnx are the same as the ex and nx options. That is, setex sets the second-level expiration time for the key. The key must not exist when setnx is set before it can be set successfully.

setex example:

Commonly used data structures in Redis (organized and shared)

##setnx example:

Commonly used data structures in Redis (organized and shared)

Because key foo-ex already exists, So setnx fails and the return result is 0. The key foo-ex2 does not exist, so setnx succeeds and the return result is 1.

Are there any application scenarios? Take the setnx command as an example. Due to the single-threaded command processing mechanism of Redis, if multiple clients execute setnx key value at the same time, only one client can set it successfully according to the characteristics of setnx. , setnx can be used as an implementation solution for distributed locks. Of course, distributed locking does not require just one command. There are many things to pay attention to. We will use a separate chapter to describe distributed locking based on Redis later.

get Get the value
If the key to be obtained does not exist, return nil (empty):

Commonly used data structures in Redis (organized and shared)

mset Set values ​​in batches
Set 4 key-value pairs at one time through the mset command

Commonly used data structures in Redis (organized and shared)

mget Get values ​​in batches

Commonly used data structures in Redis (organized and shared)

Obtained the values ​​​​of keys a, b, c, d in batches:

If some keys do not exist, then their values ​​​​are nil (null), the results are returned in the order of the input keys.

Batch operation commands can effectively improve efficiency. If there is no command like mget, the specific time required to execute n get commands is as follows:

n times get time = n times network time n times commands Time

After using the mget command, the specific time required to execute n get command operations is as follows:

n times get time = 1 time network time n times command time

Redis can support tens of thousands of read and write operations per second, but this refers to the processing capability of the Redis server. For the client, in addition to the command time, a command also has network time. Assuming that the network time is 1 millisecond, the command The time is 0.1 milliseconds (based on processing 10,000 commands per second), then it takes 1.1 seconds to execute 1,000 get commands (10001 10000.1=1100ms), and 1 mget command takes 0.101 seconds (1 1 10000.1=101ms).

Incr Numerical Operations

The incr command is used to perform increment operations on values. The returned results are divided into three situations:

The value is not an integer, Return an error.

The value is an integer, and the result after auto-increment is returned.

The key does not exist. It will be incremented according to the value of 0 and the returned result will be 1.

Commonly used data structures in Redis (organized and shared)

In addition to the incr command, Redis provides decr (self-decrement), incrby (self-increment specified number), decrby (self-decrement specified number), incrbyfloat (self-increasing floating point number) ), students are asked to try it on their own for specific effects.

append append command

append can append a value to the end of the string

Commonly used data structures in Redis (organized and shared)

strlen string Length

Return string length

Commonly used data structures in Redis (organized and shared)

Note: Each Chinese character occupies 3 bytes

getset setting And return the original value

getset will set the value just like set, but the difference is that it will also return the original value of the key

Commonly used data structures in Redis (organized and shared)

setrange sets the characters at the specified position

Commonly used data structures in Redis (organized and shared)

The subscript starts from 0.

getrange intercepts a string

getrange intercepts a part of the string to form a substring. You need to specify the start and end offsets. The intercepted range is a closed interval.

Commonly used data structures in Redis (organized and shared)

Time complexity of the command

String Among these commands, except del, mset, mget support batches of multiple keys Operation, the time complexity is related to the number of keys, which is O(n). Getrange is related to the string length, which is also O(n). The rest of the commands basically have a time complexity of O(1), in terms of speed. Still very fast.

Usage scenarios

The string type has a wide range of usage scenarios:

Cache function

Redis As the cache layer, MySQL serves as the storage layer, and most of the requested data is obtained from Redis. Since Redis has the characteristics of supporting high concurrency, caching can usually play a role in accelerating reading and writing and reducing back-end pressure.

Counting

Using Redis as the basic tool for counting, it can realize the functions of fast counting and query caching, and the data can be asynchronously landed to other data sources.

Shared Session

A distributed Web service saves the user's Session information (such as user login information) in their respective servers. This will cause a problem. For load balancing considerations, distributed services will balance user access to different servers. Users who refresh their access may find that they need to log in again. This problem is intolerable to users.

In order to solve this problem, you can use Redis to centrally manage the user's Session. In this mode, as long as Redis is highly available and scalable, every time the user updates or queries login information, it will be directly accessed from Centralized acquisition in Redis.

speed limit

For example, for security reasons, many applications will ask users to enter a mobile phone verification code every time they log in to determine whether they are the user. myself. However, in order to prevent the SMS interface from being accessed frequently, the frequency of users obtaining verification codes per minute will be limited, for example, no more than 5 times per minute. Some websites restrict an IP address from being asked more than n times in one second, and a similar idea can be adopted.

Hash

Java provides HashMap, and Redis also has a similar data structure, which is the hash type. But please note that the mapping relationship in the hash type is called field-value. Note that the value here refers to the value corresponding to the field, not the value corresponding to the key.

Operation command

Basically, the hash operation command is very similar to the string operation command. Many commands add the letter h in front of the string type command, which means it is an operation. Greek type, and also specify the value of the field to be operated on.

hset set value

hset user:1 name lijin

Commonly used data structures in Redis (organized and shared)

If the setting is successful, 1 will be returned, otherwise 0 will be returned. In addition, Redis provides the hsetnx command. Their relationship is the same as the set and setnx commands, except that the scope changes from key to field.

hget value

hget user:1 name

If the key or field does not exist, nil will be returned.

Commonly used data structures in Redis (organized and shared)

hdel delete field

hdel will delete one or more fields, and the return result is the number of successfully deleted fields.

Commonly used data structures in Redis (organized and shared)

hlen calculates the number of fields

Commonly used data structures in Redis (organized and shared)

hmset sets values ​​in batches

Commonly used data structures in Redis (organized and shared)

hmget batch value

Commonly used data structures in Redis (organized and shared)

##hexists determine whether the field exists

Commonly used data structures in Redis (organized and shared)

If it exists, return 1, if it does not exist, return 1 Returns 0

hkeys gets all fields
It returns all fields with the specified hash key

Commonly used data structures in Redis (organized and shared)

hvals gets all values

Commonly used data structures in Redis (organized and shared)

hgetall gets all fields and values

Commonly used data structures in Redis (organized and shared)

When using hgetall, if the number of hash elements is large, Redis will be blocked. possible. If you only need to get part of the field, you can use hmget. If you must get all the field-values, you can use the hscan command. This command will progressively traverse the hash type. hscan will be introduced in a later chapter.

hincrby adds
hincrby and hincrbyfloat, just like the incrby and incrbyfloat commands, but their scope is filed.

hstrlen Calculates the string length of value

Commonly used data structures in Redis (organized and shared)

Time complexity of the command

In hash type operation commands, hdel, hmget , the time complexity of hmset is related to the number of fields carried by the command, O(k), hkeys, hgetall, hvals is related to the total number of fields stored, O(N). The time complexity of the rest of the commands is O(1).

Usage scenarios

As can be seen from the previous operations, the operations of String and Hash are very similar, so why do you need to create a hash for storage.

The hash type is more suitable for storing object type data. We can compare it. If the user recorded in the database table is:

idnameage##12

1. Using the String type

requires inserting and retrieving one by one.

set user:1:name lijin;

set user:1:age 18;

set user:2:name msb;

set user :2:age 20;

Advantages: simple and intuitive, each key corresponds to a value

Disadvantages: too many keys, taking up a lot of memory, user The information is too scattered and cannot be used in the production environment

2. Serialize the object and store it in redis

set user:1 serialize(userInfo);

Advantages: Simple programming, reasonable memory usage if serialization is used

Disadvantages: Serialization and deserialization have a certain overhead, userInfo needs to be updated when updating attributes Take it all out and deserialize it, update it and then serialize it to redis

3. Use hash type

hmset user:1 name lijin age 18

hmset user:2 name msb age 20

Advantages: simple and intuitive, reasonable use can reduce memory space consumption

Disadvantage: need to control Internal encoding format, inappropriate format will consume more memory

List (list)

The list (list) type is used to store multiple ordered strings, a The four elements , b, c, c, and b form an ordered list from left to right. Each string in the list is called an element. A list can store up to (2^32-1) Element(4294967295).

Commonly used data structures in Redis (organized and shared)

In Redis, you can insert (push) and pop (pop) both ends of the list, you can also get a list of elements in a specified range, get elements with a specified index subscript, etc. . List is a relatively flexible data structure that can act as a stack and queue and has many application scenarios in actual development.

The list type has two characteristics:

First, the elements in the list are ordered, which means that an element can be obtained through the index subscript Or a list of elements within a range.

Second, the elements in the list can be repeated.

Operation command

lrange Gets a list of elements within the specified range (elements will not be deleted)

key start end

Index subscript features: from left To the right is 0 to N-1

lrange 0 -1 command can get all elements of the list from left to right

rpush insert to the right

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

lpush Insert to the left

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

linsert Insert before or after an element New element

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

The three return results are the length of the current list after the command is completed, also It is the number of elements contained in the list. At the same time, both rpush and lpush support inserting multiple elements at the same time.

lpop pops from the left side of the list (elements will be deleted)

Commonly used data structures in Redis (organized and shared)r

Please note that the elements will disappear after popping up.

rpop Pop from the right side of the list

rpop will pop the element d on the far right side of the list.

Commonly used data structures in Redis (organized and shared)

lrem Delete the specified element

Commonly used data structures in Redis (organized and shared)

lrem command will find the element equal to value from the list and delete it. There are three situations according to the count:

count>0, from left to right, delete up to count elements.

count

count=0, delete all.

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

The return value is the number of elements actually deleted.

ltirm Trim the list according to the index range

For example, if you want to retain the 0th to 1st elements in the list

Commonly used data structures in Redis (organized and shared)ls

lset modify the specification The element at the index subscript

Commonly used data structures in Redis (organized and shared)

lindex Gets the element at the specified index subscript of the list

Commonly used data structures in Redis (organized and shared)l

llen Gets the length of the list

Commonly used data structures in Redis (organized and shared)

blpop and brpop blocking pop-up elements

blpop and brpop are blocking versions of lpop and rpop. In addition, they also support multiple list types and settings. Set the blocking time in seconds. If the blocking time is 0, it means it will continue to block. Let's take brpop as an example.

Commonly used data structures in Redis (organized and shared)

A client is blocked (because it will block if there is no element)

Commonly used data structures in Redis (organized and shared)

A client has been blocked . At this time, we execute

Commonly used data structures in Redis (organized and shared)

from another client B. Client A will output

Commonly used data structures in Redis (organized and shared)

. Note: If after brpop If there are multiple keys, then brpop will traverse the keys from left to right. Once a key can pop up the element, the client will return immediately.

Usage Scenarios

The list type can be used for example:

Message queue, Redis's lpush brpop command combination can achieve blocking queue, the producer client uses lrpush from the list Insert elements on the left, and multiple consumer clients use the brpop command to "grab" elements at the end of the list in a blocking manner. Multiple clients ensure load balancing and high availability of consumption.

Article List

Each user has his or her own article list, and now the article list needs to be displayed in pages. At this time, you can consider using a list, because the list is not only ordered, but also supports obtaining elements according to the index range.

Implement other data structures

lpush lpop =Stack(stack)

lpush rpop =Queue(queue)

lpsh ltrim =Capped Collection(limited collection )

lpush brpop=Message Queue(Message Queue)

SET

Commonly used data structures in Redis (organized and shared)

The set type is also used to save Multiple string elements, but unlike the list type, duplicate elements are not allowed in the set, and the elements in the set are unordered, and the elements cannot be obtained through index subscripts.

A collection can store up to 2 to the 32nd power - 1 element. In addition to supporting addition, deletion, modification, and query within a collection, Redis also supports intersection, union, and difference sets of multiple collections. Proper use of collection types can solve many practical problems in actual development.

In-set operation command

sadd Add element

You can add multiple elements, and the return result is the number of successfully added elements

Commonly used data structures in Redis (organized and shared)

srem Delete elements

Allows to delete multiple elements, the return result is the number of successfully deleted elements

Commonly used data structures in Redis (organized and shared)

scard Calculate the number of elements

Commonly used data structures in Redis (organized and shared)

sismember determines whether the element is in the set

If the given element element is in the set, it returns 1, otherwise it returns 0

Commonly used data structures in Redis (organized and shared)

srandmember Randomly returns the specified number of elements from the collection

If the specified number is not written, the default is 1

Commonly used data structures in Redis (organized and shared)

spop randomly pops elements from the collection

You can also specify the number. If not written, the default is 1. Note that since it is popping, after the spop command is executed, the elements will be deleted from the collection, but srandmember will not .

Commonly used data structures in Redis (organized and shared)

smembers Get all elements (no elements will pop out)

The return result is unordered

Commonly used data structures in Redis (organized and shared)

##Inter-set operation command

Now there are two sets, they are set1 and set2

Commonly used data structures in Redis (organized and shared)##sinter Find the intersection of multiple sets

Commonly used data structures in Redis (organized and shared)suinon Find the union of multiple sets

Commonly used data structures in Redis (organized and shared)sdiff Find the difference of multiple sets

Commonly used data structures in Redis (organized and shared)Save the results of intersection, union, and difference sets

sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...]复制代码
The operations between sets will be more time-consuming when there are many elements, so Redis provides the above three commands ( The original command store) saves the results of intersection, union, and difference between sets in the destination key, for example:

Commonly used data structures in Redis (organized and shared)Usage scenario

Comparison of set types A typical usage scenario is tags. For example, one user may be interested in entertainment and sports, while another user may be interested in history and news. These points of interest are tags. With this data, you can get the people who like the same tag and the tags that users like in common. This data is important for user experience and enhancing user stickiness.

For example, an e-commerce website will make different types of recommendations to users with different labels. For example, for people who are more interested in digital products, the latest digital products will be recommended to them on various pages or through emails. Usually it will bring more benefits to the website.

In addition, collections can also generate random numbers for activities such as lottery activities, social graphs, etc.

Ordered sets (ZSET)

Commonly used data structures in Redis (organized and shared)Ordered sets are a little unfamiliar compared to hashes, lists, and sets, but since they are called If an ordered set is an ordered set, then it must be related to the set. It retains the feature that the set cannot have duplicate members, but the difference is that the elements in the ordered set can be sorted. But unlike the list that uses index subscripts as the basis for sorting, it sets a score for each element as the basis for sorting.

The elements in the ordered set cannot be repeated, but the score can be repeated, just like the student numbers of students in the same class cannot be repeated, but the test scores can be the same.

Ordered sets provide functions such as obtaining specified scores and element range queries, calculating member rankings, etc. Proper use of ordered sets can help us solve many problems in actual development.

Operation commands within the collection

zadd adds members

Commonly used data structures in Redis (organized and shared)The return result represents the number of successfully added members

To Note:

Commonly used data structures in Redis (organized and shared)zadd command also has four options nx, xx, ch, incr. Four options

nx: member must not exist before it can be set. Success, for adding.

xx: member must exist before it can be set successfully and used for updates.

ch: Returns the number of ordered set elements and scores that have changed after this operation

incr: Increases the score, which is equivalent to the zincrby introduced later

zcard Calculate the number of members

Commonly used data structures in Redis (organized and shared)zscore Calculate the score of a member

Commonly used data structures in Redis (organized and shared)If the member does not exist, return nil

zrank calculates the ranking of members

zrank是从分数从低到高返回排名

zrevrank反之

很明显,排名从0开始计算。

zrem 删除成员

Commonly used data structures in Redis (organized and shared)

允许一次删除多个成员。

返回结果为成功删除的个数。

zincrby 增加成员的分数

Commonly used data structures in Redis (organized and shared)

zrange和zrevrange返回指定排名范围的成员

有序集合是按照分值排名的,zrange是从低到高返回,zrevrange反之。如果加上 withscores选项,同时会返回成员的分数

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

zrangebyscore返回指定分数范围的成员
zrangebyscore key min max [withscores] [limit offset count]
zrevrangebyscore key max min [withscores][limit offset count]复制代码

其中zrangebyscore按照分数从低到高返回,zrevrangebyscore反之。例如下面操作从低到高返回200到221分的成员,withscores选项会同时返回每个成员的分数。

同时min和max还支持开区间(小括号)和闭区间(中括号),-inf和+inf分别代表无限小和无限大:

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

zcount 返回指定分数范围成员个数

zcount key min max

Commonly used data structures in Redis (organized and shared)

zremrangebyrank 按升序删除指定排名内的元素

zremrangebyrank key start end

zremrangebyscore 删除指定分数范围的成员

zremrangebyscore key min max

集合间操作命令

zinterstore 交集

zinterstoreCommonly used data structures in Redis (organized and shared)

这个命令参数较多,下面分别进行说明

destination:交集计算结果保存到这个键。

numkeys:需要做交集计算键的个数。

key [key ...]:需要做交集计算的键。

weights weight [weight ...]:每个键的权重,在做交集计算时,每个键中的每个member 会将自己分数乘以这个权重,每个键的权重默认是1。

aggregate sum/ min |max:计算成员交集后,分值可以按照sum(和)、min(最小值)、max(最大值)做汇总,默认值是sum。

不太好理解,我们用一个例子来说明。(算平均分)

Commonly used data structures in Redis (organized and shared)

Commonly used data structures in Redis (organized and shared)

zunionstore 并集

该命令的所有参数和zinterstore是一致的,只不过是做并集计算,大家可以自行实验。

使用场景

有序集合比较典型的使用场景就是排行榜系统。例如视频网站需要对用户上传的视频做排行榜,榜单的维度可能是多个方面的:按照时间、按照播放数量、按照获得的赞数。

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第26天,点击查看活动详情

推荐学习:Redis视频教程

lijin 18
msb 20

The above is the detailed content of Commonly used data structures in Redis (organized and shared). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete