In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the application scenarios in Redis". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Redis data types and application scenarios
Redis is a Key-Value storage system written in ANSI C. The type of key is a string. [related recommendation: Redis video tutorial]
Data types of value 8 data types:
Common data types
String string type
List list type
Set collection type
Sortedset (zset) ordered collection type
Hash Typ
Unusual data type
Bitmap Bitmap Typ
Geo geolocation type
Stream Typ
Be careful
Commands in Redis ignore case, (set SET), and key do not ignore case (NAME name)
String string type
Redis's String can express three types of values: string, integer, floating-point number 100.01 is a six-bit string
Commonly used command name command format command description setset key value assignment getget key value getsetgetset key value value and value msetMSET key1 value1 key2 value2.. KeyN valueN sets the values of multiple key to their corresponding value. MgetMGET KEY1 KEY2.. KEYN returns all (one or more) values of a given key EXPIREEXPIRE key seconds sets the expiration time of a key (in seconds) appendappend key value appends the value to the tail strlenstrlen key gets the string length setnxsetnx key value uses assignment when value does not exist
Set key value NX PX 3000 atomic operation, px sets milliseconds incrincr key increments digits incrbyincrby key increment increases specified integers decrdecr key decrement digits decrbydecrby key decrement reduces specified integer application scenarios
1. Object caching
2. Single-valued cache
3. Incr is used for optimistic lock incr: increasing numbers, which can be used to implement optimistic lock watch (transaction)
4. Setnx is used for distributed locks when value does not exist. It can be used to implement distributed locks.
5. Counter
6. Web cluster session sharing
Examples of common methods are dockerRedis:0 > keys * dockerRedis:0 > append testName2 "1" dockerRedis:0 > exists testName "1" dockerRedis:0 > append testName "1234"6" dockerRedis:0 > get testName "2 1234" dockerRedis:0 > set testName1 "testName1"OK" dockerRedis:0 > get testName1 "testName1" dockerRedis:0 > getset testName2 "testName2" nulldockerRedis:0 > get testName2 "testName2" dockerRedis:0 > strlen testName "6" dockerRedis:0 > set incrTest "10"OK" dockerRedis:0 > incr incrTest "11" dockerRedis:0 > get incrTest "11" dockerRedis:0 > decr incrTest "10" dockerRedis:0 > decrby incrTest 5 "5" DockerRedis:0 > mset set01 1 set02 2 set03 3 "OK" dockerRedis:0 > mget set01 set02 set031) "1" 2) "2" 3) "3" list list type
The list list type can store ordered, repeatable elements to get records near the head or tail that are extremely fast. The maximum number of list elements is 2 ^ 32-1 (4 billion).
Common command name command format command description lpushlpush key v1 v2 v3... Insert the list lpoplpop key from the left and take out rpushrpush key v1 v2 v3 from the left side of the list. Insert the list from the right rpoprpop key take out the lpushxlpushx key value from the right side of the list, insert the value into the list header blpopblpop key timeout, take out the value from the left side of the list, block when the list is empty, you can set the maximum blocking time, in seconds llenllen key get the number of elements in the list lrangelrange key start end return the elements in the specified range in the list Interval is specified by start and end lsetlset key index value sets the element of the index position of the list to the value of value rpoplpushrpoplpush key1 key2 pops up from the right side of the key1 list and inserts it into the left side of the key2 list rpushxrpushx key inserts the value into the end of the list valuebrpopblpop key takes out the right side of the list, blocks when the list is empty, and can set the maximum blocking between timeout, in seconds lindexlindex key value to get the element index with the subscript index in the list, indexltrimltrim key start end prunes the list from 0 Leaving only the start to end range endbrpoplpushbrpoplpush pops up from the right side of the key1 list and inserts to the left side of the key2 list blocks key1 key2linsertlinsert key BEFORE/AFTER pivot value from inserting value into the list and applies the scene before or after the value pivot
1. Stack (stack) = LPUSH + LPOP
2. Queue (queue) = LPUSH + RPOP
3. Blocking MQ (blocking queue) = LPUSH + BRPOP
4. User list, product list, comment list
Set collection type
Set: the maximum number of members in an unordered, unique set of elements is 2 ^ 32-1
Common command name command format command description saddsadd key value1 value2. Deposit an element into the collection key, ignore it if the element exists, and create a new sremsrem key value1 value2 if the key does not exist. Delete the element smemberssmembers key from the set key to get all the elements in the set spopspop key count select the count elements from the set key, delete the element from the key, select the count element from the set key, and the element does not delete the scardscard key from the key to obtain the number of elements of the set key sismembersismember key member to determine whether the member element exists in the set key sintersinter key1 key2 key3 the intersection of many sets sdiffsdiff key1 key2 key3 the difference of many sets sunionsunion key1 key2 key3 the union of many sets application scenario
Wechat lucky draw Mini Program
Likes, collections and tags on Weibo
Weibo Wechat follow model
E-commerce commodity screening
Zset ordered collection type
SortedSet (ZSet) ordered set: the element itself is disordered and unrepeatable. Each element is associated with a score (score) that can be sorted by score, and the score is repeatable.
Common command name command format command description zaddzadd key score1 member1 score2 member2... Add the element zremzrem key mem1 mem2 with score to the ordered set key. Delete the element zcardzcard key from the ordered set key to get the number of elements in the ordered set zcountzcount key min max returns the score value in the collection in [min The number of elements in the max] interval zincrbyzincrby key increment member is the score of the element member in the ordered set key plus incrementzscorezscore key member returns the score of the element member in the ordered set zrankzrank key member gets the ranking of the member in the set (by score from small to large) zrangezrange key start end gets the ordered set key from start subscript to stop subscript element zrevrankzrevrank key member gets the ranking of member in the set (by score from largest to smallest) zrevrangezrevrange key start end The element application scenario of getting ordered set key from start subscript to stop subscript
Click ranking, sales ranking, follow ranking
Hash Typ
Redis hash is a mapping table for field and value of type string, which provides the mapping of fields and field values. Each hash can store 2 ^ 32-1 key-value pairs (more than 4 billion).
Advantages
1. Data of the same kind are classified, integrated and stored to facilitate data management
2. Compared with string operation, it consumes less memory and cpu.
3. Save more space than string storage.
Shortcoming
1. The expiration function cannot be used on field, but only on key.
2. Redis cluster architecture is not suitable for large-scale use.
Common Command name Command format Command description hsethset key field value stores the key values of a hash table key hmsethmset key field1 value1 field2 value2 stores multiple key-value pairs in a hash table key to see if a field exists hmgethmget key field1 field2. Get a field value hsetnxhsetnx key field value stores the key value of a non-existent hash table key hexistshexists key filed to determine whether the filed exists or not hgetallhgetall key gets multiple field values hdelhdel key field1 field2... Delete specified field hincrbyhincrby key field increment specified field self-increment incrementhlenhlen key get the number of fields application scenario
Object caching
Shopping cart operation
Bitmap Bitmap Typ
Bitmap is a bit operation that uses a bit bit to represent the value or state of an element, where key is the corresponding element itself. Bitmap itself will greatly save storage space.
Common command name command format command describes how setbitsetbit key offset value sets the bit value of key at offset (can only be 0 or 1). Getbitgetbit key offset gets the bit value of key at offset bitcountbitcount key gets the number of bit bits of key bitposbitpos key value returns the first index value set to bit value bitopbitop and [or/xor/not] destkey key [key.] Application scenario of storing multiple key in destkey after logical operation
1. The user checks in every month. The user id is key, and the date is used as the offset 1 to indicate check-in.
2. Count the active users. The date is key, and the user id is offset 1 to indicate active users.
3. Query the online status of the user. The date is key, and the offset 1 of the user id means online.
Geo geolocation type
Geo is used by Redis to process location information. It is officially used in Redis3.2. It mainly uses Z-degree curve, Base32 coding and geohash algorithm.
Common command name command format command description
Geoaddgeoadd key longitude and latitude member name 1 longitude 1 latitude 1 member name 2 longitude 2 latitude 2. Add geographic coordinates geoposgeopos key member name 1 member name 2... Return member latitude and longitude geodistgeodist key member 1 member 2 unit calculate the distance between members georadiusbymembergeoradiusbymember key member value unit count [desc] according to the member search nearby member geohashgeohash key member name 1 member name 2. Returns the standard geohash string application scenario
1. Record the geographical location
2. Calculate the distance
3. Find "people nearby"
Stream data flow type
Stream is a new data structure after Redis5.0, which is used for persistent message queues.
Almost everything that message queues have is satisfied, including:
Serialization Generation of message ID
Message traversal
Blocking and non-blocking reading of messages
Packet consumption of messages
Processing of unfinished messages
Message queue monitoring
Each Stream has a unique name, which is the key of Redis, which is automatically created when a message is appended with the xadd instruction for the first time
Application scenario
Use of message queues
This is the end of the content of "what are the application scenarios in Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.