Redis:hash data types and operations

Source: Internet
Author: User
Tags hash redis

The

  redis hash is a string-type field and a mapping table of value. A key can correspond to multiple fields, and a field corresponds to a value. Storing an object as a hash type can save memory more than storing each field as a string type. When a new hash object is created, it is initially 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 is configured in redis.conf as follows:

[plain] view plain copy print? 421 # hashes is encoded in a special it (much more memory efficient) when they 422 # has at Max a given numer of Elem Ents, and the biggest element does not 423 # exceed a given threshold.   You can configure this limits with the following 424 # configuration directives. 425 Hash-max-zipmap-entries 426 Hash-max-zipmap-value 64

Operation

1. Hset

Hset key field value
Set the value of the domain field in the hash table key to value. If the key does not exist, a new hash table is created and the Hset operation is performed. If the field field already exists in the hash table, the old value will be overwritten.

2. Hget


Hget key Field


Returns the value of the field specified in the hash table key.


3. Hsetnx

Hsetnx key field value
Set the value of the field field in the hash table key to value if and only if the domain field does not exist. If the field field already exists, the operation is not valid. If key does not exist, a new hash table is created and the HSETNX command is executed.

4. Hmset

Hmset key field value [field value ...]

Set multiple Field-value (domain-value) pairs to the hash table key at the same time. This command overwrites a domain that already exists in the hash table. If the key does not exist, an empty hash table is created and the Hmset operation is performed.

5. Hmget


Hmget key field [field ...]

Returns the value of one or more given fields in a hash table key. Returns a nil value if the given domain does not exist in the hash table. Because the nonexistent key is treated as an empty hash table, a hmget operation on a nonexistent key returns a table with a nil value.

6. Hgetall

Hgetall key returns all the fields and values in the hash table key. In the return value, followed by each domain name (field name) is the value of the domain (value), so the length of the return value is twice times the size of the hash table.

7. Hdel

Hdel key field [field ...] deletes one or more specified domains in the hash table key, and the nonexistent domain is ignored.

8. Hlen


Hlen Key

Returns the number of field that the hash table key corresponds to.


9. hexists

Hexists key field to view the hash table key, whether the given domain field exists.

Ten. Hkeys


Hkeys Key

Gets all the field that corresponds to the key in the hash table.


Hvals.


Hvals Key

Gets all values corresponding to the key in the hash table.


Hincrby.

Adds an increment increment to the value of the field field in the hash table key. The increment can also be negative, which is equivalent to subtracting a given field. If key does not exist, a new hash table is created and the Hincrby command is executed. If the domain field does not exist, the value of the domain is initialized to 0 before the command is executed. Executing the Hincrby command on a domain field that stores a string value will cause an error. The value of this operation is limited to a 64-bit (bit) signed numeric representation.

For more detailed information, please refer to: http://redis.readthedocs.org/en/2.4/hash.html

The following is a test program written using the Redis C + + client:

[CPP]  View plain  copy  print? #include   "redisclient.h"       #include   "Tests/functions.h"       #include  <iostream>      #include  <boost/date_time.hpp>      #define &NBSP;OUT (x)  std::cout<< #x << " = " <<x<<std::endl;       boost::shared_ptr<redis::client> init_non_cluster_client ();   void  test_hash (redis::client & c);      Int main (int argv,  Char* argc[])     {       boost::shared_ptr<redis::client > shared_c;          shared_c = init_non_cluster_ Client ();          redis::client& c = *shared_c;           test_hash (c);          return 0;  }   Void test_hash (redis::client  & c)    {       test ("Test hash type");     

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.