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.

Redis 127.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.< Span style= "color: #800080;" >0.1:6379> hsetnx myhash field "hello "

(integer< Span style= "color: #000000;" >) 0

Redis . 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.

Redis 127.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"

Redis127.0.0.1:6379> Hget myhash Field2

"world "

Redis 127.0 . 0.1:6379> Hget myhash field3

(nil)

Redis . 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

Span style= "color: #800080;" >3) (nil)

Redis 127.0 0.1:6379>

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.