preface

Redis has five basic data structures. I wanted to put the application scenarios and API analysis of these data structures in one article, but I finished the last oneRedis string API, using scene analysisI gave up the idea again. Yes, I’ll take them apart. On the one hand, it’s convenient to change it later, and the length won’t be so long, on the other hand, it’s also very convenient to break it out, right? Good brothers. Look how sweet I am without a thumbs-up and a follow.

An overview of the

I’m sure you’re familiar with Hash tables, like the JAVA HashMap(I only know JAVA). Everyone may call a Hash differently. It could be a Hash, a dictionary, an associative array, and so on. In Redis, Hash type means that the corresponding value of a key is itself a key-value pair structure, similar to value={{field, value}. As shown in figure

1 Common Commands

1.1 set values

 # # format
 hset key field value
 Add a pair of field-values to user 1, return 1 on success, or 0 on failure
 hset user:1 name test
Copy the code

1.2 Setting Value -hsetnx

 String setnx = String setnx = String setnx = String setnx = String setnx
 hsetnx key_name field value
 Running the following command again will fail because name already exists
 hsetnx user:1 name test
Copy the code

Get the value 1.3

 ## format, need to specify both key and field
 hget key field
 Get name from user:1key
 hget user:1 name
Copy the code

1.4 remove the field

 ## format, you can delete multiple fields
 hdel key field [field .]
 ## delete name and age from user:1
 hdel user:1 name age
Copy the code

1.5 Counting the Number of Fields

 # # format
 hlen key
 ## count the number of user: 1fields
 hlen user:1
Copy the code

1.6 Batch Setting/Obtaining field-value

 # # format
 hmget key field [field .]
 hmset key field value [field value .]
 Get name and age from user:1
 hmget user:1 name age
 ## batch set user:1 field
 hmset user:1 name test2 age 12 city guangzhou
Copy the code

1.7 Checking whether the Field exists

 # # format
 hexists key field
 If user:1 exists, return 1, otherwise 0
 hexists user:1 name
Copy the code

1.8 Get all fields

 ## format, why not hfields Antirez you mislead me
 hkeys key
 ## return all fields under user:1
 hkeys user:1
Copy the code

1.9 Obtaining All Values

 # # format
 hvals key
 ## return all values under user:1
 hvals user:1
Copy the code

1.10 Obtaining all field-values

 ## format should not be used. If it is a large key, many fields will block Redis for a long time
 hgetall key
 ## return all fiele and values under user:1
 hgetall user:1
Copy the code

2 Command time complexity

As usual, I don’t think it’s too much of a picture

3 Application Scenarios

In fact, the Hash store is essentially a String. We can convert our entity object into a JSON String and store it, or we can store it in a Hash by field. Both modes are OK. We can consider the actual situation, if we have too many entity fields, it is not recommended to save Hash. When we have a field that needs to be changed frequently, we don’t recommend storing String, because we’re just changing one of those fields, and it makes more sense to store Hash. For example, for shopping cart, the user ID can be key, the item ID is field, and the item quantity is value. A user shopping cart will have multiple records. In general, the general object is stored with string + JSON, and some frequently changing attributes in the object are extracted and stored with hash. Good elder friends can use it according to the actual scene. Another aspect is storing objects. In contrast to relational database storage, which normally displays in rows like mysql, Hash displays in individual objects, as shown in the figure below.

That will do for this issue, there is not welcome good brothers comment area, additional attention and thumb up \ color # FF0000} {{attention and thumb up} attention and thumb up

Reids string API, application scenario parsing