Redis:hash data types and operations

Source: Internet
Author: User

Redis Hash is a string-type field and value mapping table. A key can correspond to more than one field, and one 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 Plaincopyprint?
    1. 421 # hashes is encoded in a special it (much more memory efficient) when they
    2. 422 # has at Max a given numer of elements, and the biggest element does not
    3. 423 # Exceed a given threshold. Can configure this limits with the following
    4. 424 # Configuration directives.
    5. 425 Hash-max-zipmap-entries 512
    6. 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 fields are ignored.

8. Hlen


Hlen Key

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


9. hexists

Hexists key Field
View the hash table key for the existence of a given domain field .

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 incrementto 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 0before 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 Plaincopyprint?
    1. #include "Redisclient.h"
    2. #include "Tests/functions.h"
    3. #include <iostream>
    4. #include <boost/date_time.hpp>
    5. #define OUT (x) std::cout<< #x << "=" <<x<<std::endl;
    6. Boost::shared_ptr<redis::client> init_non_cluster_client ();
    7. void Test_hash (Redis::client & c);
    8. int main (int argv, char* argc[])
    9. {
    10. Boost::shared_ptr<redis::client> Shared_c;
    11. Shared_c = Init_non_cluster_client ();
    12. redis::client& C = *shared_c;
    13. Test_hash (c);
    14. return 0;
    15. }
    16. void Test_hash (Redis::client & c)
    17. {
    18. Test ("test hash type");
    19. Test ("Hset & Hget & Hsetnx");
    20. {
    21. //Hset:key, field, value
    22. Out (C.hset ("Favorites", "Taobao", "www.taobao.com"));
    23. Out (C.hset ("Favorites", "Taobao", "www.taobao.com#"));
    24. Out (C.hget ("Favorites", "Taobao"));
    25. Out (C.hsetnx ("Favorites", "Taobao", "www.taobao.com"));
    26. Out (C.hget ("Favorites", "Taobao"));
    27. }
    28. Test ("Hmset & Hmget & Hgetall & Hdel & Hexists");
    29. {
    30. Redis::client::string_vector fields, values, getvalues;
    31. Fields.push_back ("Tmall");
    32. Fields.push_back ("Alibaba");
    33. Values.push_back ("www.tmall.com");
    34. Values.push_back ("www.1688.com");
    35. C.hmset ("Favorites", fields, values);
    36. //Wrong type, error
    37. C.set ("String_key", "string_value");
    38. //c.hmset ("String_key", fields, values);
    39. Fields.push_back ("Etao");
    40. C.hmget ("Favorites", Fields, getvalues);
    41. Out (Getvalues.size ());
    42. For (int i=0; i<getvalues.size (); ++i) {
    43. Out (Getvalues[i]);
    44. }
    45. Redis::client::string_pair_vector pairs;
    46. C.hgetall ("Favorites", pairs);
    47. Out (Pairs.size ());
    48. For (int i=0; i<pairs.size (); ++i) {
    49. Out (Pairs[i].first);
    50. Out (Pairs[i].second);
    51. }
    52. Out (C.hset ("Favorites", "Etao", "www.etao.com"));
    53. Out (C.hlen ("Favorites"));
    54. Pairs.clear ();
    55. C.hdel ("Favorites", "Etao");
    56. C.hdel ("Favorites", "Koubei");
    57. C.hgetall ("Favorites", pairs);
    58. Out (Pairs.size ());
    59. For (int i=0; i<pairs.size (); ++i) {
    60. Out (Pairs[i].first);
    61. Out (Pairs[i].second);
    62. }
    63. Out (C.hexists ("Favorites", "Taobao"));
    64. Out (C.hexists ("Favorites", "Koubei"));
    65. }
    66. Test ("Hincrby & Hkeys & Hvals");
    67. {
    68. Out (C.hset ("lists", "age", "20"));
    69. Out (C.hincrby ("lists", "age", 5));
    70. Redis::client::string_vector keys;
    71. C.hkeys ("lists", keys);
    72. For (size_t i=0; i<keys.size (); ++i) {
    73. Out (Keys[i]);
    74. }
    75. Redis::client::string_vector Vals;
    76. C.hvals ("lists", Vals);
    77. For (size_t i=0; i<keys.size (); ++i) {
    78. Out (Vals[i]);
    79. }
    80. }
    81. }

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