Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common data structures and usage scenarios of redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the common data structure and usage scenario of redis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the data structures and usage scenarios commonly used in redis.

Official website order Daquan website: http://www.redis.cn/commands.html

1. Commands are not case-sensitive, while key is case-sensitive.

2. Help command: help @ type noun, such as: help @ list

I. the structure of big data

Clear all key (use with caution)

Flush all1, String (character type)

Commonly used in ①:

Set key valueget key

② sets / gets multiple keys at the same time

MSET key value [key value....] MGET key [key....]

Note: m:more

③ increment / decrement specified step

Increase by 1 / decrease by 1:

INCR keyDECR key

Increment / decrement specified step size:

INCRBY key incrementDECRBY key decrement

④ gets the string length

STRLEN key

⑤ distributed lock

Create set key value [Ex seconds] [PX milliseconds] [NX | XX] before setnx key value--- does not exist-to reserve the specified time unit for creation

⑥ usage scenario

Item number and order number are generated by INCR command

Whether you like the article or not

2. Hash (hash type)

Similar to the data type in java: Map

① sets one field value at a time / gets one field value at a time

HSET key field valueHGET key field

② sets multiple field values at a time / gets multiple field values at a time

HMSET key field value [field value...] HMGET key field [field....]

③ gets all field values

Hgetall key

④ gets all the quantities in a key

Hlen key

⑤ deletes a key

Hdel key field

⑥ application scenario Shopping cart in the early stage, currently available in small and medium-sized factories

Hmset cart:001 product001 1 product003 5 sumprice 500.0RMBhgetall cart:001

Temporarily decided to increase the quantity of 3 commodity product001:

Hincrby cart:001 product001 3

3. List (list type)

The list data structure is a double-ended linked list

① adds an element to the left of the list

LPUSH key value [value...]

② adds an element to the right of the list

RPUSH key value [value....]

③ View list

LRANGE key start stop

④ gets the number of elements in the list

LLEN key

⑤ application scenario

Wechat articles subscribe to official account

4. Set (collection type)

Note: elements in the collection are not duplicated.

① add element

SADD key member [member...]

② delete element

SREM key member [member...]

③ gets all the elements in the collection

SMEMBERS key

④ determines whether the element is in the collection

SISMEMBER key member

⑤ gets the number of elements in the collection

SCARD key

⑥ randomly pops up an element from the collection and does not delete it

SRANDMEMBER key [digital]

⑦ randomly pops up an element from the collection and deletes one from it.

SPOP key [digital]

Subtraction Operation of ⑧ set Amurb: a set of elements that belong to A but not to B.

SDIFF key [key...]

Intersection operation of ⑨ sets A ∩ B: a set of elements that belong to both An and B.

SINTER key [key...]

Union operation of ⑩ set AUB: the merged set of elements belonging to An or B.

SUNION key [key...]

Application scenarios:

① Wechat lucky draw Mini Program

1 user ID, immediate participation button sadd key user ID2 shows how many people have participated in the above picture 23208 people participated in the SCARD key3 lucky draw (N winners randomly selected from the set) sadd key user001 user002... SCARD key how many people participate in the SRANDMEMBER key 2 random lottery, the element does not delete SPOP key3 random lottery 3 people, the element will be deleted

② likes on Wechat moments.

Sadd page:005 user001 user005...-add likes to an article collection srem page:005 user005-A user cancels likes and removes them from the collection

③ Weibo friends follow social relationships.

People of common concern

The person that sadd user:001 user004 user005 user003sadd user:003 user005 user009 user002-- follows together, sinter user:001 user003--, the person I follow also follows him, smembers user001smembers user003.

④ QQ tweets people you might know

Difference set

Sdiff user:001 user:003

5. SortedSet (ordered set type, zset for short)

Add an element and its score to an ordered set

① add element

ZADD key score member [score member...]

② returns all elements indexed from start to stop in the order of element scores

ZRANGE key start stop [WITHSCORES]

③ gets the score of the element

ZSCORE key member

④ delete element

ZREM key member [member...]

⑤ gets the elements in the specified score range

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

⑥ increases the score of an element

ZINCRBY key increment member

⑦ gets the number of elements in the collection

ZCARD key

⑧ gets the number of elements within the specified score range

ZCOUNT key min max

⑨ deletes elements by ranking range

ZREMRANGEBYRANK key start stop

⑩ gets the ranking of elements

1. From small to big

ZRANK key member

2. From big to small

ZREVRANK key member

Application scenarios:

1. Sort and display the goods according to the sale of the goods

Idea: define the ranking of commodity sales (sorted set collection). Key is goods:sellsort and the score is the number of goods sold. The sales volume of item number 1001 is 9, and the sales volume of item number 1002 is 15zadd goods:sellsort 9 1001 15 1002. There is a customer who bought two more items 1001, item number 1001, sales volume plus 2zincrby goods:sellsort 2 1001 to seek the top 10 ZRANGE goods:sellsort 0 9 withscores.

At this point, I believe you have a deeper understanding of the "common data structures and usage scenarios 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report