网站后台文章栏目,写小说的网站自己做封面,做棋牌网站抓到会怎么量刑,销售找客户的方法简介
在 Spring Boot 中#xff0c;你可以使用多种方法来实现自动删除超时元素的 List 或 Map。以下是两种常见的方式#xff1a;
如果你需要简单的功能并且不介意引入外部依赖#xff0c;可以选择 Guava Cache。如果你想要更灵活的控制#xff0c;使用 Spring 的调度功能…简介
在 Spring Boot 中你可以使用多种方法来实现自动删除超时元素的 List 或 Map。以下是两种常见的方式
如果你需要简单的功能并且不介意引入外部依赖可以选择 Guava Cache。如果你想要更灵活的控制使用 Spring 的调度功能也是一个不错的选择。 1. 使用 Scheduled Task
你可以使用 Spring 的 Scheduled 注解来定期清理过期的元素。 import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;Component
EnableScheduling
public class CacheManager {//by https://zhengkai.blog.csdn.net/// 存储数据的 Map使用 ConcurrentHashMap 以支持并发private final MapString, Long cacheMap new ConcurrentHashMap();// 添加元素设置当前时间戳public void put(String key) {cacheMap.put(key, System.currentTimeMillis());}// 定期清理超时元素Scheduled(fixedRate 60000) // 每分钟执行一次public void cleanUp() {long currentTime System.currentTimeMillis();long timeout 30 * 60 * 1000; // 30 分钟超时IteratorMap.EntryString, Long iterator cacheMap.entrySet().iterator();while (iterator.hasNext()) {Map.EntryString, Long entry iterator.next();if (currentTime - entry.getValue() timeout) {iterator.remove(); // 删除超时元素}}}// 获取元素public Boolean get(String key) {return cacheMap.containsKey(key);}
}在上面的代码中我们创建了一个 CacheManager 类它使用 ConcurrentHashMap 来存储元素及其添加时间。Scheduled 注解用于定义定期清理超时元素的任务。定期任务每分钟执行一次检查并删除 30 分钟之前添加的元素。
2. 使用 Guava Cache
如果你希望更简单地管理缓存可以使用 Guava 的 Cache。它提供了过期策略的支持。 示例代码
首先确保在 pom.xml 中添加 Guava 依赖
!-- https://mvnrepository.com/artifact/com.google.guava/guava --
dependencygroupIdcom.google.guava/groupIdartifactIdguava/artifactIdversion33.3.1-jre/version
/dependency然后使用 Guava Cache
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;Component
public class CacheManager {private CacheString, String cache;PostConstructpublic void init() {// 创建一个支持过期的缓存30分钟后过期。 by https://zhengkai.blog.csdn.net/cache CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).build();}// 添加元素public void put(String key, String value) {cache.put(key, value);}// 获取元素public String get(String key) {return cache.getIfPresent(key);}
}在这个例子中我们使用 Guava 的 CacheBuilder 创建了一个具有 30 分钟自动过期策略的缓存。使用 put 方法可以添加元素而使用 getIfPresent 方法可以获取当前元素。
文章转载自: http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.jycr.cn.gov.cn.jycr.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.qqhersx.com.gov.cn.qqhersx.com http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.kkrnm.cn.gov.cn.kkrnm.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.nrchx.cn.gov.cn.nrchx.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.ysllp.cn.gov.cn.ysllp.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.hdscx.cn.gov.cn.hdscx.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.dgsr.cn.gov.cn.dgsr.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.frtb.cn.gov.cn.frtb.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn