In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what the Redis data types and application scenarios are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Basic operation of string type 1-1 string type data
Add / modify data: set key value
Get data: get key
Delete data: del key
Add / modify multiple data: mset key value key1 value1
Get multiple data: mget key key1
Append information to the back of the original data (if it does not exist, add it): append key value
1-2 string type addition and subtraction operation
Set value to increase the specified range of values: incr key adds 1 by default | each time incrby key value is added, the value setting data decreases the specified range: decr key | decrby key value is the same thing as adding
"Application scenario"
Control the database table primary key id, provide the primary key generation strategy for the database table, and ensure the consistency of the primary key of the database table.
1-3 string type aging operation
Set expiration time: setex key seconds value
"Application scenario"
Achieve time-limited voting functions: for example, a Wechat can vote once an hour to achieve hot information, such as hot items in the e-commerce industry and hot news on news websites.
Application scenarios of 1-4 string types
Weibo big V home page high-frequency visits, for the number of fans, followers, Weibo numbers all need to be updated from time to time. This belongs to high-frequency information, and we can use the string type of redis to solve the problem.
Set the user information for the big V in redis, with the user primary key and attributes as the key values, the following is an example of implementation.
Here we need to briefly talk about the naming rules of key: table name + primary key + primary key value + field: field value. Naming our keys with such rules is a good way to manage our keys.
We can also use another way to implement it, that is, a structure is directly followed by the key. For example, both of the above methods can be implemented, but the first one can easily manage any value. The second is that you have to change each one once, depending on the business scenario, and refresh it regularly.
2. Basic operation of hash type 2-1 hash type data
Add / modify data: hset key field value
Get data: hget key field | hgetall key
Delete data: hdel key field field1
Add / modify multiple data: hmset key field value field1 value1
Get multiple data: hmget key field field1
Get the number of fields in the table: hlen key
Get whether a field exists in the table: hexists key field
2-2 extended operation of hash type data
Get all the field values in the hash table: hkeys key
Get all the field values in the hash table: hvals key
Sets the value of the specified field and increases the value in the specified range: hincrby key field increment | hincrbyfloat key field increment
Shopping cart for 2-3 hash business scenarios
This picture comes from the network is not self-made, but simulates the shopping cart scene. In the above picture, we can see the information in the shopping cart. Let's use redis to implement the shopping cart.
Here is a shopping cart to add and get shopping cart, keys named table name + primary key + primary key value in the above figure, we will have a problem is that the product information storage will be a lot of repetition, so we also need to hash the item separately. As shown in the figure below, only goods are stored in id. Two settings are provided here, one is to set multiple fields, and the other is to store it directly as json. If the information does not change often, you can use json to provide you with a method hsetnx key field value. If you have it, you will not add it. If you do not, then add it. This function is used without overwriting and useless operations when different users add the same product.
3. List type
Data storage requirements: store multiple data and distinguish the order of the storage space of the data. The required data structure: a storage space stores multiple data, and the data can be reflected into the sequential list type: multiple data are saved, and the bottom layer is implemented using a two-way linked list storage structure.
Basic operation of 3-1 list type data
Add / modify data: lpush key value value1 | rpush key value value1
Get data: lrange key start end | lindex key index | llen key
Delete data: rpop key | lpop key
Extension operation of 3-2 list type data
Get and remove data within the specified time: blpop key1 key2 timeout | brpop key1 key2 timeout
This function is simple to write a case, easy to understand
The terminal instruction on the left will wait 30 seconds to return the deleted data.
When the add instruction on the right is executed, the left will directly return the deleted data.
3-3 list business scenario
In the above, we know that the basic operation of list: lpop key or rpop key can be deleted from do or right, but now there is a scenario where you like the business on moments, and then cancel the data from the middle. The case is shown below.
Let's first add a b c d to the list5 and then remove c. All that's left is a b d.
4. Set type
New storage requirements: store a large amount of data, provide more efficient storage structure in query convenience: be able to save a large amount of data, efficient internal storage mechanism, easy to query set types: exactly the same as hash storage structure, only store keys, do not store values (nil), and values are not allowed to repeat
Basic operation of 4-1 set type data
Add / modify data: sadd key member member1
Get data: smembers key
Delete data: srem key member1
Get the total amount of collection data: scard key
Determine whether the collection contains the specified data: sismember key member
4-2 set type data extension operation
Randomly get a specified amount of data in the collection: srandmember key count
Randomly get some data in the collection and change the dataset to remove the collection: spop key
4-3 set type business scenario recommendation information
Randomly push hot information, hot news, hot-selling tourism, apply app recommendation, follow recommendation, etc.
Since Kaka has been writing discuz recently, this case is intended to implement the focus recommendation.
Case 1: store the corresponding users in set according to a certain recommendation mechanism, and then randomly get 2 users who need referrals at a time.
Case 2: according to a certain recommendation mechanism, the corresponding users are stored in set, and then the recommended users can not be repeated every day according to the date.
4-4 set type business scenarios mining user relationships
The intersection, union, and difference of two sets
Sinter key key1
Sunion key key1
Sdiff key key1
The intersection, union and difference sets of two sets are stored in the specified set
Sinterstore destination key1 key2
Sunionstore destination key1 key2
Sdiffstore destination key1 key2
Case: we need to mine a mutual friend of information. For example, Wechat official account's common concern about the number of friends, QQ's recommendation mechanism for adding new friends, and in-depth mining of users' direct contacts.
According to the above example, we can use subtraction to realize the possible friends of qq.
4-5 set type business scenarios to realize the PV UV IP recording of the website
PV can directly use incr statistics of string type.
UV and IP are independent and do not repeat, using set to operate.
Above we know that set has a feature that can not be repeated, so we can easily implement this function based on this. Then use scard key to count the quantity.
As for the fact that UV is a unique visitor, you can use the local cookie to implement it. In the same way, you can pass cookie to redis for recording.
5. Sorted_set type
Sorting is not supported in the previous four types. The sorted_set type we look at supports not only storing big data, but also sorting.
5-1. Basic operation of sorted_set type
Add data: zadd key score member
Get data: zrange key start stop | zrevrange key start stop
Delete data: zrem key member
Obtain data by condition: zrangebyscore key min max limit | zrevrangescore key max min
Conditional deletion data: zremrangebyrank key start stop | zremrangebyscore key min max
Get the total amount of collection data: zcard key | zcount key min max
Collection intersection and union operation: zinterstore destination numkeys key | zunionstore destination numkeys key (this instruction will not be demonstrated, but you can view the document yourself. It's a bit like set, except that it adds up all the intersecting sums. Then there is a numkeys in it, and the parameter is a total of several key to calculate the following key.)
Get the index corresponding to the data: zrank key member | zrevrank key member
Socre value acquisition and modification: zscore key member | zincrby key increment member
The above is all the contents of Redis data types and application scenarios, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.