This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

A hash hash is similar to a small Redis database. A hash can contain multiple key-value pairs. Each key in the hash cannot be repeated, different, and ordered

Hash is particularly useful for storing objects.

1. String structure:

[
    "user": ["camellia1":"90"."camellia2":"100"."camellia3":"90"]]Copy the code

Hash hash commands (PHP+ Linux)

1: Linux command usage

(1) : Add a key-value pair to the hash. Returns 1 on success; Returns 0 if the key already exists

Hset hash-key camellia1 90 // Returns (integerHset hash-key camellia1 120 // return (integerHset hash-key camellia2 120 // returns (integerHset hash-key lulubin1 90 // Returns 0 because the key already existsCopy the code

(2) : hget gets the value of a given key camellia.

hget hash-key camellia1              // "120"
Copy the code

(3) : Hgetall gets all the added values

hgetall hash-key
Copy the code

Output:

1) "camellia1"
2) "120"
3) "camellia2"
4) "120"
Copy the code

(4) : hdel deletes the key, returns 1 on success, 0 on failure;

Hdel hash-key camellia1 // returns (integer1)Copy the code

2: PHP use:

(1) : Add, return 1 on success, return 0 on failure;

    $res = $redis->hset("hash-key"."camellia1"."90");
$res = $redis->hset("hash-key"."camellia1"."90");
    var_dump($res); // Returns 1 successfully added$res = $redis->hset("hash-key"."camellia2"."120"); // Key already exists, set it to a different value, directly modify it. However, 0 is returned;$res = $redis->hset("hash-key"."camellia2"."150");
Copy the code

(2) : View all the keys you just added

$array = $redis->hgetall("hash-key");
    var_dump($array); /*array(2) {"camellia1"]=> string(2) "90" ["camellia2"]=> string(3) "150"} / / * /Copy the code

(3) : hget obtains the value corresponding to the key.

// Get one of the corresponding values$result = $redis->hget("hash-key".'camellia1');
    var_dump($result); / / back to 90Copy the code

(4) : hdel deletes one of the keys. Returns 1 if the previous key was in the hash, and 0 if not

res=res = res=redis->hdel(“hash-key”,”camellia1″); var_dump($res); / / returns 0

Three: Redis Hash application scenario

1: Redis hash is a mapping table of fields and values of string type. Hash is especially suitable for storing objects.

2: Stores some changed data, such as user information.

Four: Other common redis commands

The serial number

Commands and Description

1

HDEL key field1 [field2] Deletes one or more hash table fieldsCopy the code

2

HEXISTS key Field Checks whether the specified field in the hash table key exists.Copy the code

3

HGET Key field gets the value of the specified field stored in the hash table.Copy the code

4

HGETALL Key gets all the fields and values of the specified key in the hash tableCopy the code

5

HINCRBY key field INCREMENT Specifies the integer value of the specified field in the hash table plus increment increment.Copy the code

6

HINCRBYFLOAT Key field INCREMENT Specifies the floating point value of the specified field in the hash table key plus increment increment.Copy the code

7

HKEYS key Retrieves all fields in the hash tableCopy the code

8

HLEN key Gets the number of fields in the hash tableCopy the code

9

HMGET key field1 [field2] gets the values of all given fieldsCopy the code

10

HMSET key field1 value1 [field2 value2] Sets multiple field-value pairs to hash keys at the same time.Copy the code

11

HSET Key field Value Sets the value of field in the hash table key to value.Copy the code

12

HSETNX Key Field Value Sets the value of a hash table field only when the field does not exist.Copy the code

13

HVALS key Retrieves all values in the hash tableCopy the code

14

HSCAN key cursor [MATCH Pattern] [COUNT COUNT] Iterates key and value pairs in the hash table.Copy the code

Redis HASH HASH HASH HASH HASH HASH HASH HASH HASH HASH HASH HASH HASH

For good suggestions, please enter your comments below.

Welcome to my blog guanchao.site

Welcome to applets: