Redis development and operation and maintenance reading notes five

Chapter 2 Understanding and Using API Part 3

Hash
type refers to the key value , such as the mapping of value={{field1,value1},...{fieldN,valueN}}
Hash valuerelationship is called field-value , where the value is the value corresponding to the field, not the value corresponding to the key. The

Hash command
sets the value
hset key field value

You can also use hsetnx for adding, which is the same as the relationship between set and setnx, but the scope is field

to get the value
hget key field


delete value
hdel key field [field...]


Calculate the number of fields
hlen key


Batch set or get field-value
hmget key field [field ...]
hmset key field value [field value ...]


Check if field exists
hexists key field


get all fields
hkeys key


get all values
whale key


Get all field-values
hgetall key

Note: When there are many hash elements, hgetall may cause Redis to block. It is recommended to obtain more than one. You can use hmget. If you must obtain all field-values, you can use the hscan command to incrementally traverse the hash type and

increment the specified number/ floating point number
hincrby key field
hincrbyfload key field


Calculate the length of the value string
hstrlen key field


Internal coding
  • ziplist: zip list. When the number of hash type elements is less than the hash-max-ziplist-entries configuration (default 512), and all values ​​are less than the hash-max-ziplist_value configuration (default 64 bytes), Redis uses ziplist as an internal implementation, which is more compact and saves money RAM
  • hashtable: Hash table. When the requirements of ziplist cannot be met, hashtable is used, because the read and write efficiency of ziplist will decrease at this time, and the time complexity of reading and writing using hashtable is O(n)


Usage scenarios
The difference between Hash type and relational database:
  • Hash is sparse, relational databases are fully structured
  • Relational databases can do complex queries, Redis is not suitable for simulating complex relational queries, and the maintenance cost is too high

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271194&siteId=291194637