Detailed explanation of 5 Redis data structures

小云云
Release: 2023-03-21 09:02:02
Original
5629 people have browsed it

In this article, we mainly share with you detailed explanations of 5 Redis data structures. We hope that the cases and codes in the article can help everyone.

2.1.1 Global command

1 View all keys key*

2 The total number of keys dbsize (the dbsize command will not calculate the total number of keys Traverse all keys, but directly obtain the total number of keys built into Redis. The time complexity is O(1), while the keys command will traverse all keys, and the time complexity is O(n). When Redis saves a large number of keys, the line Use in the environment is prohibited)

3 Check whether the key exists exists key Returns 1 if it exists, 0 if it does not exist

4 Delete key del key Returns the number of successfully deleted keys , No return 0

5 key over -date Expire Key Seconds TTL commands will return the remaining expiration time key return type, if it does not exist, return none


2.1.2 Data structure and internal encoding


Each data structure has its own underlying internal encoding implementation, and it is Multiple implementations, so that Redis will choose the appropriate internal encoding in the appropriate scenario


Each data structure has more than two internal encoding implementations. For example, the list data structure includes linkedlist and ziplist. Internal encoding, you can query the internal encoding through the object encoding command


                                          Redis's design has two benefits: First, it can improve internal coding without affecting external data structures and commands. Second, multiple internal coding implementations can exert their respective advantages in different scenarios. For example, ziplist saves memory, but when there are many list elements, the performance decreases. At this time, Redis will convert the internal implementation of the list type into linkedlist

based on the configuration options 2.1.3 Single Threading architecture

                                                                                                                 , ,        , , , , , ,, , , , , , ,,,,,,,,,,,,,,,,,,,, 1

# The process of calling the client: Send the command, execute the command, and return the result


## all The commands are lined up in a queue and waited for execution, There is no situation where multiple commands are executed simultaneously. The time is about 100 nanoseconds, which is an important basis for Redis to achieve 10,000-level access per second. Second, non-blocking I/O. Redis uses epoll as the implementation of I/O multiplexing technology. In addition, Redis's own event processing model converts the connection, reading, writing, and closing in epoll into events, so as not to waste too much time on network I/O

                                       Three single threads avoid the consumption caused by thread switching and race conditions

Single threads bring several benefits: First, the implementation of data structure and algorithm single threads. Second, single threading avoids the consumption caused by thread switching and race conditions. However, there are requirements for the execution of each command. If the execution time of a certain command is too long, it will cause other commands to be blocked. Redis is a database for fast execution scenarios. Single thread is the core of understanding Redis​​​​​

2.2 String

The string type of Redis is the basis for several other types. The value can be a string (simple or complex json, xml), a number (integer, floating point), Binary (pictures, audio, video), the maximum value cannot exceed 512MB

                   

##               2.2.1 Command


1 commonly used command


1 Set value set key value second expiration time of milliseconds NX | xx


Setnx setxx #             Application scenarios: Since Redis is a single-threaded command processing mechanism, if multiple clients execute setnx key value at the same time, according to the characteristics, only one client can set it successfully, which can be used as an implementation solution for distributed locks


      2 Get the value get key if it does not exist and return nil


      3 Set the value mset key value in batches             4 Get the value mget key in batches


Learning to use batch operations will help improve business processing efficiency, but you must pay attention to the commands sent in each batch operation. Too many commands will cause Redis blocking or network congestion. key


There are three situations in which the result is returned

The value is not an integer and an error is returned


The value is an integer , return the result after incrementing ## The


# key does not exist, the self -increase is 0 according to the value, the return result is 1


and DECR (self -reduced), INCRBY Subtract the specified number), incrbyfloat (increment the floating point number)


    2 Uncommonly used commands


      1 Append value append key value


      2 String length strlen key


3 Set and return the original value getset key value


4 Set the characters at the specified position setrange key offset value


        5 Get part of the string getrange key start end

2.2.2 Internal encoding

There are 3 internal encodings for strings: int 8-byte long integer embstr less than A string equal to 39 bytes raw A string larger than 39 bytes. Redis will decide which internal encoding to use based on the type and length of the current value

2.2.3 Typical usage scenarios

                                                                                                                                                                                                                                                                                                       . Since Redis has the feature of supporting concurrency, caching can usually play a role in accelerating reading and writing and reducing back-end pressure. :Key name naming method: Business name: Object name: id: [Attribute] as key name

        Pseudo code implementation:

UserInfo getUserInfo(long id){
    userRedisKey="user:info:"+id
    value=redis.get(userRedisKey);
    UserInfo userInfo;
    if(value!=null){
        userInfo=deserialize(value)
    }else{
        userInfo=mysql.get(id)
        if(userInfo!=null)
        redis.setex(userRedisKey,3600,serizelize(userInfo))
        }
Copy after login
return userInfo
Copy after login
}
Copy after login

      2 Count

long incrVideoCounter(long id){
key="video:playCount:"+id;
return redis.incr(key)
}
Copy after login

Development Tips: Anti -cheating, counting according to different dimensions, data durable to the bottom layer data source

3 Share session













#                                                                                                                                                                                              A similar idea can be used when accessing more than n times within seconds

2.3 Hash


The hash type refers to the key value itself which is also a key-value pair structure


## 2.3.1 Command

1 Set value

hset key field value

                                                                                                                                                                                                                                                        Get the value hget key field Or get field-value hmget key field hmset key field value

6 Determine whether the field exists hexists key field


7 Get all field hkeys key


8 Get all Value HVALS Key

9 to get all the Field-Value HGETALL Key


Development Tips: If you must get all the Field-Value, you can use HSCAN to use HSCAN Command, this command will progressively traverse the hash type

10 hincrby hincrby float


11 Calculate the string length of value hstrlen key field


2.3 .2 Internal encoding


There are two types of internal encoding:

ziplist (compressed list) number of hash elements ###### 2.3.3 Usage scenarios#########        #


UserInfo getUserInfo(long id){
userRedisKey="user:info:"+id;
userInfoMap=redis.hgetAll(userRedisKey);
userInfoMap userInfo;

if(userInfoMap!=null){
userInfo=transferMapToUserInfo(userInfoMap);
}else{
userInfo=mysql.get(id);
redis.hmset(userRedisKey,tranferUserInfoToMap(userInfo));
redis.expire(userRedisKey,3600);
}
return userInfo;
}
Copy after login

             哈希类型和关系型数据库两点不同:

                1 哈希类型是稀疏的,而关系型数据库是完全结构化的

                2 关系型数据库可以做复杂的查询,而Redis去模拟关系型复杂查询开发困难,维护成本高

            三种方法缓存用户信息

                1 原声字符串类型:每个属性一个键

                    

               

                优点:简单直观,每个属性都支持更新操作

                缺点:占用过多的键,内存占用量较大,同时用户信息内聚性比较差,所以一般不会在生产环境用

               2 序列化字符串类型:将用户信息序列化后用一个键保存

        

                优点:简化编程,如果合理的使用序列化可以提高内存的使用效率

                缺点:序列化和反序列化有一定的开销,同时每次更新属性,都需要把数据取出来反序列化,更新后再序列化到Redis中

                3 哈希类型:每个用户属性使用一对field-value,但是只用一个键保存

                优点:简单直观,如果使用合理,可以减少内存空间的使用

                缺点:要控制哈希在ziplist和hashtable两种内部编码的转换,hashtable会消耗更多的内存

    2.4 列表

        列表类型用来存储多个有序的字符串,一个列表最多存储2的32次方-1个元素,列表是一种比较灵活的数据结构,它可以灵活的充当栈和队列的角色,在实际开发上有很多应用场景

        列表有两个特点:第一、列表中的元素是有序的,这就意味着可以通过索引下标获取某个元素或者某个范围内的元素列表。第二、列表中的元素可以是重复的

        2.4.1 命令

            1 添加操作

                 1.1 从右边往左插入元素 rpush key value

                 1.2 从左往右插入元素 lpush key value

                 1.3 向某个元素前或者后插入元素 linsert key before|after pivot value

                    

            2 查找

                1 获取指定范围内的元素列表 lrange key start end            

Index with two characteristics: First, the index is 0-N-1 from left to right, respectively, from right to left is -1-n, second, LRANDE's END option contains its own , this is not the same as many programming languages ​​​​that do not include end

      2 Get the element lindex key index

                                                                          or             or         or       or             or           or               or               or                     or                                   to  to get the element lindex key
#                    

3 Delete

1 Popping elements from the left side of the list lpop Key

## 2 2 pop -up RPOP Key


                 3 Delete the specified element lrem key count value


                                                                                                                                                                  lrem key                   Target element lset key index newValue

5 Blocking operation brpop blpop key timeout


1 The list is empty: if timeout=3, then the client will wait until 3s to return. If timeout= 0, the client will block and wait. If data is added, the client will immediately return

      2 The list is not empty: the client will immediately return

      3

2.4.2 Internal encoding

There are two internal encodings for list types

ziplist (compressed list): When the number of list elements is < list-max-ziplist-entries, and list-max-ziplist-value (64 bytes), Redis will use the internal implementation of the list to reduce memory usage When the conditions of ziplist cannot be met, Redis will use linkedlist as the internal implementation of the list

2.4.3 Usage scenarios

1 Message queue


The lpush+brpop command combination can realize blocking queue


                                                                                                                                                                      First, if a large number of articles are obtained in each paging, multiple hgetall operations need to be performed. In this case, consider using pipeline to obtain in batches, or consider serializing the article data into string type and using mget to obtain in batches. Second, when obtaining the article list in pages, the lrange command has better performance at both ends of the list. However, if the list is larger, the performance of obtaining the middle range elements of the list will become worse. At this time, you can consider two-level split

                                                                                                                                      using using using using             using using ’ s ’s ‐       ‐ off ‐   ‐                                                                               using
# lpsh +ltrim=Capped Collection(limited collection)

              lpush+brpop=Message Queue(Message Queue)

2.5 Collection

                                                                    1 There are duplicate elements, and the elements in the set are unordered

2.5.1 Commands

1 Operations within the set

1.1 Add elements sadd key element

1.2 Delete element srem key element

1.3 Calculate the number of elements scar key

1.4 Determine whether the element is in the set sismember key element

          1.5 Randomly return a specified number of elements from the collection srandmember key

        1.6 Randomly pop up elements from the collection spop key

      1.7 Get all elements smembers key

      2 Operations between sets

        1 Find the intersection sinter key of multiple sets...

        2 Find The union of multiple sets suinon key..

                                                                                                                                                                                                                                    . Save

                                                        sinterstore destination key                                                         sdiffstore key                                                                 


2.5.2 Internal encoding


There are two internal types of collection types:


intset (integer set): When the elements in the set When they are all integers and the number of elements is less than the set-max-intset-entries configuration (default 512), Redis will use intset as the internal implementation of the set, thereby reducing memory usage

              hashtable( Hash table) When the collection type cannot meet the conditions of intset, Redis will use hashtable as the internal implementation of the collection

2.5.3 Usage scenarios

Typical application scenarios of collection types is a label.


1 Add tags to users


# Sadd User: 1: Tags Tag1 Tag2


2 to add users

Sadd Tag1: Users User: 1 User: 3


## Development Tips: The relationship between the relationship between the user and the label should be executed in one transaction to prevent the data caused by some commands.

3 Delete the tags under the user

srem user:1:tags tag1 tag5

4 Delete the user under the tag

          srem tag1:users user:1

        5 Calculate the tags of common interest to users

                                                                                                                                                                                                                                                  been been been 5 Calculating tags of common interest to users

=Random item (generate random numbers, such as lottery)

spop/srandmember=Random item (generate random numbers, such as lottery) sadd+sinter=Social Graph (social needs)

2.6 Orderly Set

An ordered set is to add a score to the set as the basis for sorting

2.6.1 Command

1 In the collection

1Add memberzadd key score memeber

nx xx ch returns the number of elements and scores of the ordered set that have changed after this operation, incr: increase the score

Ordered sets provide a sorting field compared to sets, but they also produce Including the cost, the time complexity of zadd is O(log(n)), and the time complexity of sadd is O(1)

      2 Calculate the number of members

scard key

3 Calculate the score of a member zscore key member

4 Calculate the member’s ranking zrank key member

5 Delete the member zrem key member

6 6 Add members' scores Zincrby Key Increment Member

## 7 Back to the specified member Zrange Key Start End Members of the score range zrangebysore key min max


9 Returns the number of members in the specified score range zcount key min max


10 Delete the ascending elements in the specified ranking zremrangebyrank key start end


11 to delete the member of the specified score range, Zremrangebyscore Key MAX MAX


2 episodes operation rooms

1 intersection zintersstore destination numkeys Key

      2 Union zunionstore destionation numkeys key


2.6.2 Internal encoding


There are two internal encodings for ordered set types:


                 ziplist (compressed list) When the number of elements in the ordered set is less than the zset-max-ziplist-entries configuration, and the value of each element is less than the zset-max-ziplist-value configuration, Redis will use ziplist As the internal implementation of ordered collections, ziplist can effectively reduce memory usage. Skiplist (skip list) When ziplist conditions are not met, ordered collections will use skiplist as internal implementation, so this At this time, the reading and writing efficiency of ziplist will decrease


2.6.3 Usage scenarios


For example, a video website needs to rank the videos uploaded by users

1 Add User Like ZDD User: RANKING: 2016_03_15 Mike 3

## After Zincrby User: RANKING: 2016_03_15 Mike 1


                                                                                                                                                                                                                              Since rangebyrank user:ranking:2016_03_15 0 9

4 Display user information and user scores

# This function can use the user name as a key suffix and save user information in the hash type. Ranking can use two functions, zcore and zrank                                                                                                                 Management

1 Key rename rename key newkey

2 Randomly return a key randomkey

3 Key expiration -1 The key has no expiration setting Time - 2 The key does not exist

Expire key seconds: The key expires after seconds seconds

expire key itmestamp The key expires after the second-level timestamp timestamp

1 If the key of Expire Key does not exist, the return result is 0

2. If the expiration time is negative, the key will be deleted immediately, just like using the Del command

        3 The persist command can clear the expiration time of the key

          4 For string type keys, executing the set command will remove the expiration time. This issue is easily overlooked in development

      5 Redis does not support the expiration function of internal elements of the secondary data structure. For example, the expiration time cannot be set for an element of this list type.


        6 The setex command is used as a set+expire combination. It not only executes atomically, but also reduces the time of network communication. The methods, their implementation methods and usage scenarios are not the same


          1 move is used for data migration within Redis


          2 dump+restore is implemented in different Redis The function of data migration between instances. This migration is divided into two steps. 1. On the source Redis, the dump command will serialize the key value in the RDB format.


                             2 On the target Redis, the restore command restores the above serialized value, where the ttl parameter represents the expiration time

3 Migrate command used for data migration between redis instances

## This 2.7.2 traversed key


Redis provides two commands to traverse all the keys to separate, respectively It is keys and scan


                                                                                                                                                                                     not not have not been been been been been been been been in the key.


## [] represents a character matching a character


## \ x is used to transfer it. Causes blocking


            2 Progressive traversal


                                                                                                                                     keys until all keys in the dictionary are traversed

The corresponding commands include hsan, sscan, zcan

Progressive traversal can effectively solve the blocking problem that may occur with the keys command. When there are additions and deletions, the new keys cannot be guaranteed to be traversed to

2.7.3 Database management

1 Switch database select dbIndex

2 flushdb/flushall is used to clear the database, which will cause blocking when there is a lot of data.

2.8 Review of the end of this chapter


Related recommendations:


Redis data structure

The above is the detailed content of Detailed explanation of 5 Redis data structures. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!