diff --git a/couplet-common/couplet-common-redis/src/main/java/com/couplet/common/redis/service/RedisService.java b/couplet-common/couplet-common-redis/src/main/java/com/couplet/common/redis/service/RedisService.java index 3b6e681..8e17f89 100644 --- a/couplet-common/couplet-common-redis/src/main/java/com/couplet/common/redis/service/RedisService.java +++ b/couplet-common/couplet-common-redis/src/main/java/com/couplet/common/redis/service/RedisService.java @@ -1,7 +1,10 @@ package com.couplet.common.redis.service; +import ch.qos.logback.core.BasicStatusManager; import com.couplet.common.domain.CoupletVehicleData; import com.couplet.common.domain.request.RealTimeDataRequest; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; @@ -23,13 +26,14 @@ public class RedisService { public RedisTemplate redisTemplate; + // ... 其他已有方法 ... /** * 向指定集合中添加值,如果值不存在则添加并返回true,否则返回false。 * * @param setKey 集合键名 - * @param value 要添加的值 + * @param value 要添加的值 * @return true表示值已成功添加(之前不存在),false表示值已存在 */ public boolean addToSetIfNotExists(String setKey, String value) { @@ -43,7 +47,7 @@ public class RedisService { * @param key 缓存的键值 * @param value 缓存的值 */ - public void setCacheObject(final String key, final T value) { + public void setCacheObject (final String key, final T value) { redisTemplate.opsForValue().set(key, value); } @@ -55,7 +59,7 @@ public class RedisService { * @param timeout 时间 * @param timeUnit 时间颗粒度 */ - public void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) { + public void setCacheObject (final String key, final T value, final Long timeout, final TimeUnit timeUnit) { redisTemplate.opsForValue().set(key, value, timeout, timeUnit); } @@ -64,9 +68,10 @@ public class RedisService { * * @param key Redis键 * @param timeout 超时时间 + * * @return true=设置成功;false=设置失败 */ - public boolean expire(final String key, final long timeout) { + public boolean expire (final String key, final long timeout) { return expire(key, timeout, TimeUnit.SECONDS); } @@ -76,9 +81,10 @@ public class RedisService { * @param key Redis键 * @param timeout 超时时间 * @param unit 时间单位 + * * @return true=设置成功;false=设置失败 */ - public boolean expire(final String key, final long timeout, final TimeUnit unit) { + public boolean expire (final String key, final long timeout, final TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); } @@ -86,9 +92,10 @@ public class RedisService { * 获取有效时间 * * @param key Redis键 + * * @return 有效时间 */ - public long getExpire(final String key) { + public long getExpire (final String key) { return redisTemplate.getExpire(key); } @@ -96,9 +103,10 @@ public class RedisService { * 判断 key是否存在 * * @param key 键 + * * @return true 存在 false不存在 */ - public Boolean hasKey(String key) { + public Boolean hasKey (String key) { return redisTemplate.hasKey(key); } @@ -106,9 +114,10 @@ public class RedisService { * 获得缓存的基本对象。 * * @param key 缓存键值 + * * @return 缓存键值对应的数据 */ - public T getCacheObject(final String key) { + public T getCacheObject (final String key) { ValueOperations operation = redisTemplate.opsForValue(); return operation.get(key); } @@ -118,7 +127,7 @@ public class RedisService { * * @param key */ - public boolean deleteObject(final String key) { + public boolean deleteObject (final String key) { return redisTemplate.delete(key); } @@ -126,9 +135,10 @@ public class RedisService { * 删除集合对象 * * @param collection 多个对象 + * * @return */ - public boolean deleteObject(final Collection collection) { + public boolean deleteObject (final Collection collection) { return redisTemplate.delete(collection) > 0; } @@ -137,9 +147,10 @@ public class RedisService { * * @param key 缓存的键值 * @param dataList 待缓存的List数据 + * * @return 缓存的对象 */ - public long setCacheList(final String key, final List dataList) { + public long setCacheList (final String key, final List dataList) { Long count = redisTemplate.opsForList().rightPushAll(key, dataList); return count == null ? 0 : count; } @@ -148,9 +159,10 @@ public class RedisService { * 获得缓存的list对象 * * @param key 缓存的键值 + * * @return 缓存键值对应的数据 */ - public List getCacheList(final String key) { + public List getCacheList (final String key) { return redisTemplate.opsForList().range(key, 0, -1); } @@ -159,9 +171,10 @@ public class RedisService { * * @param key 缓存键值 * @param dataSet 缓存的数据 + * * @return 缓存数据的对象 */ - public BoundSetOperations setCacheSet(final String key, final Set dataSet) { + public BoundSetOperations setCacheSet (final String key, final Set dataSet) { BoundSetOperations setOperation = redisTemplate.boundSetOps(key); Iterator it = dataSet.iterator(); while (it.hasNext()) { @@ -169,25 +182,25 @@ public class RedisService { } return setOperation; } - /** * 缓存Set * - * @param key 缓存键值 + * @param key 缓存键值 * @param setValue 缓存的数据 + * * @return 缓存数据的对象 */ - public BoundSetOperations setCacheSet(final String key, final T setValue) { + public BoundSetOperations setCacheSet (final String key, final T setValue) { BoundSetOperations setOperation = redisTemplate.boundSetOps(key); setOperation.add(setValue); return setOperation; } - /** * 缓存Set * - * @param key 缓存键值 + * @param key 缓存键值 * @param setValue 缓存的数据 + * * @return 缓存数据的对象 */ public void deleteSet(String key, String setValue) { @@ -195,25 +208,26 @@ public class RedisService { BoundSetOperations setOperations = redisTemplate.boundSetOps(key); setOperations.remove(setValue); } - /** * 获得缓存的set * * @param key + * * @return */ - public Set getCacheSet(final String key) { + public Set getCacheSet (final String key) { return redisTemplate.opsForSet().members(key); } + /** * 缓存Map * * @param key * @param dataMap */ - public void setCacheMap(final String key, final Map dataMap) { + public void setCacheMap (final String key, final Map dataMap) { if (dataMap != null) { redisTemplate.opsForHash().putAll(key, dataMap); } @@ -223,9 +237,10 @@ public class RedisService { * 获得缓存的Map * * @param key + * * @return */ - public Map getCacheMap(final String key) { + public Map getCacheMap (final String key) { return redisTemplate.opsForHash().entries(key); } @@ -236,7 +251,7 @@ public class RedisService { * @param hKey Hash键 * @param value 值 */ - public void setCacheMapValue(final String key, final String hKey, final T value) { + public void setCacheMapValue (final String key, final String hKey, final T value) { redisTemplate.opsForHash().put(key, hKey, value); } @@ -245,9 +260,10 @@ public class RedisService { * * @param key Redis键 * @param hKey Hash键 + * * @return Hash中的对象 */ - public T getCacheMapValue(final String key, final String hKey) { + public T getCacheMapValue (final String key, final String hKey) { HashOperations opsForHash = redisTemplate.opsForHash(); return opsForHash.get(key, hKey); } @@ -257,9 +273,10 @@ public class RedisService { * * @param key Redis键 * @param hKeys Hash键集合 + * * @return Hash对象集合 */ - public List getMultiCacheMapValue(final String key, final Collection hKeys) { + public List getMultiCacheMapValue (final String key, final Collection hKeys) { return redisTemplate.opsForHash().multiGet(key, hKeys); } @@ -268,9 +285,10 @@ public class RedisService { * * @param key Redis键 * @param hKey Hash键 + * * @return 是否成功 */ - public boolean deleteCacheMapValue(final String key, final String hKey) { + public boolean deleteCacheMapValue (final String key, final String hKey) { return redisTemplate.opsForHash().delete(key, hKey) > 0; } @@ -278,19 +296,33 @@ public class RedisService { * 获得缓存的基本对象列表 * * @param pattern 字符串前缀 + * * @return 对象列表 */ - public Collection keys(final String pattern) { + public Collection keys (final String pattern) { return redisTemplate.keys(pattern); } public void setVinAndUserId(RealTimeDataRequest realTimeDataRequest) { - String key = "vin:" + realTimeDataRequest.getVin(); - redisTemplate.opsForSet().add(key, realTimeDataRequest); + String key = "vin"+realTimeDataRequest.getVin(); + SetOperations setOperations = redisTemplate.opsForSet(); + // 序列化RealTimeDataRequest对象为JSON字符串 + ObjectMapper mapper = new ObjectMapper(); + String jsonStr; + try { + jsonStr = mapper.writeValueAsString(realTimeDataRequest); + } catch (JsonProcessingException e) { + throw new RuntimeException(e.getMessage()); + } + + List wrappedJson = Collections.singletonList(jsonStr); + + // 将List作为Set的唯一元素添加到Redis中 + setOperations.add(key, wrappedJson); } public void stopViewingData(String vin) { - String key = "vin:" + vin; + String key = "vin:"+vin; redisTemplate.delete(key); } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/VehicleDetectionController.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/VehicleDetectionController.java index cf1919a..5e6966c 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/VehicleDetectionController.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/controller/VehicleDetectionController.java @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Objects; +import java.util.Set; /** * @author fufanrui @@ -67,9 +68,9 @@ public class VehicleDetectionController { * @date */ @PostMapping("/monitorinDataList/{vin}") - public Result> monitorinDataList(@PathVariable String vin){ - List monitorinDataList = vehicleDetectionService.monitorinDataList(vin); - Result> success = Result.success(monitorinDataList); + public Result> monitorinDataList(@PathVariable String vin){ + Set monitorinDataList = vehicleDetectionService.monitorinDataList(vin); + Result> success = Result.success(monitorinDataList); return success; } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/VehicleDetectionService.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/VehicleDetectionService.java index f894f8b..8c2ac1c 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/VehicleDetectionService.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/VehicleDetectionService.java @@ -6,6 +6,7 @@ import com.couplet.common.domain.CoupletVehicleData; import com.couplet.common.domain.Vehicle; import java.util.List; +import java.util.Set; public interface VehicleDetectionService { @@ -13,7 +14,9 @@ public interface VehicleDetectionService { void stopViewingData(String vin); - List monitorinDataList(String vin); + void monitorinData(String vin); + + Set monitorinDataList(String vin); } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleDetectionServiceImpl.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleDetectionServiceImpl.java index b3686cf..8c5a09c 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleDetectionServiceImpl.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleDetectionServiceImpl.java @@ -50,12 +50,12 @@ public class VehicleDetectionServiceImpl implements VehicleDetectionService{ } @Override - public List monitorinDataList(String vin) { + public Set monitorinDataList(String vin) { String key = "vin:query:" + vin; log.info("key为:"+key); Set cacheSet = redisService.getCacheSet(key); - ArrayList coupletMsgData = new ArrayList<>(cacheSet); - return coupletMsgData; + + return cacheSet; } @Override