Introduction of hash type in Redis and detailed instructions

Source: Internet
Author: User
Tags hash redis

In Redis, a hash data type stores data that is very similar to storing a record in a MySQL database, a string of field and value mapping tables that are especially good for storing objects, but field values can only be strings and other types are not supported.

The Redis hash is a string-type field and value mapping table. Its add, delete operations are all O (1) (average).
Hashing is particularly useful for storing objects. Gencun each character of an object to a single string type. To save an object
Storing in a hash type consumes less memory and makes it easier to access the entire object. The reason for saving memory is the new
When a hash object is built, it is stored with a zipmap (also known as a small hash). This zipmap actually doesn't
is a hash table, but Zipmap can save a lot of metadata that the hash itself needs compared to the normal hash implementation
Storage overhead. Although Zipmap additions, deletions, lookups are all O (n), the number of field numbers for general objects is not
Too much. So the use of Zipmap is also very quick, that is to add the deletion average or O (1). If field or value
When the size exceeds a certain limit, Redis automatically replaces the zipmap with a normal hash implementation internally. This restriction can be
To specify in the configuration file
Hash-max-zipmap-entries #配置字段最多64个
Hash-max-zipmap-value #配置value maximum of 512 bytes

In a hash type, a key can correspond to more than one field, and a field corresponds to a value. One of the benefits of storing an object as a hash type is that it saves memory more than each field is stored separately as a string type.

The following are common operations commands for hash (hash) types:

1. "Hset key field value" Sets the hash field to the specified value, and if the key does not exist, it is created first.
2. "Hmset key field1 value1 ... fieldn valuen" sets multiple values at the same time.
3. "Hget key field" Gets the specified hash field
4. "Hmget key field1 field1 ... fieldn" gets the specified number of hash field
5. "Hincrby key field num" adds the specified hash field to the specified value.
6. "Hexists key field" To see if the specified field exists.
7. "Hdel key Field" Deletes the specified hash field.
8. "Hlen Key" returns the number of field in the specified hash.
9. "Hkeys Key" returns the field of all hashes.
10. "Hvals" returns all the value in the hash.
11. "Hgetall Key" returns all field and value in the hash.

Example

1 Hset


Sets the hash field to the specified value, and if the 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 the key does not exist, it is created first. If field already exists, returning 0,NX is
Not the meaning of exist.
Redis 127.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


Sets multiple field fields for the 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.
Redis 127.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, a null value is taken nil


5 Hmget


Gets all the specified hash filed.
Redis 127.0.0.1:6379> hmget myhash field1 field2
1) "Hello"
2) "World"
3) (nil)
Redis 127.0.0.1:6379>
Because the database is not field3, a null value is taken nil


6 Hincrby


The specified hash filed plus the given value.
Redis 127.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 case, we dropped the value of field3 from 20 to 12, which means we did a minus 8 operation.


7 hexists


Tests whether the specified field exists.
Redis 127.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>
It can be explained by the above example that field1 exists, but FIELD9 does not exist.


8 Hlen


Returns the field number of the specified hash.
Redis 127.0.0.1:6379> Hlen Myhash
(integer) 4
Redis 127.0.0.1:6379>
You can see from the example above that there are 4 field in Myhash.


9 Hdel


Deletes the field for the specified hash.
Redis 127.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>


Ten Hkeys


Returns all field for the hash.
Redis 127.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


One hvals


Returns all the value of the hash.
Redis 127.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


Hgetall


Gets all the filed and value in a hash.
Redis 127.0.0.1:6379> Hgetall Myhash
1) "Field2"
2) "World"
3) "Field"
4) "Hello"
5) "Field3"
6) "12"
Redis 127.0.0.1:6379>


Visible, all of a sudden myhash all the field and corresponding value are taken out.

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.