1, the introduction of
All data structures in Redis use a unique string key to obtain the corresponding value data. Redis has five basic data structures, which are:
- String (string)
- List (list)
- Hash (dictionary)
- Set
- Zset (Ordered set)
List, set, Hash, and zset are container data structures that share the following two general rules:
- Create if not exists: Creates a container if it does not exist
- Drop if no elements: If there are no elements in the container, the container is immediately dropped to free the memory
This article is about the hash of Redis’ five basic data structures
2. Introduction to Hash (dictionary)
2.1 Internal structure of hash(dictionary)
Redis hash(dictionary) is equivalent to the Java language HashMap, which is an unordered dictionary distributed according to hash values, and the internal elements are stored as key-value pairs.The implementation of hash(dictionary) is the same as the structure of HashMap (JDK1.7) in Java. Its data structure is also a two-dimensional structure consisting of array and linked list. The node elements are hashed on the array, and the linked list is concatenated on the array nodes if a hash collision occurs.
2.2 Hash (Dictionary) Expansion
Redis hash(dictionary) stores values that can only be string values, and the expansion is different from Java HashMap. In Java, HashMap is completed at one time during expansion, while Redis adopts progressive Rehash strategy in pursuit of high performance considering that its core access is a performance problem of single thread. Progressive rehash means that it is not done once, it is done multiple times, so the old hash structure needs to be factoring, so the hash(dictionary) in Redis will have both the old and new hash structures, and after the rehash is completed, that is, after the old hash has been moved to the new hash, The new hash completely replaces the old hash in function.
2.3 Usage scenarios of Hash (dictionary)
A hash(dictionary) is used to store information about an object. A hash represents an object, a key of the hash represents an attribute of the object, and the value of the key represents the value of the attribute. The hash structure, unlike strings, does not require the entire object to be serialized and stored. This allows partial fetching at fetch time. So in contrast, hash(dictionary) has the following advantages and disadvantages:
- Read Can be partially read to save network traffic
- Storage consumes more storage than a single string
3 Hash (dictionary) instructions
3.1 Common Hash (Dictionary) Instructions
Hset -> hash(dictionary) inserts the value. If the dictionary does not exist, create key to represent the name of the dictionary, field equals key, and value is the value of key
hset key field value
Hmset -> batch set value
hmset key field value [field value …]
Example:
7.0.0.1:6379> hset book Java" Thinking in Java" # String containing Spaces requires "wrap (integer) 1 127.0.0.1:6379> hset book python" python Code "(INTEGER) 1 127.0.0.1:6379> hset book c" The best of C" (integer) 1 127.0.0.1:6379> hmset book go "concurrency in Go "mysql "high performance mysqlCopy the code
Hget -> get the value of the specified key in the dictionary
hget key field
Hgetall -> getall keys and values in the dictionary, newline output
hgetall key
Example:
127.0.0.1:6379> hget book java
"Thinking in Java"
127.0.0.1:6379> hgetall book
1) "java"
2) "Thinking in Java"
3) "python"
4) "Python code"
5) "c"
6) "The best of c"
Copy the code
Hlen -> Get the number of keys for the specified dictionary
hlen key
For example:
127.0.0.1:6379> hlen book
(integer) 5
Copy the code
3.2 Hash (dictionary) Using tricks
You can use incr and incrby to auto-add strings whose values are integers. You can also auto-add a single child key if it is an integer in a hash structure. Hincrby -> Add to the integer value of a key in the hash(dictionary)
**hincrby key field increment **
127.0.0.1:6379> hset liziba money 10
(integer) 1
127.0.0.1:6379> hincrby liziba money -1
(integer) 9
127.0.0.1:6379> hget liziba money
"9"
Copy the code
Note that an error is reported if it is not an integer.
127.0.0.1:6379> hset liziba money 10.1
(integer) 1
127.0.0.1:6379> hincrby liziba money 1
(error) ERR hash value is not an integer
Copy the code