Redis Detailed: Hashes data types and operations

Source: Internet
Author: User

Redis Hash is a string-type field and value mapping table. Its add, delete operations are O (1) (average). Hash is particularly useful for storing objects. Compared to Gencun each word of an object into a single string type. Storing an object in a hash type consumes less memory and makes it easier to access the entire object. The reason for saving memory is that when a new hash object is created, it is stored with Zipmap (also known as small hash). This zipmap is not actually hash table, but zipmap compared to the normal hash implementation can save a lot of the hash itself needs some metadata storage overhead. Although Zipmap's additions, deletions, and lookups are all O (n), there are not too many field numbers for general objects. So the use of Zipmap is also very fast, that is, add delete average or O (1). If the size of field or value exceeds a certain limit, Redis automatically replaces the zipmap with the normal hash implementation internally. This restriction can be specified in the configuration file

Hash-max-zipmap-entries #配置字段最多64个.

Hash-max-zipmap-value #配置value最大为512字节.

  1, Hset

Sets the hash field to the specified value, and if key does not exist, it is created first.

Redis127.0.0.1:6379> hset myhash field1 Hello
(integer) 1
Redis 127.0.0.1:6379>

  2, Hsetnx

Sets the hash field to the specified value, and if key does not exist, it is created first. If field already exists, returning 0,nx is the meaning of not exist.

Redis127.0.0.1:6379> hsetnx myhash field "Hello"
(integer) 1
Redis 127.0.0.1:6379> hsetnx myhash field "Hello"
(integer) 0
Redis 127.0.0.1:6379>

The first execution was successful, but the second execution of the same command failed because the field already exists.

 3, Hmset

Set multiple fields of hash at the same time.

Redis127.0.0.1:6379> hmset myhash field1 Hello field2 World
Ok
Redis 127.0.0.1:6379>

 4, Hget

Gets the specified hash field.

Redis127.0.0.1:6379> hget myhash field1
"Hello"
Redis 127.0.0.1:6379> hget myhash field2
"World"
Redis 127.0.0.1:6379> hget myhash field3
(nil)
Redis 127.0.0.1:6379>

Because the database is not field3, it is a null value of nil.

 5, Hmget

Gets all the specified hash filed.

Redis127.0.0.1:6379> hmget myhash field1 field2 field3
1) "Hello"
2) "World"
3) (nil)
Redis 127.0.0.1:6379>

Because the database is not field3, it is a null value of nil.

6, Hincrby

Specifies the hash filed plus the given value.

Redis127.0.0.1:6379> hset myhash field3 20
(integer) 1
Redis 127.0.0.1:6379> hget myhash field3
"20"
Redis 127.0.0.1:6379> hincrby myhash field3 -8
(integer) 12
Redis 127.0.0.1:6379> hget myhash field3
"12"
Redis 127.0.0.1:6379>

In this example, we reduced the value of field3 from 20 to 12, which was done with a minus 8 operation.

 7, Hexists

Tests whether the specified field exists.

Redis127.0.0.1:6379> hexists myhash field1
(integer) 1
Redis 127.0.0.1:6379> hexists myhash field9
(integer) 0
Redis 127.0.0.1:6379>

The above example shows that field1 exists, but FIELD9 does not exist.

 8, Hlen

Returns the number of field for the specified hash.

Redis127.0.0.1:6379> Hlen Myhash
(integer) 4
Redis 127.0.0.1:6379>

You can see that there are 4 field Myhash in the previous example.

 9, Hdel

Returns the number of field for the specified hash.

Redis127.0.0.1:6379> Hlen Myhash
(integer) 4
Redis 127.0.0.1:6379> hdel myhash field1
(integer) 1
Redis 127.0.0.1:6379> hlen Myhash
(integer) 3
Redis 127.0.0.1:6379>

 10, Hkeys

Returns all field of the hash.

Redis127.0.0.1:6379> Hkeys Myhash
1) "Field2"
2) "field"
3) "field3"
Redis 127.0.0.1:6379>

Indicates that there are 3 field in this hash.

  11, Hvals

Returns all the value of a hash.

Redis127.0.0.1:6379> hvals Myhash
1) "World"
2) "Hello"
3) "12"
Redis 127.0.0.1:6379>

Indicates that there are 3 field in this hash.

  12, Hgetall

Get all the filed and value in a hash.

Redis127.0.0.1:6379> Hgetall Myhash
1) "Field2"
2) "World"
3) "field"
4) "Hello"
5) "Field3"
6) "12"
Redis 127.0.0.1:6379>

As you can see, all the field in Myhash and the corresponding value are taken out.

Redis Detailed: Hashes data types and operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.