Use redis for caching

Redis is often used as a cache server. The advantage of caching is to reduce the pressure on the server, and the data query speed is fast. Solve the problem of slow data response.

Add cache : add cache only with redis' Hash data type.

For example: you need to add a cache in the business function of the query

1. First, you need to query the cache before executing normal business logic (before querying the database). If there is no required data in the cache, query the database

In order to prevent errors in adding the cache and affecting the execution of normal business code, place the code for adding the cache in the try-catch code and let the program automatically capture it.

2. Complete the database query operation. After the query is completed, the queried data needs to be added to the cache.

Code:

    @Override
     public List<TbContent> findContentByCategoryId(Long categoryId) {
         // The queried content list can be added to the cache for easy display. In order to ensure that errors in adding the cache do not affect the normal business functions of the program, you can use try catch to add cache 
        try {
            String json = jedisClient.hget(CONTENT_LIST, categoryId + "");
            if (json != null) {
                List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        TbContentExample example = new TbContentExample();
        Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        // Use the selectByExampleWithBLOBs method to query the content in the content property box 
        List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);

        // After the operation is completed, the content of the query needs to be added to the cache, because the process of adding the cache may be wrong, so use try catch to throw the exception.
         // categoryId+""Convert the data of type Long into type of String 
        try {
            jedisClient.hset(CONTENT_LIST, categoryId + "", JsonUtils.objectToJson(list));
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return list;
    }

Tool class for Json conversion:

package nyist.e3.utils;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Taotao Mall custom response structure
 */ 
public  class JsonUtils {

    // Define jackson object 
    private  static  final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * Convert the object to a json string.
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            e.printStackTrace ();
        }
        return null;
    }
    
    /**
     * Convert json result set to object
     *
     * @param jsonData json data
     * @param clazz object type in the object
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }
    
    /**
     * Convert json data to pojo object list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(jsonData, javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace ();
        }
        
        return null;
    }
    
}
View Code

 

How to achieve cache synchronization?

The so-called cache synchronization means that after the addition, deletion and modification of the database is completed, the corresponding cache can be cleared. The next time the query operation is performed, a new cache is added again, so that the problem of cache synchronization is well realized.

Guess you like

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