编程知识 cdmana.com

5. Redis hash application scenarios and API analysis

Preface

Redis There are five basic data structures , The original intention is to use these data structure scenarios and API Put it in an article , But I finished the last one Redis character string API、 Use scenario analysis , I gave up the idea again . Yes , I'll take them apart . On the one hand, it is convenient to change later , It won't be that long , On the other hand, it's very convenient to split it out , Is that so? , Good guys . Don't you think I'm so intimate yet .
 be moved

summary

Hash I think my brothers are familiar with it , image JAVA Medium HashMap( I will only JAVA). Everyone is right. Hash May be called differently , It could be a hash 、 Dictionaries 、 Associative arrays and so on .
stay Redis in ,Hash Type means that the key corresponding value itself is a key value pair structure , similar value={{field,value}. Pictured
Hash

1 Common commands

1.1 Set the value

 ##  Format 
 hset key field value
 ##  For the user 1 Add a pair of field-value, Successfully returns 1, The opposite is 0
 hset user:1 name test

1.2 Set the value -hsetnx

 ##  Format , This order follows String Of set、setnx Logic is the same , But its scope changes from bond to field, If you don't understand, you can read the last one 
 hsetnx key_name field value
 ##  Executing the following command again will not succeed , because name  It already exists 
 hsetnx user:1 name test

1.3 Get value

 ##  Format , It needs to be developed at the same time key and field
 hget key field
 ##  obtain user:1key Below name
 hget user:1 name

1.4 Delete field

 ##  Format , Multiple can be deleted field
 hdel key field [field ...]
 ##  Delete user:1 below name and age
 hdel user:1 name age

1.5 Statistics field The number of

 ##  Format 
 hlen key
 ##  Statistics user:1field The number of 
 hlen user:1

1.6 Batch setting / obtain field-value

 ##  Format 
 hmget key field [field ...]
 hmset key field value [field value ...]
 ##  obtain user:1 Under the name and age
 hmget user:1 name age
 ##  Batch settings user:1 Of field
 hmset user:1 name test2 age 12 city guangzhou

1.7 Judge field Whether there is

 ##  Format 
 hexists key field
 ##  Judge user:1 Under the name Whether there is , There is returned 1, otherwise 0
 hexists user:1 name

1.8 Get all field

 ##  Format , Why not hfields,Antirez  You misled me 
 hkeys key
 ##  return user:1  All of them field
 hkeys user:1

1.9 Get all value

 ##  Format 
 hvals key
 ##  return user:1  All of them value
 hvals user:1

1.10 Get all field-value

 ##  Format   Try not to use this , If it's a big key Words ,field quite a lot , May let Redis It's been blocked for a long time 
 hgetall key
 ##  return user:1  All of them fiele and value
 hgetall user:1

2 Command time complexity

Old rules , It's not too much to put in a picture
 Insert picture description here

3 Application scenarios

actually Hash The essence of being is still String, We can convert our entity objects into JSON The string is then stored in , Of course, you can also store it according to the field Hash, Both patterns are OK Of . Mainly, we can consider according to the actual situation , If we have too many entity fields, it is not recommended to save Hash 了 . When our entity has a field that needs to be modified frequently, it is not recommended to save String, Because only one of the fields has been changed , It is obvious that there is Hash More reasonable . Like shopping carts , You can use the user id by key, goods id by field, The quantity of goods is value, A user's shopping cart will have multiple records .
Generally speaking, general objects use string + json Storage , Some frequently changing attributes in the object are extracted for use hash Storage , Good brothers, watch and use according to the actual scene .
On the other hand, it's storing objects . Compared with relational database storage , Normal image mysql This is displayed by line , and Hash It is shown as a single object , See the picture below .
 contrast

That's all for this issue , If there is something wrong, please leave a message in the comments area , in addition $\color{#FF0000}{ Please pay attention to 、 Please thumb up }$

Next :Redis List Use scenarios 、API analysis
Last one :Reids character string API、 Use scenario analysis

版权声明
本文为[]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224141040561q.html

Scroll to Top