From 479b55ec18879ab2d08e18777e942b5a6c47d514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=91=E5=B9=B4=E6=A2=A6=E4=B8=8E=E7=A0=96?= <2847127106@qq.com> Date: Mon, 30 Sep 2024 11:14:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95Rabbit=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../muyu/common/caffeine/CaffeineCacheUtils.java | 7 ++++--- ....boot.autoconfigure.AutoConfiguration.imports | 2 +- .../processing/controller/TestController.java | 16 ++++++++++++++++ .../rebbit/GoOnlineRabbitConsumer.java | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java index 59ac015..709cf0b 100644 --- a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java +++ b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java @@ -43,6 +43,7 @@ public class CaffeineCacheUtils { cache.put(key, value); // 全部存储到 CaffeineCache集合 caches.add(new CaffeineCache(vin, cache)); + log.info("存储缓存,vin:{}, key:{}, value:{}", vin, key, value); }); simpleCacheManager.setCaches(caches); log.info("车辆编码:{},本地缓存完成...",vin); @@ -52,7 +53,7 @@ public class CaffeineCacheUtils { * 车辆下线 - 删除缓存 */ public void deleteCarCache(String vin) { - if (hasCarVinCache(vin)) { + if (!hasCarVinCache(vin)) { log.warn("车辆编码:{},本地缓存不存在该车辆信息...", vin); return; } @@ -64,7 +65,7 @@ public class CaffeineCacheUtils { * 获取车辆信息缓存 */ public Object getCarCache(String vin, String key) { - if (hasCarVinKeyCache(vin, key)){ + if (!hasCarVinKeyCache(vin, key)){ log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin); return null; } @@ -75,7 +76,7 @@ public class CaffeineCacheUtils { * 获取车辆信息缓存 */ public T getCarCache(String vin, String key, Class type) { - if (hasCarVinKeyCache(vin,key)){ + if (!hasCarVinKeyCache(vin,key)){ log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin); return null; } diff --git a/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 7cb57e7..2452d1c 100644 --- a/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,2 @@ - +com.muyu.common.caffeine.CaffeineCacheUtils com.muyu.common.caffeine.bean.CaffeineManagerBean diff --git a/cloud-data-processing/src/main/java/com/muyu/data/processing/controller/TestController.java b/cloud-data-processing/src/main/java/com/muyu/data/processing/controller/TestController.java index 4adee97..b8edca8 100644 --- a/cloud-data-processing/src/main/java/com/muyu/data/processing/controller/TestController.java +++ b/cloud-data-processing/src/main/java/com/muyu/data/processing/controller/TestController.java @@ -1,6 +1,7 @@ package com.muyu.data.processing.controller; +import com.muyu.common.caffeine.CaffeineCacheUtils; import com.muyu.common.core.utils.uuid.UUID; import com.muyu.common.iotdb.config.IotDBConfig; import com.muyu.common.kafka.constants.KafkaConstants; @@ -9,6 +10,7 @@ import jakarta.annotation.Resource; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import lombok.extern.slf4j.Slf4j; @@ -30,6 +32,10 @@ public class TestController { private RabbitTemplate rabbitTemplate; @Resource private IotDBConfig iotDBConfig; + @Resource + private CaffeineCacheUtils caffeineCacheUtils; + @Resource + private RedisTemplate redisTemplate; @GetMapping("/testKafka") public void sendMsg(@RequestParam("msg") String msg) { @@ -87,4 +93,14 @@ public class TestController { String sql = String.format("insert into root.one.%s(timestamp, temperature) values (%d, %f)", deviceId, time, value); iotDBConfig.iotSession().executeNonQueryStatement(sql); } + + @GetMapping("/testSetRedis") + public void testSetRedis(@RequestParam("key") String key,@RequestParam("value") String value) { + redisTemplate.opsForValue().set(key,value); + } + + @GetMapping("/testGetCache") + public void testGetCache(@RequestParam("vin") String vin,@RequestParam("key") String key) { + System.out.println(caffeineCacheUtils.getCarCache(vin,key)); + } } diff --git a/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java b/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java index 1ad8133..015c4b3 100644 --- a/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java +++ b/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java @@ -35,7 +35,7 @@ public class GoOnlineRabbitConsumer { try { // 重复性校验 if (DATA_SET.add(message.getMessageProperties().getMessageId())) { -// caffeineCacheUtils.addCarCache(vin); + caffeineCacheUtils.addCarCache(vin); log.info("车辆 {} 上线, 消息已确认。。。",vin); } else { log.info("车辆 {} 上线, 消息重复消费,已确认。。。",vin);