redis usage scenarios (8 types)

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it


Today, let's introduce the data structures of redis and their usage scenarios. My level is limited. If you are misled, welcome to learn together and make progress together!

1, 8 data types of redis?

There are 8 data types in redis, including 5 basic data types and 3 special data types
Basic data types: String, list (set), set (set), Zset (ordered set), hash (hash)
Special data types: geospatial (geographical location), hyperlog (advantage: small memory consumption), bitmap (bitmap)

1. string type

String type can store string, number and binary (bit). Common commands are as follows:

set  key  value   ex  10  nx     yes setnx  key  value;expire  key  10  Integration of. Represents 10 second expiration.
incr key		Automatic pair key of value  Add 1, value Must be numeric type
decr key
incrby  key  num    Automatic pair key of value conduct, value+num Assigned to value Operation of,
decrby  key  num

2. hash type

string is the most commonly used in hash, and the structure of hash is in the format of key file value. Common commands are as follows:

hset			Deposit one  key  filed  value    Hash structure of
hsetnx		Deposit one  key  field  value  ,if key If it already exists, the operation fails and does not exist,success
hget			Gets the specified key
hmset		Batch deposit  key  field
hmget		Batch acquisition  key  field
hdel 		Delete the specified  key  field
hincrby		yes key  field To add or subtract the value of

3. list type

Common commands are as follows:

lpush  key  value  [value ...]	to key Put an element on the left of the list key, key New if none exists
rpush  key  value  [value ...]	to key Put an element on the right of the list key, key New if none exists
lpop  key  				from key An element pops up at the leftmost end of the list key
rpop  key					from key An element pops up at the right end of the list key(delete from list)
lrange  key  start   stop		Get list key from start Subscript to  stop  Subscript element    lrange key 0 -1 whole
blpop  key[key...]  timeout	Blocked slave key An element pops up at the leftmost end of the list key. If there is no element in the list key,	
Blocked waiting{timeout}Seconds, if{timeout}=0,Always blocked
brpop   key[key...]  timeout	Blocked slave key An element pops up at the rightmost end of the list key. If there is no element in the list key,
Blocked waiting{timeout}Seconds, if{timeout}=0,Always blocked

4. set type

Common commands are as follows:

sadd  key  member [member...]		Forward set key key Store elements in, do key If it does not exist, it will be added
srem  key  member [member...]		From set key key Delete element from		(srem  sremove Delete meaning)
smembers  key					Get collection key key All elements in
scard  key						Get collection key key Number of elements
sismember  key  member			judge member Whether the element exists in the collection key key in
srandmember  key  [count]			From set key key Selected from{count}Element, not removed from the collection key
spop  key  [count]					From set key key Selected from{count}Element and removed from the collection key
count Is a number, representing from key Delete so many elements from(Random deletion). redis3.2 Later versions support this command
SET If possible, it can also:
Intersection operation:
sinter   key   [key...]
sinterstore  destination   key  [key...]
Union operation:
sunion  key  [key...]
sunionstore  destination  key [key...]
Difference set operation:
sdiff  key   [key...]
sdiffstore   destination  key  [key...]

Suppose there are three set
 set1  a,b,c
 set2  b,c,d
 set3  c,d,e
 sinter  set1  set2  set3		c
 sinterstore  destination  set1   set2  set3   : take set1,2,3 Union of set enter key Value call destination inside
 sunioin  set1  set2  set3		a,b,c,d,e
 sdiff set3  set2  set1			e	(This has a reference, set3 and set2 By comparison, yes e yes set2 Not in. With set1 comparison, set1 either e,So it is e)

5. zset type command

zadd  key  score  element  [...]		Forward ordered set key key Store elements in, score Is the score, element Is an element. if key If it does not exist, create a new one
zrem  key  element  [...]				Forward ordered set key key Delete element from
zscore	key  element					Get ordered collection key key in element Elemental score value
zincrby  key  increment  element		Ordered set key Medium element Element proceed score Value operation,
										increment The value of can be positive or negative. if key If it does not exist, create a new one, element If the element does not exist, add it score operation
										zcard  key								Get ordered collection key Number of elements in
zrange  key  start  stop  [withscores]	Get ordered set in positive order key from start Subscript to stop Subscript element.
Plus the back withscores The corresponding score will also be printed. If it is not added, only the open element will be printed
zrevrange  key  start  stop  [withscores]	Get ordered set in reverse order key from start Subscript to stop Subscript element
 Set operation
zunionstore	 destkey  numkeys  key [key..]	Union calculation	(Very common)
zinterstore	 destkey  numkeys  key [key..]	Intersection calculation (Not commonly used)

6,geospatial & hyperloglog & Bitmap

seeing the name of a thing one thinks of its function. geospatial is geographical location, hyperlog is log, and Bitmap is Bitmap. One of the characteristics of hyperloglog is that it occupies little memory.

2, Usage scenario

1. string type

  • Data caching to speed up response time and improve user experience.
  • Distributed lock (refer to the following "advantages of string over hash 3")

string and hash are almost the most commonly used, so I won't introduce them here

2. hash type

  • Shopping cart function
    Add hincrby {userid}: shoppingcart {goodsid} {count}
    Query hget {userid}: shoppingcart
    It means that each user has his own shopping cart (so the shopping cart is regarded as a key), and each commodity in the shopping cart is regarded as a field of hash structure; The quantity of each commodity is regarded as the value of the hash structure. If the user selects 2 for the number of pieces of the same commodity, add 1 to the value. Because it is the user id and commodity id, you can get the commodity unit price and commodity name.
  • Cache of menu data
    For example, your menus include ICs KM system and ICs customer system. Each system has its own type and value. For example, the channels of the ics-km system include official website, Alipay, WeChat, many spells, and small programs. You can use hash structure key-field-value structure, key is ics-km, field is Alipay, value is 0. The code is as follows (example):
hset ics-km-channel Alipay 0 Wechat 1 

3. list type

  • Message queuing can be implemented. Because it has a command called blpop key timeout. Blocked wait timeout seconds. The provider can be used to insert messages into the queue, and the consumer can block the consumption of messages from the queue. Although there is no sequential and persistent solution like the special message queue, this function can also be realized (it needs to be limited from other places)

  • You can implement the latest message of the list of concerns. For example, you pay attention to Zhang San and Li Si. When you read 2021.8.3, you should push the content of 2021.8.3; When you look at 2021.8.4, you should push the message of 2021.8.4. So how to achieve the latest news at the front? You can use the rpush key v of redis to push the latest message to the front.

4. set type

  • Like a lucky draw. Microblog bloggers said that if you forward my microblog, I will draw a lottery at random. (or swiping a gift lottery or something). For example, whoever forwards your microblog, you add the user id to the key. Then the second person forwards it and adds the user id of the second person to the key. Finally, when it's time to draw, it depends on your rules. If a person can only win one prize, you can spop key 2. (assuming that there are two people in the second prize, two people will be randomly selected to win the prize, and they will be moved out of the key after winning the prize). If you can repeat the middle, you can srandmember key 3 and draw 3 winners at random. After winning, the winners are still in the key and can still participate in the next lucky draw.
  • Can do some praise, sign in, my collection of knowledge... And so on
For example, if user 1001 likes a post with ID 8001, you can:	SADD  like_8001  1001
 The user cancelled the like again:			SREM	like_8001   1001
 Check whether user 1001 likes it		SISMEMBER   like_8001  1001
 Get users who like post 8001		SMEMBERS	like_8001
 Total number of users who get likes of post 8001		SCARD	like_8001
  • Tiktok can be used as user recommendation, product recommendation, etc. (for example, people who may be concerned with shaking).
Zhang San(People concerned are): 	zhangSub	--> 	{li,wang,zhao}
Li Si:	liSub				--> 	{zhang,wang,zhao,tian}
Wang Wu:	wangSub				--> 	{zhang,li,zhao,tian}
Zhang San opens Li Si's home page
 Common concerns of Zhang San and Li Si:				SINTER	zhangSub  liSub	 -->	{wang,zhao}
Zhang San is also concerned about him(Mutual attention)	SISMEMBER	wangSub	li,SISMEMBER  zhaoSub  li,..      (Traverse all the followers of Zhang San to see if there is Zhang San in the list of followers)
People Zhang San may know					SDIFF	liSub  zhangSub	-->		{zhang,li}	// Because Zhang San enters Li Si's home page and recommends people he may know to Zhang San, he recommends some of Li Si's concerns to Zhang San, but not Zhang San's

5. zset type

  • One day ranking list (news hot list, knowledge hot list, etc.). For example, you can create a new key for zset:
    zadd hostNewId_date 1 newId / / create a new hot news id_ key of date 1 news id
    zincrby hostNewId_date 1 newId / / add 1 each time you click
    zrevrange hostNewId_ Date 0 15 WithCores / / get the top 15 on the hot list and display the values.
  • Weekly list, monthly list, annual list, etc. For example, you use the knowledge Id_ The date is used as the key of zset of redis, and the combined key names of zunionstore are 7 key1... Key when making weekly list statistics
    7 (the zunionstore is fixed, the merged keys are random, 7 is the number of keys, followed by the value of each key) count the keys for 7 days, and then use zrevrange key 0 10 to get the first 10 data. Similarly, you can get the monthly list and annual list.

3, Other

1. What are the advantages of hash over string?

1. Information can be gathered together for easy management
2. To some extent, misoperation can be avoided and key conflict can be reduced
3, Reduce memory / io/cpu consumption (this means that redis is aimed at scanning, retrieving, ttl expiration, etc. for the key. Although there are multiple field s in the key, there is still only one key. If a string is used, a large number of keys will be generated, and a scan of a large number of keys will be generated. If a key is used, the Ke of the outermost layer will be reduced Y, cpu consumption. For example, key conflicts, key management, etc. one key must consume less resources than multiple keys)

2. What are the advantages of string over hash?

1. The field of hash has no expired function. It can only set the expiration time of the key, and cannot set the expiration time for a single field (so the hash of redis cannot be used as a distributed lock, but only a string)
2. The hash structure does not support binary commands, while the string type supports (setbit, getbit, bitcount...)
3. In a redis cluster, you cannot use hash to distribute data. In redis3 In the custer cluster of 0, the key value is hashed and the module is taken (the hash nest is used to calculate the crc16 hash function based on the key, and then the module is taken with HA Xi nest, which is managed based on different hardware). When the key is the same, the location must be the same, that is, it must be on a physical machine, Storage that cannot be distributed. For string type, the key is different, while for hash type, the key is the same, and the fields in the key are different. However, when clustering, the operation is based on the key, not the field


Here is just a brief introduction to the use scenario of redis. I will share with you the underlying principle and specific implementation of redis in the future.

Keywords: Redis Cache

Added by designbooks59 on Mon, 03 Jan 2022 05:58:51 +0200