Store Java Bean Object Business Profiling (Hash)

“This is the 8th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Java objects are usually stored in strings. Why hash?

Redis stores Java objects, usually strings or hashes. When do I use String?

  • StringEveryday application inFrequent read operationIs stored in JSON format, that is, Java objects are converted to JSON and then stored in Redis.
  • HashEveryday application inFrequent writeWhen an attribute of the object is frequently modified, using hash can reduce the consumption of network resources for the large object. We only need to modify an attribute, while using string data structure, each modification needs to be changedThe entire objectIt is passed to the Redis server and converted to JSON storage. If the string is very large, it will consume more than hashNetwork resources.
    • If you hash, you can targetA property is modified individuallyTo modify the entire object without using the serial number. For example,Inventory, price, number of concerns, number of commentsStore hash results when changes are frequent.

Case study: Store commodity data

Scenario 1: Creating a commodity

Commodity data needs to be modified frequently and all are hash structured.

@Autowired
private RedisTemplate redisTemplate;

@PostMapping(value = "/create")
public void create(Product obj) {
    / / TODO advanced db
    // Create an item, add the data to the database first, then store it in Redis
    String key="product:"+1000;
    // Convert attributes and values in Object to Map objects
    Map<String, Object> map=this.objectToMap(obj);
    PutAll = hmset putAll = hmset
    this.redisTemplate.opsForHash().putAll(key,map);
    / / query
    Object name=this.redisTemplate.opsForHash().get(key,"name");
    log.info("name={}",name);
    Object price=this.redisTemplate.opsForHash().get(key,"price");
    log.info("price={}",price);
    Object detail=this.redisTemplate.opsForHash().get(key,"detail");
    log.info("detail={}",detail);

}

/** * Convert attributes and values in Object to Map */
public  Map<String, Object> objectToMap(Object obj)  {
    Map<String, Object> map = newHashMap<String,Object>(); Class<? > clazz = obj.getClass();for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Object value = null;
        try {
            value = field.get(obj);
        } catch (IllegalAccessException e) {
            log.error(e.getMessage());
        }
        map.put(fieldName, value);
    }
    return map;
}
Copy the code

Scenario 2: Commodity prices rise

To modify the price of goods, we just need to use the hash structure and just modify the price parameters.

@PostMapping(value = "/addPrice")
public void addPrice(int id,int price) {
    String key="product:"+id;
    // Increment equals hincrby
    this.redisTemplate.opsForHash().increment(key,"price",price);
    Object price2=this.redisTemplate.opsForHash().get(key,"price");
    log.info("price={}",price2);
}

Copy the code

Redis distributed cache family

  • Redis Distributed Cache (1) – Redis Installation (Linux and Docker)
  • Redis Distributed cache (ii) – RDB and AOF
  • SpringBoot integrates Mybatis-Plus,Redis and Swagger
  • Redis distributed cache (4) – SpringCache integrates redis
  • Redis Distributed Cache (5) — Common command (String)
  • Redis Distributed Cache (6) — Article read volume PV Solution (String)
  • Redis Distributed Cache (7) — Distributed Global ID Solution (String)
  • Redis Distributed Cache (8) — Highly Concurrent atomic Operations (Redis+Lua)
  • Redis Distributed Cache (9) — Hacker Anti-brush Attack Solution (String)
  • Redis Distributed Cache (10) common Commands (Hash)
  • The article continues to be updated…