In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to learn the data structure of Redis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to learn the data structure of Redis.
Redis installation and download address: http://redis.io/download installation steps: 1, yum install gcc2, wget http://download.redis.io/releases/redis Mel 5.0.3.tar.gztar xzf redis-5.0.3.tar.gzcd redis-5.0.33, make4, src/redis-server redis.conf (pay attention to using background startup So change the daemonize in redis.conf to yes) 5, ps-ef | grep redis6, src/redis-cliRedis core data structure and usage scenarios
String structure
Common string operations
SET key value / / save string key value pair MSET key value [key value.] / bulk store string key value pair SETNX key value / / store a non-existent string key value pair GET key / / get a string key value MGET key [key.] / / batch get string key value DEL key [key...] / / delete a key EXPIRE key seconds / / set the expiration time of a key (seconds)
Atomic addition and subtraction
INCR key / / add the digital value stored in key to 1DECR key / / subtract the digital value stored in key / / add the value stored in key to incrementDECRBY key decrement / / subtract the value stored in key from decrement
String application scenario
This is the statistics of the number of articles read on Wechat official account, which can be solved through redis's string data structure. Using the incr atomic operation command, the key of redis is article:readcount: {article id}. When the article is read, call the incr command, add one to the number of readings, and realize the function of counting the number of articles by get the key of the redis.
Incr article:readcount: {article id} get article:readcount: {article id} Hash structure
Common operations of Hash
HSET key field value / / Storage the key value of a hash table key HSETNX key field value / / Store the key value HMSET key field value of a non-existent hash table key [field value...] / / store multiple key value pairs HGET key field / / get the field key value HMGET key field corresponding to the hash table key in a hash table key [field...] / / batch get multiple field keys in the hash table key [field...] / delete the field keys in the hash table key / / return the number of field in the hash table key HGETALL key / / return all the key values HINCRBY key field increment in the hash table key / / add incremental increment to the value of the field key in the hash table key
Hash usage scenario
This is a screenshot of JD.com 's shopping cart. We can see that many functions can be realized with redis commands. Save the operation of the product id, shopping cart id and so on id in redis, save the description of the product in the front end, when operating the shopping cart, the bottom layer is actually the command to operate redis.
Add goods: hset cart:1001 1088 1 add quantity: hincrby cart:1001 1088 1 Total items: hlen cart:1001 delete items: hdel cart:1001 1008 get shopping cart all items: hgetall cart:1001
Advantages and disadvantages of Hash: the same kind of data is classified, integrated and stored, which is convenient for data management. Compared with string operation, it consumes less memory and cpu. Expiration function cannot be used on field, but can only be used on key.
List structure
Common operations of List
LPUSH key value [value...] / / insert one or more values value into the header (leftmost) of the key list RPUSH key value [value...] / / insert one or more values value into the footer (rightmost) of the key list LPOP key / remove and return the header element of the key list RPOP key / / remove And return the last element of the key list LRANGE key start stop / / return the elements in the specified interval in the list key Interval specifies BLPOP key [key...] with offsets start and stop Timeout / / an element pops up from the header of the key list. If there is no element in the list, block wait, timeout seconds, if timeout=0, keep blocking waiting for BRPOP key [key.] Timeout / / Pop up an element from the end of the key list. If there is no element in the list, block wait, timeout seconds, if timeout=0, keep blocking wait
Application scenarios of List
This is the Sina Weibo we often use. Let's see how to use redis's list data structure in Weibo articles. Sina Weibo is a major user of redis. According to friends who work in Sina, the total capacity of Sina's use of redis is more than 500T, and it is conceivable that almost 70% of the functions are operated in redis.
"Xiaomi Mobile" sends a message on Weibo with an id of 1001. The redis command used is:
LPUSH msg: {Xiaoqiang-id} 1001
"extra Changsha" posts on Weibo with a message id of 1002. The redis command used is:
LPUSH msg: {Xiaoqiang-id} 1002
Xiao Qiang checks the latest 10 Sina Weibo messages:
LRANGE msg: {Xiaoqiang-id} 0 10Set structure
Common operations of Set
SADD key member [member...] / / deposits elements into the collection key, ignoring the existence of elements If key does not exist, create a new SREM key member [member...] / / Delete elements from the set key / / get all the elements in the set key SCARD key / / get the number of elements of the set key SISMEMBER key member / / judge Whether the member element exists in the collection key SRANDMEMBER key [count] / / Select count elements from the collection key Elements do not delete SPOP key from key [count] / / select count elements from the collection key, elements are deleted from key
Set operation
SINTER key [key...] / / intersection operation SINTERSTORE destination key [key..] / / save the intersection result in the new set destination SUNION key [key..] / / Union operation SUNIONSTORE destination key [key...] / / save the union result in the new set destination SDIFF key [key...] / / subtraction operation SDIFFSTORE destination key [key...] / / save the result of the subtraction in the new set destination
Set usage scenario
Use this lottery interface as an example of the use of set, I believe everyone is familiar with this interface, WeChat Mini Programs's "lucky draw" function, this is a typical scenario using redis's set data structure. When the user clicks "participate in the lottery", that is, add the user ID to the collection:
SADD key {userID}
View all users participating in the raffle:
SMEMBERS key
Extract count winning users
SRANDMEMBER key [count] Zset ordered set structure
Common operations of ZSet
ZADD key score member [[score member]...] / / add the element ZREM key member [member...] to the ordered set key. / / remove element ZSCORE key member from ordered set key / / return the score of element member in ordered set key ZINCRBY key increment member / / is the score of element member in ordered set key plus increment ZCARD key / / return the number of elements in ordered set key ZRANGE key start stop [WITHSCORES] / / get ordered set key in positive order Elements from start subscript to stop subscript ZREVRANGE key start stop [WITHSCORES] / / get ordered set key subscript from start to stop subscript elements
Zset usage scenario
Weibo hot search list everywhere uses the data structure of Redis ordered collection, we all know that ordered collection is an extension of the collection, adding score field. Through the score field, we can select the largest or smallest topN, so we have a ranking. If you use a traditional database to achieve this function, it is estimated that the consumption of data performance is very large.
1) Click News ZINCRBY hotNews:20190819 1 Wuzhen 12: 00 hours 2) display Top 10 ZREVRANGE hotNews:20190819 0 10 WITHSCORES 3) Seven days search list calculation ZUNIONSTORE hotNews:20190813-20190819 7 hotNews:20190813 hotNews:20190814... HotNews:201908194) Top 10 ZREVRANGE hotNews:20190813-20190819 0 10 WITHSCORES for 7 days
At this point, I believe you have a deeper understanding of "how to learn the data structure of Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.