In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-10-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the method of redis caching hot data for everyone. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
For hot data (data that is frequently queried but not frequently modified or deleted), redis caching is preferred, and redis performs very well.
Because redis is an in-memory database, the resources it occupies are very precious, so store redis data as little as possible.
You can set the expiration date of the cache, usually one day, which can be adjusted according to the actual situation. This way to clear the data that is not commonly used in the cache.
The code can be set like this:
Set expire when storing redis data and reset expire when fetching.
If the data is not retrieved and reset within the expiration time, the redis data is erased.
public TbItem getTbItemByid(Long itemid) { //Add redis cache Jedis jedis = null; try { if(itemid != null){ jedis = jedisPool.getResource(); String jedisRes = jedis.get(ITEM_INFO_KEY+":"+itemid+":BASE"); if(StringUtils.isNotBlank(jedisRes)){ //reset expire when fetching redis data jedis.expire(ITEM_INFO_KEY+":"+itemid+":BASE", ITEM_INFO_KEY_EXPIRE); return JsonUtils.jsonToPojo(jedisRes, TbItem.class); } } } catch (Exception e) { e.printStackTrace(); }finally{ //close jedis, actually return to the connection pool jedis.close(); } TbItem tbItem = itemMapper.selectByPrimaryKey(itemid); try { if(tbItem!= null){ jedis = jedisPool.getResource(); jedis.set(ITEM_INFO_KEY+":"+itemid+":BASE", JsonUtils.objectToJson(tbItem)); //set expire when storing redis data jedis.expire(ITEM_INFO_KEY+":"+itemid+":BASE", ITEM_INFO_KEY_EXPIRE); } } catch (Exception e) { e.printStackTrace(); }finally{ //close jedis, actually return to the connection pool jedis.close(); } return tbItem; } About redis cache hot data method to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.