Caffeine缓存公共模块

dev.protocol.parsing
面包骑士 2024-09-26 19:26:50 +08:00 committed by chentaisen
parent 9b3da53c6b
commit 8741c20ddb
5 changed files with 191 additions and 0 deletions

View File

@ -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>

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -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";
}

View File

@ -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