table of Contents
Basic operation of hash type data
Hash type data expansion operation
hash type
Storage confusion
- The storage of object data will be cumbersome if it has more frequent update requirements.
- There are two ways to store data through string.
- or
- Make changes to the above data
---
- It's a bit similar to putting Redis in Redis
hash type
- New storage requirements: group a series of stored data to facilitate management, typical applications store object information
- Required storage structure: One storage space stores multiple key-value pair data
- Hash type : the bottom layer uses a hash table structure to achieve data storage
- Hash storage structure optimization
- If the number of fields is small, the storage structure is optimized to an array-like structure
- If the number of fields is large, the storage structure uses HashMap structure
Basic operation of hash type data
Add/modify data hset key field value retrieve data hget key fieldhgetall key delete data hdel key field1 [field2]
- Example:
Add/modify multiple data hmset key field1 value1 field2 value2 … Get multiple data hmget key field1 field2 … Get the number of fields in the hash table hlen key Get whether the specified field exists in the hash table hexists key field
Hash type data expansion operation
Get all field names or field values in the hash table hkeys keywhale key Set the numeric data of the specified field to increase the value of the specified range hincrby key field incrementhincrbyfloat key field increment
Notes on hash type data operations
- The value under the hash type can only store strings , and no other data types are allowed. There is no nesting phenomenon. If the data is not obtained, the corresponding value is (nil)
- The hash type is very close to the data storage form of the object, and can flexibly add and delete object attributes.
- But hash is not designed to save money for the storage of large numbers of objects designed, can not remember the abuse, but can not make the hash as a list of objects
- The hgetall operation can get all attributes. If there are too many internal fields, the efficiency of traversing the overall data will be very low, which may become a data access bottleneck
Hash type application scenarios
Business scenario 1
- Design and implementation of shopping cart on e-commerce website
- Business analysis
- Only analyze the redis storage model of the shopping cart
- Add, browse, change quantity, delete, empty
- Persistent synchronization between shopping cart and database (not discussed)
- Relationship between shopping cart and order (not discussed)
- Submit shopping cart: read data to generate order
- Temporary price adjustment for merchants: belonging to order level
- Not logged in user shopping cart information storage (not discussed)
- cookie storage
solution
- With the customer id as the key, each customer creates a hash storage structure to store the corresponding shopping cart information
- Store the product number as the field and the purchase quantity as the value
- Add products: add new field and value
- Browse: Traverse the hash
- Change quantity: self-increase/self-decrease, set value
- Delete product: delete field
- Empty: delete key
- Only discuss the model design in the shopping cart here
- Persistent synchronization between shopping cart and database, relationship between shopping cart and order, and shopping cart information storage for unlogged users will not be discussed
Does the current design accelerate the presentation of the shopping cart
- At present, the data is only stored in redis, which does not play a role in accelerating . The product information needs to query the database twice
- The product records in each shopping cart are saved as two fields
- field1 is dedicated to save the purchase quantity
- Naming format: product id:nums
- Save data: Value
- field2 is dedicated to save the information displayed in the shopping cart, including text description, picture address, business information, etc. ( independent hash )
- Naming format: product id:info
- Save data: json
hsetnx key field value
Business scenario 2
- Double 11 Day, sales of mobile phone recharge cards for mobile business, China Unicom, Telecom 30 yuan, 50 yuan, 100 yuan of goods introduced buying activities, each business limit rush to buy goods 1000
solution
- Use merchant id as key
- Use the id of the product participating in the panic as the field
- Take the number of commodities participating in the rush as the corresponding value
- Use devaluation to control the quantity of products when snapping up
- There are practical problems such as oversold in actual business, so I won’t discuss them here
- [Note] Redis is used in data storage design for businesses such as panic buying, restricted purchases, limited distribution of coupons, activation codes, etc.
- [Note]: Refer to the Dark Horse Redis tutorial: https://www.bilibili.com/video/BV1AE411j7Wq?t=5