当前位置:网站首页>Redis basic usage 1

Redis basic usage 1

2022-06-10 14:13:00 tnan2522

stay redis There is 5 Type of data
string: character string
hash: hash ( Personal understanding is python Medium dict)
list: list
set: aggregate
zset: Ordered set

Delete
del key
string

string character string , With key - value For storage ,

set name lowang                    set  Set a key value pair 
mset name laowang age 10 sex  male     mset  Set key value pairs in batch 
mget name age sex                  mget  Get key value pairs in batch 
getrange name 1 2                   obtain value  And cut 
getset name laoli                   Get it name After setting name Value 
setex aaa 10 bbb 				    Set up aaa value The value of is bbb  The expiration time is 10 second 
setnx name laoluo 				    When name The key value pair is set only when the value does not exist  name laoluo  This avoids the problem of overwriting values 
strlen name                         Get current name Corresponding value Length of string 
incr age                            Can give current key Corresponding value Value number +1  The storage format is string , But you can +1 Of ,  If stored in a non numeric format , That would be a mistake 
append age 110                    	 If key non-existent , Then we will add key Key value pair , If key There is , Then we will value  Add to key Corresponding value Behind 

hash

hash Dictionaries stay redis In storage , Store in the form of a dictionary ,

hset dict name laowang                       Set up a dictionary , Only one key value pair can be set 
hmset dict name laowang age 18 sex  male         Batch set Dictionary ,key yes dict value yes  {
    "name": " Lao Wang ", "age": "18", "sex": " male "}
hget dict name                               Get the corresponding key-value  You can only get one 
hmget dic name id age                        It can be obtained in batches value
hgetall dict                                 Get all key  and  value
hexists dict name                            To get in dict In the dictionary key Whether there is , If there is , return 1, There is no return 0
hkeys dict                                   obtain dict  All of the key
hsetnx dict name laowangba                   If dict Does not exist in the key name , Then set , If there is , Then do not set 
hvals dict	                                 obtain dict All in   Of value value 

list
lpush lists laowang laoli laozhao            add to  3 individual value It's worth it lists In the list ,  The way to add this is to insert forward , The last data is at the top 
llen  lists                                  View how many data there are in the current list 
lindex lists 0                               Get list number 0 Data , Negative numbers can also be used (-1) This is the last data in the list 
rpush  lists laoluo                          Add data to the far right of the list ,  Be similar to python List in  append() Add to the end 
rpoplpush lists lists1                       take lists The last data in the list is removed and added to lists1 In the list 
lpop  lists                                  Delete the first element data in the list 
blpop  lists 10                       		 Delete the first element in the list , If there is no data in the list , Will wait for 10 second , stay 10 If any data is added, it will be deleted , No, just wait 10 Seconds to exit the command 
lrem lists 1 aaaa                            Start at the far left of the list , Delete a value of  aaaa  The data of , If  count  by 0  when , All values in the list will be aaaa Data deletion , If  count  If it's negative , Data will be found from the right to the front for deletion , The deleted value is the absolute value of a negative number 

ltrim lists 1 -1                             take 1 To -1 Data retention between , Delete data outside this interval 
linsert lists after eee ggg                  Insert data in  eee  Insert... At the back , after( Insert after element )before ( Insert before element )
lset lists 0 abcdefg                         Modify the subscript in the list as 0 Of value Value 

set

set Can't repeat

sadd  set laowang laoli 20 nan              Set up set  aggregate , add to value laowang laoli  etc. value
sunion set                                  obtain set aggregate   All in   Of value
sunion set set1                             duplicate removal , Get the data in two sets , If there are duplicates , Only once 
scard  set                                  Get the number of elements in the collection , When the set does not exist, it returns 0
srandmember  set 10                         Random access set  In the collection 10 Elements , When insufficient 10 When it's time , Get all 
sinter set                                  Get all the data 
sinter set set1                             Get data common to both sets 
srem set 10 20 laowang                      Delete... From the collection 10 20 laowang value value ,  If you don't have these elements in the collection , So it will return 0  If the value exists 1 individual , Then return to 1,( Only one... Is deleted )

smove sets set 20                           take set1  Medium  20 value  Move add to set Set the last 
sismember set 20                            Inquire about set Is there any  20  This element 
sdiff set set1                              Get the elements in the first set that are not in the second set 
sdiffstore sets set set1                    The elements in the first set that are not in the second set sadd To sets  Collection 
spop set                                    Random deletion returns a data 

zset

zadd myset 1 laowang 2 laoli 				get Ordered set  myset  Add data  1  Lao Wang   This 1  It's sort , The smaller the value, the more forward the row , When the sorting values are the same redis Sort automatically , These same values are automatically set internally  value Sort 


zrange myset 0 -1 							 Loop through the values of the current set , Get by subscript , stay redis in , If the subscript is negative, it is from right to front ,-1 On behalf of the last 
zrange myset 0 -1 withscores                 Loop the current set value and sort value ( It's the weight ),

zremrangebyrank myset 0 1                    Remove subscript 0-1 Data between 
zcard  myset                                 Get the number of elements in the collection , When the list does not exist, it returns 0
zrem   myset  aaa bbb ccc                    Delete multiple data 
zrank  myset  ccc                            obtain ccc  The index of 
zremrangebyscore myset 4 5                   Delete the score in 4  and  5 All values between 

zcount myset 0 100                           Get points in  0 - 100 The total number of all values between    

原网站

版权声明
本文为[tnan2522]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101339432872.html