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 vuuegetkeySet/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 keyIncrease the specified integer incrby key incrementDecrease the numerical decr keyDecrease the specified integer decrby key decrement
Get the character length
STRLEN keyDistributed lock
setnx key valueset key value [EX seconds] [PX milliseconds] [NX|XX]Application scenarios
and Java data structure mapping
Map##Set one field value at a time HSET 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 listrpush 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 Add element Delete element Get all elements of the collection Judge whether the element is in the collection Get the number of elements in the collection From the collection Randomly pop up an element from the collection, the element will not be deleted Randomly pop up an element from the collection, delete one element Set operation Application Scenario Common commands 11. 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 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! ! set Non-duplicate list type
A set constructed from elements that belong to A but not Bsdiff key [key ...]
Elements that belong to A and also belong to B.simter key [key ...]
The merged set of elements belonging to A or Bsunion key [key ...]
ZADD key score member [score member ...]
3. Return all elements with index from strat to stop in the order of arrival of element scores
zrange key start stop [WITHSORES]
4. Get the score of the element
zscore key member [member ...]
5. Delete Element
zrem key member [member ...]
6. Get the elements of the specified score range
zrangebyscore key min max [ WITHSCORES] [LIMIT offset count]
7. Increase the score of an element
zincrby key increment member
8. Get the set The number of elements in
zcard key
9. Obtain the number of elements within the specified score range
zcount key min max
10. Delete elements according to ranking range
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
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!