当前位置: 首页 > news >正文

spring-cache concurrentHashMap 自定义过期时间

1.自定义实现缓存构建工厂

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;public class ExpiringConcurrentMapCacheFactoryBeanimplements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean {private String name = "";@Nullableprivate ConcurrentMap<Object, Object> store;private boolean allowNullValues = true;@Nullableprivate ConcurrentMapCache cache;@Setter@Getterprivate long expiringMillis = 1000*60*60*24;//默认一天public void setName(String name) {this.name = name;}public void setStore(ConcurrentMap<Object, Object> store) {this.store = store;}public void setAllowNullValues(boolean allowNullValues) {this.allowNullValues = allowNullValues;}@Overridepublic void setBeanName(String beanName) {if (!StringUtils.hasLength(this.name)) {setName(beanName);}}@Overridepublic void afterPropertiesSet() {if (store==null){store = new ConcurrentHashMap<>(256);}ExpiringConcurrentMapCache expiringConcurrentMapCache = new ExpiringConcurrentMapCache(this.name, store, this.allowNullValues);expiringConcurrentMapCache.setExpiringMillis(expiringMillis);this.cache = expiringConcurrentMapCache;}@Override@Nullablepublic ConcurrentMapCache getObject() {return this.cache;}@Overridepublic Class<?> getObjectType() {return ExpiringConcurrentMapCache.class;}@Overridepublic boolean isSingleton() {return true;}}

2.自定义实现缓存

import lombok.Getter;
import lombok.Setter;
import org.springframework.cache.concurrent.ConcurrentMapCache;import java.util.concurrent.ConcurrentMap;public class ExpiringConcurrentMapCache extends ConcurrentMapCache {@Setter@Getterprivate long expiringMillis = 1000*60*60*24;//默认一天public ExpiringConcurrentMapCache(String name, ConcurrentMap<Object, Object> store, boolean allowNullValues) {super(name, store, allowNullValues);}// 自定义缓存值,包含数据和过期时间public static class CacheValue {@Getterprivate final Object value;private final long expirationTime;public CacheValue(Object value, long expirationTime) {this.value = value;this.expirationTime = System.currentTimeMillis() + expirationTime;}public boolean isExpired() {long l = System.currentTimeMillis();return  l > expirationTime;}}@Overridepublic void put(Object key, Object value) {// 设置过期时间,例如 5 分钟CacheValue cacheValue = new CacheValue(value, expiringMillis);super.put(key, cacheValue);}@Overrideprotected Object lookup(Object key) {CacheValue cacheValue = (CacheValue) super.lookup(key);if (cacheValue != null && !cacheValue.isExpired()) {return cacheValue.getValue();}return null;}}

3.自定义缓存配置

import com.cardcharge.share.cache.ExpiringConcurrentMapCacheFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Collections;@Configuration
public class SpringCacheConfiguration {@Value("${spring.cache.expireTimeMillis}")private Long springCacheExpireTime;@BeanExpiringConcurrentMapCacheFactoryBean defaultCache() {ExpiringConcurrentMapCacheFactoryBean cache = new ExpiringConcurrentMapCacheFactoryBean();if (springCacheExpireTime!=null){cache.setExpiringMillis(springCacheExpireTime);}cache.setName("nbCard");return cache;}@BeanCacheManager cacheManager(ConcurrentMapCache defaultCache) {SimpleCacheManager cacheManager = new SimpleCacheManager();cacheManager.setCaches(Collections.singletonList(defaultCache));return cacheManager;}}

4.在需要缓存的 方法上加 注解

  /*** 查所有* @param tokenInfo* @return* @throws CodeException*/@Override@Cacheable(cacheManager = "cacheManager",cacheNames = "nbCard",key = "#root.target.All_Nb_Card_Vo_Cache_Key",sync = true)public List<NbCardVo> findByRoleAll(TokenInfoDto tokenInfo) throws CodeException {ExecutorService executorService = Executors.newFixedThreadPool(16);//开启固定线程List<NbCardVo> result = new CopyOnWriteArrayList<>();

5.修改的缓存上面加注解

 @Override@Transactional(rollbackFor = Exception.class)@CacheEvict(cacheManager = "cacheManager",cacheNames = "nbCard",key = "#root.target.All_Nb_Card_Vo_Cache_Key")public void purchaseUpdate(PurchaseUpdateNbCardBasicInfo nbCardDto, TokenInfoDto tokenInfo) throws CodeException

http://www.lryc.cn/news/486010.html

相关文章:

  • 解析传统及深度学习目标检测方法的原理与具体应用之道
  • shell数组
  • 高斯混合模型回归(Gaussian Mixture Model Regression,GMM回归)
  • 【3D Slicer】的小白入门使用指南八
  • 【流量分析】常见webshell流量分析
  • 基于树莓派的边缘端 AI 目标检测、目标跟踪、姿态估计 视频分析推理 加速方案:Hailo with ultralytics YOLOv8 YOLOv11
  • Java在算法竞赛中的常用方法
  • Vulnhub靶场案例渗透[10]- Momentum2
  • Spark RDD中常用聚合算子源码层面的对比分析
  • 计算机网络 (6)物理层的基本概念
  • 快速上手:Docker 安装详细教程(适用于 Windows、macOS、Linux)
  • kafka消费者出现频繁Rebalance
  • rk3399开发环境使用Android 10初体验蓝牙功能
  • ASP.NET 部署到IIS,访问其它服务器的共享文件 密码设定
  • 将自定义函数添加到MATLAB搜索路径的方法
  • 云原生之运维监控实践-使用Telegraf、Prometheus与Grafana实现对InfluxDB服务的监测
  • 什么是MySQL,有什么特点
  • 初始化mysql5.7
  • C# 字典应用
  • CDH安装与配置及相关大数据组件实践
  • fastapi 调用ollama之下的sqlcoder模式进行对话操作数据库
  • YOLO系列基础(六)YOLOv1原理详解,清晰明了!
  • LeetCode100之环形链表(141)--Java
  • 【ict基础软件赛道】真题-50%openEuler
  • <AI 学习> 下载 Stable Diffusions via Windows OS
  • 计算机图形学在游戏开发中的应用
  • 【CubeMX-HAL库】STM32H743II——SDRAM配置所遇问题
  • mac上使用docker搭建gitlab
  • 二维数组操作
  • uniapp设置tabBar高斯模糊并设置tabBar高度占位