Redis type of Hash

 Redis hash command

The following table lists the basic redis hash related commands:

 

No. Command and description
1 HDEL key field1 [field2] 
delete one or more of the hash table field
2 HEXISTS key field 
to view the hash table key, the specified field is present.
3 HGET key field 
acquires the specified field values stored in the hash table.
4 HGETALL key 
to get all the specified key fields and values in the hash table
5 HINCRBY key field increment 
the integer value of the specified field in the hash table key plus delta increment.
6 HINCRBYFLOAT key field increment 
hash table key field specified floating-point value plus the delta increment.
7 HKEYS key 
to get all the fields in the hash table
8 HLEN key 
to obtain the number of fields in the hash table
9 HMGET key field1 [field2] 
Gets all values given field
10 HMSET key field1 value1 [field2 value2] 
simultaneously a plurality of field-value (Domain - value) provided to the key in the hash table.
11 HSET key field value 
will be the value of the hash table key field field is set to value.
12 HSETNX key field value 
only if the absence of the field field, the field setting value of the hash table.
13 HVALS key 
to obtain the hash table of all values
14 HSCAN key cursor [MATCH pattern] [  COUNT count]
iterative hash table of key-value pairs.

 Examples are as follows:

127.0.0.1:6379> HSET user name zhangsan
(integer) 1
127.0.0.1:6379> HGETALL user
1) "name"
2) "zhangsan"
127.0.0.1:6379> hest user age 38
(error) ERR unknown command `hest`, with args beginning with: `user`, `age`, `38`,
127.0.0.1:6379> hset user age 38
(integer) 1
127.0.0.1:6379> HGETALL user
1) "name"
2) "zhangsan"
3) "age"
4) "38"
127.0.0.1:6379> HGET user name
"zhangsan"
127.0.0.1:6379> hmget name age
1) (nil)
127.0.0.1:6379> hmget user name age
1) "zhangsan"
2) "38"
127.0.0.1:6379> HLEN user
(integer) 2
127.0.0.1:6379> HEXISTS user name
(integer) 1
127.0.0.1:6379> HKEYS user
1) "name"
2) "age"
127.0.0.1:6379> HVALS user
1) "zhangsan"
2) "38"
127.0.0.1:6379> HINCRBY user age 2
(integer) 40
127.0.0.1:6379> HVALS user
1) "zhangsan"
2) "40"
127.0.0.1:6379>

 hash type scenarios:

 

 

 A simple demonstration:

127.0.0.1:6379> hset u1 g01 100 g02 200
(integer) 2
127.0.0.1:6379> HGETALL u1
1) "g01"
2) "100"
3) "g02"
4) "200"
127.0.0.1:6379> HMSET u2 g01 100 g02 100
OK
127.0.0.1:6379> HGETALL u2
1) "g01"
2) "100"
3) "g02"
4) "100"
127.0.0.1:6379> hset u1 g03 5
(integer) 1
127.0.0.1:6379> HGETALL u1
1) "g01"
2) "100"
3) "g02"
4) "200"
5) "g03"
6) "5"
127.0.0.1:6379> hdel u1 g01
(integer) 1
127.0.0.1:6379> HGETALL u1
1) "g02"
2) "200"
3) "g03"
4) "5"
127.0.0.1:6379> HINCRBY g02 100
(error) ERR wrong number of arguments for 'hincrby' command
127.0.0.1:6379> HINCRBY u1 g02 100
(integer) 300
127.0.0.1:6379> HGETALL u1
1) "g02"
2) "300"
3) "g03"
4) "5"
127.0.0.1:6379> HMSET u3 g01:nums 100 g01:info {.....}
OK
127.0.0.1:6379> HGETALL u3
1) "g01:nums"
2) "100"
3) "g01:info"
4) "{.....}"
127.0.0.1:6379> HMSET u3 g02:nums 200 g02:info {.....}
OK
127.0.0.1:6379> HGETALL u3
1) "g01:nums"
2) "100"
3) "g01:info"
4) "{.....}"
5) "g02:nums"
6) "200"
7) "g02:info"
8) "{.....}"
127.0.0.1:6379>

Solution
customer id as the key, each customer create a hash cart storage structure for storing information corresponding
The product number as a field, the number of purchase as a value stored
add items: Append a new field with the value
Views: traverse hash
to change the number: increment / decrement, set the value value
remove items: delete Field,
empty: delete key

When saving business information when shopping cart, you can save business information into a Hash, Hash to keep the mark seed cart on it

Business Scene
Double 11 Day, sales of mobile phone business prepaid card 30 yuan, 50 yuan Mobile, China Unicom, Telecom, launched 100 yuan commodity buying activities, each business
limit rush to buy goods 1000

 

 

 

127.0.0.1:6379> hmset p01 c30 1000 c50  1000
OK
127.0.0.1:6379> HINCRBY p01 c30 -20
(integer) 980
127.0.0.1:6379> HGETALL p01
1) "c30"
2) "980"
3) "c50"
4) "1000"
127.0.0.1:6379>

Solution
to merchant id as Key
will be involved in buying merchandise id as Field,
the number of items involved in panic buying as the corresponding value
Use drop value when buying controlled Quantity

Note hash type of data manipulation 

H l value under ash type can only store strings, not allowed to store other data types, nesting phenomenon does not exist. If the data is not acquired, the corresponding value of Notes (nil) hash operations on data type
l each hash can store two 32 - 1 value pairs
l hash form of data storage very close to the type of object, and can flexibly add and delete object properties. But hash is not designed to store large amounts of objects designed, can not remember the abuse, but can not use the hash as a list of objects
l hgetall operation can get all the properties, if too much internal field, traversing the overall efficiency of the data will be very low, It may become a bottleneck in data access 

Published 407 original articles · won praise 2 · Views 6788

Guess you like

Origin blog.csdn.net/qq_29860591/article/details/104854032