Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use hash type in Redis

Shulou Source: shulou.com Published: 2022-05-31 19:22:33 10月12日 Update

Redis how to use the hash type, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The hash type is particularly suitable for storing objects.

Hset: set hash field to the specified value. If key does not exist, create it first.

127.0.0.1 purl 6379 > hset myhash field hello

(integer) 1

127.0.0.1 purl 6379 > hget myhash field

"hello"

Hsetnx: set hash field to the specified value, create it first if key does not exist, and return 0 if it exists.

127.0.0.1 purl 6379 > hsetnx myhash field hello

(integer) 0

127.0.0.1 purl 6379 > hsetnx myhash field2 world

(integer) 1

127.0.0.1 purl 6379 > hget myhash field

"hello"

127.0.0.1 purl 6379 > hget myhash field2

"world"

Hmset: set multiple field of hash at the same time.

127.0.0.1 purl 6379 > hmset myhash field1 hello field2 world

OK

Hmget: gets all the specified hash field.

127.0.0.1 purl 6379 > hmget myhash field1 field2

1) "hello"

2) "world"

Hincrby: the specified hash field plus the given value.

127.0.0.1 6379 > hset myhash age 10

(integer) 1

127.0.0.1 purl 6379 > hget myhash age

"10"

127.0.0.1 6379 > hincrby myhash age 8

(integer) 18

127.0.0.1 purl 6379 > hget myhash age

"18"

Hexists: tests whether the specified field exists.

127.0.0.1 purl 6379 > hexists myhash age

(integer) 1

Hlen: returns the number of field of the specified hash.

127.0.0.1 purl 6379 > hlen myhash

(integer) 4

Hdel: deletes the field of the specified hash.

127.0.0.1 purl 6379 > hdel myhash age

(integer) 1

127.0.0.1 purl 6379 > hlen myhash

(integer) 3

Hkeys: returns all field of hash.

127.0.0.1 purl 6379 > hkeys myhash

1) "field"

2) "field2"

3) "field1"

Hvals: returns all value of hash.

127.0.0.1 purl 6379 > hvals myhash

1) "hello"

2) "world"

3) "hello"

Hgetall: get all the field and value in a hash.

127.0.0.1 purl 6379 > hgetall myhash

1) "field"

2) "hello"

3) "field2"

4) "world"

5) "field1"

6) "hello"

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Type help clear content simultaneously multiple for this object quantity article novice more knowledge industry information information channel problem demand channel enter one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Xiaomi Microsoft Huawei