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?
- 421 # hashes is encoded in a special it (much more memory efficient) when they
- 422 # has at Max a given numer of elements, and the biggest element does not
- 423 # Exceed a given threshold. Can configure this limits with the following
- 424 # Configuration directives.
- 425 Hash-max-zipmap-entries 512
- 426 Hash-max-zipmap-value 64
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?
- #include "Redisclient.h"
- #include "Tests/functions.h"
- #include <iostream>
- #include <boost/date_time.hpp>
- #define 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");
- Test ("Hset & Hget & Hsetnx");
- {
- //Hset:key, field, value
- Out (C.hset ("Favorites", "Taobao", "www.taobao.com"));
- Out (C.hset ("Favorites", "Taobao", "www.taobao.com#"));
- Out (C.hget ("Favorites", "Taobao"));
- Out (C.hsetnx ("Favorites", "Taobao", "www.taobao.com"));
- Out (C.hget ("Favorites", "Taobao"));
- }
- Test ("Hmset & Hmget & Hgetall & Hdel & Hexists");
- {
- Redis::client::string_vector fields, values, getvalues;
- Fields.push_back ("Tmall");
- Fields.push_back ("Alibaba");
- Values.push_back ("www.tmall.com");
- Values.push_back ("www.1688.com");
- C.hmset ("Favorites", fields, values);
- //Wrong type, error
- C.set ("String_key", "string_value");
- //c.hmset ("String_key", fields, values);
- Fields.push_back ("Etao");
- C.hmget ("Favorites", Fields, getvalues);
- Out (Getvalues.size ());
- For (int i=0; i<getvalues.size (); ++i) {
- Out (Getvalues[i]);
- }
- Redis::client::string_pair_vector pairs;
- C.hgetall ("Favorites", pairs);
- Out (Pairs.size ());
- For (int i=0; i<pairs.size (); ++i) {
- Out (Pairs[i].first);
- Out (Pairs[i].second);
- }
- Out (C.hset ("Favorites", "Etao", "www.etao.com"));
- Out (C.hlen ("Favorites"));
- Pairs.clear ();
- C.hdel ("Favorites", "Etao");
- C.hdel ("Favorites", "Koubei");
- C.hgetall ("Favorites", pairs);
- Out (Pairs.size ());
- For (int i=0; i<pairs.size (); ++i) {
- Out (Pairs[i].first);
- Out (Pairs[i].second);
- }
- Out (C.hexists ("Favorites", "Taobao"));
- Out (C.hexists ("Favorites", "Koubei"));
- }
- Test ("Hincrby & Hkeys & Hvals");
- {
- Out (C.hset ("lists", "age", "20"));
- Out (C.hincrby ("lists", "age", 5));
- Redis::client::string_vector keys;
- C.hkeys ("lists", keys);
- For (size_t i=0; i<keys.size (); ++i) {
- Out (Keys[i]);
- }
- Redis::client::string_vector Vals;
- C.hvals ("lists", Vals);
- For (size_t i=0; i<keys.size (); ++i) {
- Out (Vals[i]);
- }
- }
- }
Redis:hash data types and operations