In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you ways to add redis cache. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Redis is often used as a cache server. The advantage of cache is to reduce the pressure on the server and the speed of data query. Solve the problem of slow data response.
Add cache: use only the Hash data type of redis to add cache.
For example, cache needs to be added to the business function of the query.
1. First of all, you need to query the cache before executing the normal business logic (before querying the database). If there is no needed data in the cache, query the database.
In order to prevent the error of adding cache and affect the execution of normal business code, put the added cache code into try-catch code fast and let the program capture it automatically.
two。 Complete the query operation of the database, after the query is completed, you need to add the query data to the cache.
Code:
@ Override public List findContentByCategoryId (Long categoryId) {/ / the list of contents queried can be added to the cache for easy display. In order to ensure that adding cache errors do not affect the normal business function of the program, you can use try catch to cache try {String json = jedisClient.hget (CONTENT_LIST, categoryId + ""). If (json! = null) {List list = JsonUtils.jsonToList (json, TbContent.class); return list;}} catch (Exception e) {e.printStackTrace ();} TbContentExample example = new TbContentExample (); Criteria criteria = example.createCriteria (); criteria.andCategoryIdEqualTo (categoryId) / / using the selectByExampleWithBLOBs method will also query the contents of the content property box List list = contentMapper.selectByExampleWithBLOBs (example) / / you need to add the contents of the query to the cache after the operation is completed, because there may be an error in the process of adding the cache, so you can use try catch to throw an exception / / categoryId+ "" to convert data of type Long to try {jedisClient.hset (CONTENT_LIST, categoryId+ ", JsonUtils.objectToJson (list) of type String). } catch (Exception e) {e.printStackTrace ();} return list;}
Utility classes 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;/** * Custom response structure of Taobao Mall * / public class JsonUtils {/ / define the jackson object private static final ObjectMapper MAPPER = new ObjectMapper (); / * * convert the object into a json string. *
Title: pojoToJson
*
Description:
* @ 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 the json result set to the object type in the object * * @ param jsonData json data * @ param clazz object * @ return * / public static T jsonToPojo (String jsonData, Class beanType) {try {t = MAPPER.readValue (jsonData, beanType); return t } catch (Exception e) {e.printStackTrace ();} return null;} / * convert json data to pojo object list *
Title: jsonToList
*
Description:
* @ param jsonData * @ param beanType * @ return * / public static List jsonToList (String jsonData, Class beanType) {JavaType javaType = MAPPER.getTypeFactory (). ConstructParametricType (List.class, beanType); try {List list = MAPPER.readValue (jsonData, javaType); return list;} catch (Exception e) {e.printStackTrace ();} return null }} the above is the method of adding redis cache shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.