Caffeine缓存公共模块
parent
9b3da53c6b
commit
8741c20ddb
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-caffeine</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
<version>2.9.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,98 @@
|
|||
package com.muyu.common.caffeine;
|
||||
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.muyu.common.caffeine.constents.CaffeineContent;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit; /**
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineUtils
|
||||
* @Description: 缓存工具类
|
||||
* @CreatedDate: 2024/9/26 下午2:53
|
||||
* @FilePath: com.muyu.common.caffeine
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CaffeineCacheUtils {
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
@Resource
|
||||
private SimpleCacheManager simpleCacheManager;
|
||||
|
||||
|
||||
/**
|
||||
* 车辆上线 - 新增缓存
|
||||
*/
|
||||
public void addCarCache(String vin) {
|
||||
ArrayList<CaffeineCache> caches = new ArrayList<>();
|
||||
// 从Redis中获取缓存信息
|
||||
Map<String,Object> cacheMap = redisService.getCacheMap(CaffeineContent.CAR_VIN+vin);
|
||||
cacheMap.forEach((key, value) -> {
|
||||
Cache<Object , Object> cache = Caffeine.newBuilder().build();
|
||||
cache.put(key, value);
|
||||
// 全部存储到 CaffeineCache集合
|
||||
caches.add(new CaffeineCache(vin, cache));
|
||||
});
|
||||
simpleCacheManager.setCaches(caches);
|
||||
log.info("车辆编码:{},本地缓存完成...",vin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 车辆下线 - 删除缓存
|
||||
*/
|
||||
public void deleteCarCache(String vin) {
|
||||
if (hasCarVinCache(vin)) {
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...", vin);
|
||||
return;
|
||||
}
|
||||
simpleCacheManager.getCache(vin).invalidate();
|
||||
log.info("车辆编码:{},本地缓存删除完成...", vin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取车辆信息缓存
|
||||
*/
|
||||
public Object getCarCache(String vin, String key) {
|
||||
if (hasCarVinKeyCache(vin, key)){
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin);
|
||||
return null;
|
||||
}
|
||||
return simpleCacheManager.getCache(vin).get(key).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取车辆信息缓存
|
||||
*/
|
||||
public <T> T getCarCache(String vin, String key, Class<T> type) {
|
||||
if (hasCarVinKeyCache(vin,key)){
|
||||
log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin);
|
||||
return null;
|
||||
}
|
||||
return simpleCacheManager.getCache(vin).get(key, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存存在与否
|
||||
*/
|
||||
public Boolean hasCarVinCache(String vin) {
|
||||
return ObjectUtils.isNotEmpty(simpleCacheManager.getCache(vin));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存的Key存在与否
|
||||
*/
|
||||
public Boolean hasCarVinKeyCache(String vin,String key) {
|
||||
return hasCarVinCache(vin) && ObjectUtils.isNotEmpty(simpleCacheManager.getCache(vin).get(key).get());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.common.caffeine.bean;
|
||||
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.caffeine.CaffeineCache;
|
||||
import org.springframework.cache.support.SimpleCacheManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineCacheConfig
|
||||
* @Description: Caffeine管理器
|
||||
* @CreatedDate: 2024/9/26 上午11:52
|
||||
* @FilePath: com.muyu.common.caffeine.config
|
||||
*/
|
||||
@Component
|
||||
public class CaffeineManagerBean {
|
||||
|
||||
/**
|
||||
* 创建缓存管理器
|
||||
* @return 缓存管理器实例
|
||||
*/
|
||||
@Bean
|
||||
public SimpleCacheManager simpleCacheManager() {
|
||||
return new SimpleCacheManager();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.muyu.common.caffeine.constents;
|
||||
|
||||
/**
|
||||
* @Author: 胡杨
|
||||
* @Name: CaffeineContent
|
||||
* @Description: Caffeine常量
|
||||
* @CreatedDate: 2024/9/26 下午12:06
|
||||
* @FilePath: com.muyu.common.caffeine.constents
|
||||
*/
|
||||
|
||||
public class CaffeineContent {
|
||||
|
||||
public static final String CAR_VIN = "car:Vin";
|
||||
|
||||
public static final String VIN = "vin";
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
com.muyu.cloud.common.saas.interceptor.WebMvcSaaSConfig
|
||||
com.muyu.cloud.common.many.datasource.ManyDataSource
|
||||
com.muyu.cloud.common.many.datasource.factory.DruidDataSourceFactory
|
Loading…
Reference in New Issue