This article will introduce you to the five basic types in Redis through commands and application scenarios. There are many commands and practices. I hope it will be helpful to everyone!
redis The implementation of traditional 5 big data types
Redis introduction:
Redis is an open source (BSD licensed), in-memory data structure storage system that can be used as a database, cache, and messaging middleware. It supports many types of data structures such as strings, hashes, lists, sets, sorted sets] with range queries, bitmaps, hyperloglogs and geospatial (geospatial) Index radius query. Redis has built-in replication, LUA scripting, LRU eviction, transactions and different levels of disk persistence, and through Redis Sentinel and automatic partitioning (Cluster) ) provides high availability. [Related recommendations:Redis video tutorial]
redis command query: http://www.redis.cn/commands.html
Note: redis commands are not size sensitive Write, and key is case-sensitive
Query command help:
help @type noun
Example:
Most commonly used
## set key vuue getkeySet/get multiple key values at the same time
MSET key value [key value .. .] MGET key [key ,,,]Increase and decrease the value
increment the number incr key Increase the specified integer incrby key increment Decrease the numerical decr key Decrease the specified integer decrby key decrementGet the character length
STRLEN keyDistributed lock
setnx key value set key value [EX seconds] [PX milliseconds] [NX|XX]Application scenarios
and Java data structure mapping
Map##Set one field value at a timeHSET key field value
Get one field value at a timeHGET key field
Set multiple field values at one timeHMSET key field value [fild value ...]
Get multiple field values at one timeHMGET key field [field ...]
Get all field valueshgetall key
Get all the quantities in a keyhlen
Delete a keyhdel
Command demonstration
Application scenariosIn the early days of the shopping cart, currently small and medium-sized factories can use
to add new products --> hset shopcar:uid1024 334488 1
Add new product--> hset shopcar:uid2014 334477 1
Add product quantity--> hincrby shopcar:uid1024 334477 1
product Total --> hlen shopcar:uid1024
Select all --> hgetall shopcar:uid1024
list List typelpush key value [value ...]
Add elements to the right of the list
rpush key value [value ...]
View the list
lrange key start stop
Get the elements in the list Number
llen key
Command usage
Application scenario
WeChat article subscription public account
1, [xx treasure] and [xx newspaper] published articles 11 and 22## respectively
#2. The author follows both of them. As long as they publish a new article, it will be pushed to my listlpush likearticle: uid1024 11 223. View the author All articles of your own subscription account are similar to paging. The following 0-10 is to display 10 items at a time lrange likearticle:uid1024 0 10Add element
sadd key member [member ...]Delete element
srem key member [member ...]Get all elements of the collection
smembers keyJudge whether the element is in the collection
sismember key memberGet the number of elements in the collection
scard keyFrom the collection Randomly pop up an element from the collection, the element will not be deleted
srandmember key [number]Randomly pop up an element from the collection, delete one element
spop key [Number]Set operation
Application Scenario
WeChat Lottery Mini Program1. User id, participate immediately, sadd key user id2. Display how many people have participated. There are currently 67231 people participating. scard key3. Lottery (arbitrarily select N winners from the set)srandmember key 2 random If you draw 2 people, the elements will not be deletedspop key 3 Drivers draw 3 people, the elements will be deletedLikes in WeChat Moments1. Add a like; sadd pub:msgid like user id1 like user id22. Cancel like; srem pub:msgid like user id3. Show all points Users who have liked smembers pub:msgid4. Statistics of the number of users who like it, which is the common red number waiting for likes. scard pub:msgid5. Determine whether a friend is right The author liked it, sismember pub:msgid user idWeibo friends follow social relationshipsPeople who follow togethersadd s1 1 2 3 4sadd s2 2 3 6 8sinter s1 s2Common attention: I go to someone’s Weibo and immediately get the information that is shared with someone PeopleThe people I follow also follow him (we all have the same hobbies)I follow Yu Chengdong of Huawei, Yu Chengdong also follows Zhang Zhaodong, Mr. Yu and I have the same hobbiessadd s1 1 2 3 4 5sadd s2 3 4 5 6 7sismember s1 3sismember s2 3QQ Recommend people you may knowsadd s1 1 2 3 4 5sadd s2 3 4 5 6 7// Common friendssinter s1 s2// Difference setsdiff s1 s2sdiff s2 s1zset ordered setCommon commands
1. Add an element and the score of the element to the ordered set2. Add element11. Get the ranking of elements
zrank key member
Zrevrank key member from large to small
Application scenarios
1. More product sales Sorting and displaying products
Idea: Define the product sales ranking list (sorted set), the key is goods:sellsort, and the score is the product sales quantity.
The sales volume of product number 1001 is 9, and the sales volume of product number 1002 is 15 | zadd goods:sellsort 9 1001 15 1002
A customer bought 2 more items Product 1001, the product number is 1001 loudly increase 2 | zincrby goods:sellsort 2 10001
Find the top 10 products by sales zrange goods:sellsort 0 10 withscores
2. Douyin hot search
1. Click on the video
ZINCRBY hotavi:20220203 1 八百
ZINCRBY hotavi :20220203 15 Eight Hundred 2 Hua Mulan
2. Display the top 10 items on the day
zrevrange hotavi:20220203 0 9 withscores
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of Through commands and application scenarios, we will guide you to understand the five basic types in Redis.. For more information, please follow other related articles on the PHP Chinese website!