Home > Database > Redis > Analysis of common operation examples of Hash, the basic data type of Redis

Analysis of common operation examples of Hash, the basic data type of Redis

王林
Release: 2023-05-31 10:43:26
forward
912 people have browsed it

Redis data type Hash common operations

In Redis, the hash table is a data structure that maps string type fields and corresponding values. Particularly suitable for storing objects, each hash can store more than 4 billion key-value pairs.

Children's shoes who are familiar with python can think of it as a dictionary dict. The previous data type storage was k-v, and the hash storage is k-dict, and dict will have its own k-v.

1. hset

Assign values ​​to the fields in the hash table. If the hash table does not exist, create a new hash table and perform the hset operation.
If the field already exists in the hash table, the old value will be overwritten.

hset myhash k1 v1
Copy after login

Analysis of common operation examples of Hash, the basic data type of Redis

2. hget

Returns the value of the specified field in the hash table. If the given field or key does not exist, nil is returned.

hget myhash k1
Copy after login

Analysis of common operation examples of Hash, the basic data type of Redis

3. hmset

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

hmset myhash k2 v2 k3 v3
Copy after login

If the field already exists in the hash table, it will be overwritten.
If the hash table does not exist, an empty hash table will be created and the hset operation will be performed.

Analysis of common operation examples of Hash, the basic data type of Redis

4. hmget

Returns the value of one or more given fields in the hash table.

hmget myhash k1 k2 k3 k4
Copy after login

If the specified field does not exist in the hash table, then a nil value is returned.

Analysis of common operation examples of Hash, the basic data type of Redis

5. hgetall

Returns all fields and values ​​in the hash table.

hgetall myhash
Copy after login

Note that in the return value, immediately following each field name (field name) is the value of the field (value), so the length of the return value is twice the size of the hash table.

Analysis of common operation examples of Hash, the basic data type of Redis

6. hdel

Delete one or more specified fields in the hash table key. Non-existing fields will be ignored.

hdel myhash k2 k3 k5
Copy after login

Returns the number of successfully deleted fields, excluding ignored fields.

Analysis of common operation examples of Hash, the basic data type of Redis

7. hlen

Get the number of fields in the hash table.

hlen myhash
Copy after login

Analysis of common operation examples of Hash, the basic data type of Redis

8. hexists

Check whether the specified field of the hash table exists.

hexists myhash k1
Copy after login

If the hash table contains the given field, return 1.
If the hash table does not contain the given field, or key does not exist, return 0.

Analysis of common operation examples of Hash, the basic data type of Redis

9. hkeys

Get all fields in the hash table.

hkeys myhash
Copy after login

Contains a list of all fields in the hash table. When key does not exist, an empty list is returned.

Analysis of common operation examples of Hash, the basic data type of Redis

10. hvals

Returns the values ​​of all fields in the hash table.

hvals myhash
Copy after login

Returns a list containing all field values ​​in the hash table. When key does not exist, an empty table is returned.

Analysis of common operation examples of Hash, the basic data type of Redis

11. hincrby

Adds the specified increment value to the field value in the hash table. This increment can also be a negative number, which is equivalent to Subtraction.

If the key of the hash table does not exist, create a new hash table and execute the hincrby command.

If the specified field does not exist, the value of the field will be initialized to 0 before executing the command.

If executed on a field that stores a string value, an error will be reported.

Analysis of common operation examples of Hash, the basic data type of Redis

12. hsetnx

Assign values ​​to fields that do not exist in the hash table.

If the hash table does not exist, create a new hash table and perform hset operation.

If the field already exists in the hash table, the operation is invalid.

If the key does not exist, create a new hash table and execute the hsetnx command.

Analysis of common operation examples of Hash, the basic data type of Redis

Regarding the application of hash in redis, such as saving user information data and frequently changing information, if you do not want to use traditional k-v objects to store, you can use redis hash.

The above is the detailed content of Analysis of common operation examples of Hash, the basic data type of Redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template