当前位置:网站首页>Explanation of redis database (III) redis data type
Explanation of redis database (III) redis data type
2022-04-23 15:20:00 【C chord~】
Catalog
2、incr / decr / incrby / decrby
3、lrem / lset / lindex / ltrim
5、rpush / rpushx / rpop / rpoplpush
1、hset / hget / hdel / hexists / hlen / hsetnx
3、hmset / hmget / hgetall / hkeys / hvals
Four 、Set type ( unordered set )
1、sadd / smembers / scard / sismember
2、spop / srem / srandmember / smove
5、 ... and 、Sorted Set type (zset、 Ordered set )
1、zadd / zcard / zcount / zrem / zincrby / zscore / zrank
2、zrangebyscore / zremrangebyrank / zremrrangebyscore
3、zrevrange / zrevrangebyscore / zrevrank
introduction
Redis Compared with other non relational databases, the advantage of database mainly lies in the rich data types , Write it down and learn it together redis The data type of the database
One 、String type
- String yes redis Most basic types , Maximum energy storage 512MB The data of ,String Type is binary safe , Any data can be stored 、 Such as digital 、 picture 、 Serialized objects, etc
1、set / get / append / strlen
redis-cli
exists ljm # Determine whether the key exists , There is returned 1, Otherwise return to 0
append ljm "hello" # The key does not exist , therefore append The command returns the current value The length of
append ljm " world" # The key already exists , So return after append value The length of
get ljm # adopt get Command to get the key , To judge append Result
set ljm "just do it" # adopt set Command sets a new value for the key , And cover the original value
get ljm
strlen ljm # Get specified key The character length of
2、incr / decr / incrby / decrby
exists ac # Determine whether the key exists
del ac # Delete the original key , return 1 Description delete successfully
set ac 100 # Set a new value for the key
incr ac # The value of this key is incremented 1
decr ac # The value of this key decreases 1
incrby ac 10 # The value of this key is incremented by a fixed value
decrby ac -20 # The value of this key is reduced by a fixed value
decrby ac 30
get ac # View the value of the key
set ac "hello" # The value of this key is set to string
get ac # Check the value
incr ac # Do the self growth operation ( notes : String types cannot grow by themselves )
3、getset
get ac
getset ac 0 # While getting the original value of the counter , And set it to the new value , These two operations are done atomically at the same time
get ac
4、setex
setex ac seconds 15 # Set the specified Key Expires on 15 second
ttl key # adopt ttl Command view specified key The remaining life of ( second ),-2 Indicates that it has expired ,-1 Never expire
get ac
ttl ac
ttl ac
5、setnx
del ac
setnx ac 100 # Create specified key , If the key exists, do not execute , If it doesn't exist, execute
setnx ac 150
get ac
6、mset / mget / msetnx
mset ll "hellow" jj "world" # The value of the batch setting key
mget ll jj # Batch get key values
msetnx mm 100 nn 200 # Batch setting key values , If there are existing keys, do not execute
msetnx ll 150 mm 250
Two 、List type
The element type of the list is string, Sort by insertion order , Add elements to the head or tail of the list
1、lpush / lpushx / lrange
lpush # This command creates the key and its associated List, Then... In the parameter values Insert into the header from left to right
lpushx # This command only if key In existence , take value The value is inserted into the header
lrange # Returns the elements in the specified interval in the list ,0 Represents the first element ,1 For the second element
2、lpop / llen
lpop # Remove and return the first element , Start from scratch
llen # Check the number of elements in the list
3、lrem / lset / lindex / ltrim
lrem # From the head (left) To the tail (right) Variable linked list , Delete 2 Values equal to a The elements of , The return value is the actual deleted quantity
lset # Set the index value to xxx Set the element value of to the new value xxx
lindex # Get index value as xxx The element value of .
ltrim # Keep only index values xxx To xxx The elements of
4、linsert
linsert # At the bottom of the key xxx Element before | Insert new element after
5、rpush / rpushx / rpop / rpoplpush
rpush # Insert values from left to right at the end of the table
rpushx # Executes when the specified key exists , Otherwise, do not execute
rpop # Remove and return the first element of the key , Start at the end
rpoplpush # Will key 1 The tail element xxx eject , At the same time, insert the key 2 The head of ( Atomically complete these two steps )
3、 ... and 、Hash type
- hash For storing objects . It can be named like this : Object category and ID Key name , Use fields to represent the properties of objects , Field values store property values .
- If Hash It contains very few fields , Then this type of data will only occupy a small amount of disk space . every last Hash Can be stored 4294967295 Key value pairs .
1、hset / hget / hdel / hexists / hlen / hsetnx
hset # to xxx The key setting field is xxx, The value is xxx
hget # obtain xxx key , Field is xxx Value
hdel # Delete xxx Keyed xxx Field , Successfully returns 1
hexists # Judge xxx In the key xxx Whether the field exists , There is returned 1
hlen # obtain xxx Number of fields for the key
hsetnx # to xxx Key to add a new field , Whether to execute is based on whether this field exists , Whether the key exists or not , return 1 Indicates successful execution
2、hincrby
hincrby # to xxx Keyed xxx Field value plus x
3、hmset / hmget / hgetall / hkeys / hvals
hmset key field value # Batch is xxx Key to create fields and assign values
hmget key field # Get and specify multiple field values
hgetall key # return xxx All fields of the key and their values , It's listed pair by pair
hkeys key # Just get xxx All field names in the key
hvals key # Just get xxx All field values in the key
Four 、Set type ( unordered set )
- unordered set , Element type is String type , Element is unique , Duplicate members are not allowed . Union can be performed between multiple collection types 、 Intersection and subtraction operations .
1、sadd / smembers / scard / sismember
sadd # Add one or more member elements to the collection , Member elements that already exist in the collection will be ignored . If set key non-existent , Then create a collection containing only the added elements as members
smembers # adopt smembers Command to view the inserted results , The order of output is independent of the order of insertion
scard # Get the number of members in the collection
sismember # In the judgment key xxx Whether members exist , return 0 Does not exist ,1 Indicates presence
2、spop / srem / srandmember / smove
spop # Randomly remove and return a member of the key
srem # Remove... From the key xxx、xxx、xxx member , And return the number of removed members
srandmember # This command randomly returns a member
smove # Will key 1 Of xxx Move member to key 2, Successfully returns 1, Failure to return 0
5、 ... and 、Sorted Set type (zset、 Ordered set )
- Ordered set , Element type is String, Element is unique , Can't repeat .
- Each element is associated with a double Score of type score( Said the weight ), You can sort by weight , Elemental score Can be the same .
1、zadd / zcard / zcount / zrem / zincrby / zscore / zrank
zadd # Add one or more member elements and their fractional values to an ordered set
zcard # Get the number of members in the key
zcount # The fraction satisfies the expression x <= score <= x The number of members of
zrem # Delete members xxx、xxx, Returns the number of members actually deleted
zincrby # member xxx non-existent ,zincrby The command adds the member and assumes that its initial score is 0
zscore # Get members xxx The scores of
zrank # Get members xxx Location index value of
2、zrangebyscore / zremrangebyrank / zremrrangebyscore
zrangebyscore # Get the score to satisfy the expression x <= score <= x Members of
zremrangebyrank # The delete location index satisfies the expression x <= rank <= x Members of .
zremrrangebyscore # Delete the fraction satisfaction expression x <= score <= x Members of , And returns the actual number of deletions .
3、zrevrange / zrevrangebyscore / zrevrank
zrevrange # Get and return the members in this interval in the way of location index from high to low
zrevrangebyscore # Get the score to satisfy the expression x >= score >= x Members of , And output from top to bottom .
zrevrank # Get member index
summary
String The conventional set/get operation ,value It can be String It can also be numbers . Generally do some complex counting function cache hash
here value It's storing structured objects , It is more convenient to operate one of the fields . When doing single sign on , This data structure is used to store user information , With cookieId As key, Set up 30 Minutes is the cache expiration time , It can simulate the similar session The effect of
list
Use List Data structure of , Can do simple message queuing function . There's another one , You can use lrange command , Based on redis The paging function of , Excellent performance , Good user experience .LIST It can complete the queue very well , The first in, first out principle
set
because set It's a collection of distinct values . So we can do the function of global de duplication . Why not JVM Self contained Set Deduplication ? Because our system is generally a cluster deployment , Use JVM Self contained Set, More trouble , Do you want to do a whole thing for one , Another public service , It's too troublesome
in addition , Is to use intersection 、 Combine 、 Subtraction and so on , We can calculate common preferences , All my hobbies , Their own unique preferences and other functions
sorted set
sortedset One more weight parameter score, The elements in the set can press score Arrange . Can do leaderboard application , take TOP N operation
版权声明
本文为[C chord~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406063768.html
边栏推荐
- API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
- How to design a good API interface?
- Leetcode165 compare version number double pointer string
- Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
- Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
- Educational Codeforces Round 127 A-E题解
- Machine learning - logistic regression
- Adobe Illustrator menu in Chinese and English
- like和regexp差别
猜你喜欢
8.2 text preprocessing
每日一题-LeetCode396-旋转函数-递推
Leetcode exercise - 396 Rotation function
Mysql连接查询详解
About UDP receiving ICMP port unreachable
Sword finger offer (1) -- for Huawei
Thinkphp5 + data large screen display effect
LeetCode151-颠倒字符串中的单词-字符串-模拟
Detailed explanation of MySQL connection query
How does eolink help telecommuting
随机推荐
Basic operation of circular queue (Experiment)
UML learning_ Day2
Educational Codeforces Round 127 A-E题解
January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
asp. Net method of sending mail using mailmessage
OPPO数据湖统一存储技术实践
win10 任务栏通知区图标不见了
async void 导致程序崩溃
MySQL query library size
Five data types of redis
How to design a good API interface?
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
Hj31 word inversion
TLS / SSL protocol details (30) RSA, DHE, ecdhe and ecdh processes and differences in SSL
How to write the keywords in the cover and title? As we media, why is there no video playback
How to upload large files quickly?
Grep was unable to redirect to the file
Introduction to distributed transaction Seata
买卖股票的最佳时机系列问题