Common Commands (Hash)

“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

What is redis hash data structure?

  1. The Redis hash data structure is basically an updated version of string. It upgrades the value type in the Key value of string data structure to hash, which is the same structure as Java hash.
Map<String, HashMap<String.Object>> hash=new HashMap<String,HashMap<String.Object> > ();Copy the code
  1. Storage size per hash: can store 2 (32-1) square key-value pairs (over 4 billion)

Hash structure Classic scenario: Storing Java objects

Store a Product object into the Redis hash structure

@Data
public class Product {
    Id / / commodities
    private Long id;
    // Product name
    private String name;
    // Commodity prices
    private Integer price;
    // Product details
    private String detail;
}
Copy the code

Hset – set values

Syntax: hset key field value

Set the value of field in hash table key to value.

127.0. 01.:6379> hset product:100 name iphone11
(integer) 1
Copy the code

Hget – get the value

Syntax: hget key field

Gets the value of the specified field stored in the hash table.

127.0. 01.:6379> hget product:100 name
"iphone11"
Copy the code

Hmset – Batch add

Syntax: hmset key field1 value1 [field2 value2]

Set multiple field-value pairs into the hash key at the same time.

127.0. 01.:6379> hmset product:100 price 5000 detail "I love iphone"
OK
Copy the code

Hmget – Batch fetch

Syntax: hmget key field1 [field2 field3…

Gets the values of all given fields

127.0. 01.:6379> hmset product:100 price 5000 detail "I love iphone"
OK
127.0. 01.:6379> hmget product:100 name price detail
1) "iphone11"
2) "5000"
3) "I love iphone"
Copy the code

Hkeys – Gets all the field values in the hash

Syntax: hkeys key

Gets all field values for the specified hash

127.0. 01.:6379> hkeys product:100
1) "name"
2) "price"
3) "detail"
Copy the code

Hvals – Gets all values in the hash

Syntax: hVALS key

Gets all values of the specified hash

127.0. 01.:6379> hvals product:100
1) "iphone11"
2) "5000"
3) "I love iphone"
Copy the code

Hgetall – Obtains all field and value values in the hash

Syntax: hgetall key

Gets all the fields and values of the specified hash

127.0. 01.:6379> hgetall product:100
1) "name"
2) "iphone11"
3) "price"
4) "5000"
5) "detail"
6) "I love iphone"
Copy the code

Hlen – Gets the number of elements in the hash

Syntax: hlen key

Gets the number of elements in the specified hash

127.0. 01.:6379> hlen product:100
(integer) 3
Copy the code

Hincrby – Integer addition

Syntax: Hincrby key field data(integer)

Appends the value of the specified field to data

127.0. 01.:6379> hincrby product:100 price 100
(integer) 5100
127.0. 01.:6379> hgetall product:100
1) "name"
2) "iphone11"
3) "price"
4) "5100"
5) "detail"
6) "I love iphone"
Copy the code

Hincrbyfloat – Float addition

Syntax: HincrbyFloat key field data

Appends the value of the specified field to data

Hexists – Checks whether the field exists

Syntax: hexists key field

Checks whether the specified field exists

127.0. 01.:6379> hexists product:100 name
(integer) 1
Copy the code

Hdel – Deletes one or more hash table fields

Syntax: hdel key field1 [field2 fiedl3…

Deletes one or more hash table fields

127.0. 01.:6379> hdel product:100 name
(integer) 1
127.0. 01.:6379> hgetall product:100
1) "price"
2) "5100"
3) "detail"
4) "I love iphone"
Copy the code

Redis distributed cache family

  • Redis Distributed Cache (1) – Redis Installation (Linux and Docker)
  • Redis Distributed cache (ii) – RDB and AOF
  • SpringBoot integrates Mybatis-Plus,Redis and Swagger
  • Redis distributed cache (4) – SpringCache integrates redis
  • Redis Distributed Cache (5) — Common command (String)
  • Redis Distributed Cache (6) — Article read volume PV Solution (String)
  • Redis Distributed Cache (7) — Distributed Global ID Solution (String)
  • Redis Distributed Cache (8) — Highly Concurrent atomic Operations (Redis+Lua)
  • Redis Distributed Cache (9) — Hacker Anti-brush Attack Solution (String)
  • The article continues to be updated…