feat: 更改Caffeine缓存管理器的初始化配置,初始化信息来源从配置文件改为了枚举

dev.data.processing.dataTreating
面包骑士 2024-10-02 09:34:38 +08:00
parent a28e5d751b
commit eb37fb5072
7 changed files with 108 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package com.muyu.common.caffeine.bean;
import com.muyu.common.caffeine.enums.CacheNameEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import com.github.benmanes.caffeine.cache.Caffeine;
@ -11,10 +12,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Caffeine
* @Author:
* @Name: CaffeineCacheConfig
* @Description: Caffeine
@ -25,8 +28,6 @@ import java.util.List;
@Slf4j
@Component
public class CaffeineManager {
@Value("#{'${cacheNames}'.split(',')}")
private List<String> cacheNames;
/**
*
@ -35,6 +36,7 @@ public class CaffeineManager {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<String> cacheNames = CacheNameEnums.getCodes();
cacheManager.setCaches(cacheNames.stream()
.map(name -> new CaffeineCache(
name,

View File

@ -1,6 +1,7 @@
package com.muyu.common.caffeine.constents;
/**
* Caffeine
* @Author:
* @Name: CaffeineContent
* @Description: Caffeine

View File

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

View File

@ -4,6 +4,7 @@ package com.muyu.common.caffeine.utils;
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.caffeine.enums.CacheNameEnums;
import com.muyu.common.redis.service.RedisService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
@ -28,32 +29,25 @@ import java.util.Collection;
@Slf4j
@Component
public class CaffeineCacheUtils {
@Resource
private RedisService redisService;
@Resource
private CacheManager cacheManager;
@Resource
private RedisTemplate<String, String> redisTemplate;
// /**
// * 车辆上线 - 新增缓存
// */
// public void addCarCache(String vin) {
//
// ArrayList<CaffeineCache> caches = new ArrayList<>();
// // 从Redis中获取缓存信息
// Collection<String> keys = redisTemplate.keys(CaffeineContent.CAR_VIN_KEY + vin);
// keys.forEach(key -> {
// Object string = redisTemplate.opsForValue().get(key);
// Cache<Object , Object> cache = Caffeine.newBuilder().build();
// 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);
// }
//
/**
* 线 -
*/
public void addCarCache(String vin) {
// 从Redis中获取缓存信息
for (String name : CacheNameEnums.getCodes()) {
String value = redisTemplate.opsForValue().get(name+":"+vin);
cacheManager.getCache(name).put(vin, value);
log.info("存储缓存, 缓存分区:[{}], 车辆编码:[{}], 存储值:[{}]", name, vin, value);
}
log.info("车辆编码:{},本地缓存完成...",vin);
}
/**
* 线 -
*/

View File

@ -1,6 +1,7 @@
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.iotdb.config.IotDBConfig;
import com.muyu.common.kafka.constants.KafkaConstants;
@ -130,6 +131,10 @@ public class TestController {
@GetMapping("/testDelCache")
public void testDelCache(@RequestParam("cacheName") String cacheName) {
if (!CacheNameEnums.isCode(cacheName)){
log.info("缓存分区不存在");
return;
}
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
cache.invalidate();

View File

@ -11,7 +11,7 @@ import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
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.stereotype.Component;
@ -33,7 +33,7 @@ public class DownlineRabbitConsumer {
@Resource
private RedisTemplate<String,String> redisTemplate;
@Resource
private SimpleCacheManager simpleCacheManager;
private CacheManager cacheManager;
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.DOWNLINE_QUEUE)})
public void downline(String vin, Message message, Channel channel) {
@ -64,7 +64,7 @@ public class DownlineRabbitConsumer {
* 线 -
*/
public void deleteCarCache(String vin) {
Cache cache = simpleCacheManager.getCache(vin);
Cache cache = cacheManager.getCache(vin);
if (ObjectUtils.isNotEmpty(cache)){
cache.invalidate();
}

View File

@ -9,10 +9,13 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
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.stereotype.Component;
import java.io.IOException;
import java.util.List;
/**
* 线
@ -28,8 +31,11 @@ import java.io.IOException;
public class GoOnlineRabbitConsumer {
@Resource
private RedisTemplate<String,String> redisTemplate;
@Resource
private CacheManager cacheManager;
@Value("#{'${cacheNames}'.split(',')}")
private List<String> cacheNames;
@RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.GO_ONLINE_QUEUE)})
public void goOnline(String vin, Message message, Channel channel){
@ -60,17 +66,11 @@ public class GoOnlineRabbitConsumer {
*/
public void addCarCache(String vin) {
// 从Redis中获取缓存信息
// ArrayList<CaffeineCache> caches = new ArrayList<>();
// Cache<Object , Object> cache = Caffeine.newBuilder().build();
// Collection<String> keys = redisTemplate.keys(vin+":*");
// keys.forEach(key -> {
// String value = redisTemplate.opsForValue().get(key);
// cache.put(key, value);
// // 全部存储到 CaffeineCache集合
// caches.add(new CaffeineCache(key, cache));
// log.info("存储缓存,vin:{}, key:{}, value:{}", vin, key, value);
// });
// simpleCacheManager.setCaches(caches);
// log.info("车辆编码:{},本地缓存完成...",vin);
for (String name : cacheNames) {
String value = redisTemplate.opsForValue().get(name+":"+vin);
cacheManager.getCache(name).put(vin, value);
log.info("存储缓存, 缓存分区:[{}], 车辆编码:[{}], 存储值:[{}]", name, vin, value);
}
log.info("车辆编码:{},本地缓存完成...",vin);
}
}