feat: 更改Caffeine缓存管理器的初始化配置,初始化信息来源从配置文件改为了枚举
parent
a28e5d751b
commit
eb37fb5072
|
@ -1,6 +1,7 @@
|
||||||
package com.muyu.common.caffeine.bean;
|
package com.muyu.common.caffeine.bean;
|
||||||
|
|
||||||
|
|
||||||
|
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
@ -11,10 +12,12 @@ import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Caffeine管理器
|
||||||
* @Author: 胡杨
|
* @Author: 胡杨
|
||||||
* @Name: CaffeineCacheConfig
|
* @Name: CaffeineCacheConfig
|
||||||
* @Description: Caffeine管理器
|
* @Description: Caffeine管理器
|
||||||
|
@ -25,8 +28,6 @@ import java.util.List;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class CaffeineManager {
|
public class CaffeineManager {
|
||||||
@Value("#{'${cacheNames}'.split(',')}")
|
|
||||||
private List<String> cacheNames;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建缓存管理器
|
* 创建缓存管理器
|
||||||
|
@ -35,6 +36,7 @@ public class CaffeineManager {
|
||||||
@Bean
|
@Bean
|
||||||
public CacheManager cacheManager() {
|
public CacheManager cacheManager() {
|
||||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||||
|
List<String> cacheNames = CacheNameEnums.getCodes();
|
||||||
cacheManager.setCaches(cacheNames.stream()
|
cacheManager.setCaches(cacheNames.stream()
|
||||||
.map(name -> new CaffeineCache(
|
.map(name -> new CaffeineCache(
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.muyu.common.caffeine.constents;
|
package com.muyu.common.caffeine.constents;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Caffeine常量
|
||||||
* @Author: 胡杨
|
* @Author: 胡杨
|
||||||
* @Name: CaffeineContent
|
* @Name: CaffeineContent
|
||||||
* @Description: Caffeine常量
|
* @Description: Caffeine常量
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.muyu.common.caffeine.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存分区枚举
|
||||||
|
*
|
||||||
|
* @Author: 胡杨
|
||||||
|
* @Name: CacheNameEnums
|
||||||
|
* @Description: 缓存分区枚举
|
||||||
|
* @CreatedDate: 2024/10/2 上午9:17
|
||||||
|
* @FilePath: com.muyu.common.caffeine.enums
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum CacheNameEnums {
|
||||||
|
FAULT("fault", "故障"),
|
||||||
|
FENCE("fence", "围栏"),
|
||||||
|
WARMING("warming", "预警");
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
private final String info;
|
||||||
|
|
||||||
|
CacheNameEnums(String code, String info) {
|
||||||
|
this.code = code;
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 鉴别参数是否是枚举的值
|
||||||
|
*
|
||||||
|
* @param code 需鉴别参数
|
||||||
|
* @return 如果存在返回结果turn, 否则返回false
|
||||||
|
*/
|
||||||
|
public static boolean isCode(String code) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.map(CacheNameEnums::getCode)
|
||||||
|
.anyMatch(c -> c.equals(code));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取枚举Value
|
||||||
|
* @param code 编码
|
||||||
|
* @return Value
|
||||||
|
*/
|
||||||
|
public static String getInfo(String code) {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.filter(c -> c.getCode().equals(code))
|
||||||
|
.map(CacheNameEnums::getInfo)
|
||||||
|
.findFirst()
|
||||||
|
.orElse("");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有code
|
||||||
|
* @return code集合
|
||||||
|
*/
|
||||||
|
public static List<String> getCodes() {
|
||||||
|
return Arrays.stream(values())
|
||||||
|
.map(CacheNameEnums::getCode)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ package com.muyu.common.caffeine.utils;
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import com.muyu.common.caffeine.constents.CaffeineContent;
|
import com.muyu.common.caffeine.constents.CaffeineContent;
|
||||||
|
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||||
import com.muyu.common.redis.service.RedisService;
|
import com.muyu.common.redis.service.RedisService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -28,32 +29,25 @@ import java.util.Collection;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class CaffeineCacheUtils {
|
public class CaffeineCacheUtils {
|
||||||
@Resource
|
|
||||||
private RedisService redisService;
|
|
||||||
@Resource
|
@Resource
|
||||||
private CacheManager cacheManager;
|
private CacheManager cacheManager;
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * 车辆上线 - 新增缓存
|
* 车辆上线 - 新增缓存
|
||||||
// */
|
*/
|
||||||
// public void addCarCache(String vin) {
|
public void addCarCache(String vin) {
|
||||||
//
|
// 从Redis中获取缓存信息
|
||||||
// ArrayList<CaffeineCache> caches = new ArrayList<>();
|
for (String name : CacheNameEnums.getCodes()) {
|
||||||
// // 从Redis中获取缓存信息
|
String value = redisTemplate.opsForValue().get(name+":"+vin);
|
||||||
// Collection<String> keys = redisTemplate.keys(CaffeineContent.CAR_VIN_KEY + vin);
|
cacheManager.getCache(name).put(vin, value);
|
||||||
// keys.forEach(key -> {
|
log.info("存储缓存, 缓存分区:[{}], 车辆编码:[{}], 存储值:[{}]", name, vin, value);
|
||||||
// Object string = redisTemplate.opsForValue().get(key);
|
}
|
||||||
// Cache<Object , Object> cache = Caffeine.newBuilder().build();
|
log.info("车辆编码:{},本地缓存完成...",vin);
|
||||||
// cache.put(key, string);
|
}
|
||||||
// // 全部存储到 CaffeineCache集合
|
|
||||||
// caches.add(new CaffeineCache(vin, cache));
|
|
||||||
// log.info("存储缓存,vin:{}, key:{}, value:{}", vin, key, string);
|
|
||||||
// });
|
|
||||||
// simpleCacheManager.setCaches(caches);
|
|
||||||
// log.info("车辆编码:{},本地缓存完成...",vin);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
/**
|
/**
|
||||||
* 车辆下线 - 删除缓存
|
* 车辆下线 - 删除缓存
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.muyu.data.processing.controller;
|
package com.muyu.data.processing.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.muyu.common.caffeine.enums.CacheNameEnums;
|
||||||
import com.muyu.common.core.utils.uuid.UUID;
|
import com.muyu.common.core.utils.uuid.UUID;
|
||||||
import com.muyu.common.iotdb.config.IotDBConfig;
|
import com.muyu.common.iotdb.config.IotDBConfig;
|
||||||
import com.muyu.common.kafka.constants.KafkaConstants;
|
import com.muyu.common.kafka.constants.KafkaConstants;
|
||||||
|
@ -130,6 +131,10 @@ public class TestController {
|
||||||
|
|
||||||
@GetMapping("/testDelCache")
|
@GetMapping("/testDelCache")
|
||||||
public void testDelCache(@RequestParam("cacheName") String cacheName) {
|
public void testDelCache(@RequestParam("cacheName") String cacheName) {
|
||||||
|
if (!CacheNameEnums.isCode(cacheName)){
|
||||||
|
log.info("缓存分区不存在");
|
||||||
|
return;
|
||||||
|
}
|
||||||
Cache cache = cacheManager.getCache(cacheName);
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
cache.invalidate();
|
cache.invalidate();
|
||||||
|
|
|
@ -11,7 +11,7 @@ import org.springframework.amqp.core.Message;
|
||||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
import org.springframework.cache.support.SimpleCacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class DownlineRabbitConsumer {
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String,String> redisTemplate;
|
private RedisTemplate<String,String> redisTemplate;
|
||||||
@Resource
|
@Resource
|
||||||
private SimpleCacheManager simpleCacheManager;
|
private CacheManager cacheManager;
|
||||||
|
|
||||||
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.DOWNLINE_QUEUE)})
|
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.DOWNLINE_QUEUE)})
|
||||||
public void downline(String vin, Message message, Channel channel) {
|
public void downline(String vin, Message message, Channel channel) {
|
||||||
|
@ -64,7 +64,7 @@ public class DownlineRabbitConsumer {
|
||||||
* 车辆下线 - 删除缓存
|
* 车辆下线 - 删除缓存
|
||||||
*/
|
*/
|
||||||
public void deleteCarCache(String vin) {
|
public void deleteCarCache(String vin) {
|
||||||
Cache cache = simpleCacheManager.getCache(vin);
|
Cache cache = cacheManager.getCache(vin);
|
||||||
if (ObjectUtils.isNotEmpty(cache)){
|
if (ObjectUtils.isNotEmpty(cache)){
|
||||||
cache.invalidate();
|
cache.invalidate();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,13 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.amqp.core.Message;
|
import org.springframework.amqp.core.Message;
|
||||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上线事件监听
|
* 上线事件监听
|
||||||
|
@ -28,8 +31,11 @@ import java.io.IOException;
|
||||||
public class GoOnlineRabbitConsumer {
|
public class GoOnlineRabbitConsumer {
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String,String> redisTemplate;
|
private RedisTemplate<String,String> redisTemplate;
|
||||||
|
@Resource
|
||||||
|
private CacheManager cacheManager;
|
||||||
|
|
||||||
|
@Value("#{'${cacheNames}'.split(',')}")
|
||||||
|
private List<String> cacheNames;
|
||||||
|
|
||||||
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.GO_ONLINE_QUEUE)})
|
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.GO_ONLINE_QUEUE)})
|
||||||
public void goOnline(String vin, Message message, Channel channel){
|
public void goOnline(String vin, Message message, Channel channel){
|
||||||
|
@ -60,17 +66,11 @@ public class GoOnlineRabbitConsumer {
|
||||||
*/
|
*/
|
||||||
public void addCarCache(String vin) {
|
public void addCarCache(String vin) {
|
||||||
// 从Redis中获取缓存信息
|
// 从Redis中获取缓存信息
|
||||||
// ArrayList<CaffeineCache> caches = new ArrayList<>();
|
for (String name : cacheNames) {
|
||||||
// Cache<Object , Object> cache = Caffeine.newBuilder().build();
|
String value = redisTemplate.opsForValue().get(name+":"+vin);
|
||||||
// Collection<String> keys = redisTemplate.keys(vin+":*");
|
cacheManager.getCache(name).put(vin, value);
|
||||||
// keys.forEach(key -> {
|
log.info("存储缓存, 缓存分区:[{}], 车辆编码:[{}], 存储值:[{}]", name, vin, value);
|
||||||
// String value = redisTemplate.opsForValue().get(key);
|
}
|
||||||
// cache.put(key, value);
|
log.info("车辆编码:{},本地缓存完成...",vin);
|
||||||
// // 全部存储到 CaffeineCache集合
|
|
||||||
// caches.add(new CaffeineCache(key, cache));
|
|
||||||
// log.info("存储缓存,vin:{}, key:{}, value:{}", vin, key, value);
|
|
||||||
// });
|
|
||||||
// simpleCacheManager.setCaches(caches);
|
|
||||||
// log.info("车辆编码:{},本地缓存完成...",vin);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue