Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of caching Hot spot data in redis

2025-03-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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report