commit upd
parent
0b3ed78d08
commit
b293555663
|
@ -1,7 +1,10 @@
|
||||||
package com.couplet.common.redis.service;
|
package com.couplet.common.redis.service;
|
||||||
|
|
||||||
|
import ch.qos.logback.core.BasicStatusManager;
|
||||||
import com.couplet.common.domain.CoupletVehicleData;
|
import com.couplet.common.domain.CoupletVehicleData;
|
||||||
import com.couplet.common.domain.request.RealTimeDataRequest;
|
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.apache.poi.ss.formula.functions.T;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.*;
|
import org.springframework.data.redis.core.*;
|
||||||
|
@ -23,6 +26,7 @@ public class RedisService {
|
||||||
public RedisTemplate redisTemplate;
|
public RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ... 其他已有方法 ...
|
// ... 其他已有方法 ...
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,6 +68,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param timeout 超时时间
|
* @param timeout 超时时间
|
||||||
|
*
|
||||||
* @return true=设置成功;false=设置失败
|
* @return true=设置成功;false=设置失败
|
||||||
*/
|
*/
|
||||||
public boolean expire (final String key, final long timeout) {
|
public boolean expire (final String key, final long timeout) {
|
||||||
|
@ -76,6 +81,7 @@ public class RedisService {
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param timeout 超时时间
|
* @param timeout 超时时间
|
||||||
* @param unit 时间单位
|
* @param unit 时间单位
|
||||||
|
*
|
||||||
* @return true=设置成功;false=设置失败
|
* @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) {
|
||||||
|
@ -86,6 +92,7 @@ public class RedisService {
|
||||||
* 获取有效时间
|
* 获取有效时间
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
|
*
|
||||||
* @return 有效时间
|
* @return 有效时间
|
||||||
*/
|
*/
|
||||||
public long getExpire (final String key) {
|
public long getExpire (final String key) {
|
||||||
|
@ -96,6 +103,7 @@ public class RedisService {
|
||||||
* 判断 key是否存在
|
* 判断 key是否存在
|
||||||
*
|
*
|
||||||
* @param key 键
|
* @param key 键
|
||||||
|
*
|
||||||
* @return true 存在 false不存在
|
* @return true 存在 false不存在
|
||||||
*/
|
*/
|
||||||
public Boolean hasKey (String key) {
|
public Boolean hasKey (String key) {
|
||||||
|
@ -106,6 +114,7 @@ public class RedisService {
|
||||||
* 获得缓存的基本对象。
|
* 获得缓存的基本对象。
|
||||||
*
|
*
|
||||||
* @param key 缓存键值
|
* @param key 缓存键值
|
||||||
|
*
|
||||||
* @return 缓存键值对应的数据
|
* @return 缓存键值对应的数据
|
||||||
*/
|
*/
|
||||||
public <T> T getCacheObject (final String key) {
|
public <T> T getCacheObject (final String key) {
|
||||||
|
@ -126,6 +135,7 @@ public class RedisService {
|
||||||
* 删除集合对象
|
* 删除集合对象
|
||||||
*
|
*
|
||||||
* @param collection 多个对象
|
* @param collection 多个对象
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean deleteObject (final Collection collection) {
|
public boolean deleteObject (final Collection collection) {
|
||||||
|
@ -137,6 +147,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key 缓存的键值
|
* @param key 缓存的键值
|
||||||
* @param dataList 待缓存的List数据
|
* @param dataList 待缓存的List数据
|
||||||
|
*
|
||||||
* @return 缓存的对象
|
* @return 缓存的对象
|
||||||
*/
|
*/
|
||||||
public <T> long setCacheList (final String key, final List<T> dataList) {
|
public <T> long setCacheList (final String key, final List<T> dataList) {
|
||||||
|
@ -148,6 +159,7 @@ public class RedisService {
|
||||||
* 获得缓存的list对象
|
* 获得缓存的list对象
|
||||||
*
|
*
|
||||||
* @param key 缓存的键值
|
* @param key 缓存的键值
|
||||||
|
*
|
||||||
* @return 缓存键值对应的数据
|
* @return 缓存键值对应的数据
|
||||||
*/
|
*/
|
||||||
public <T> List<T> getCacheList (final String key) {
|
public <T> List<T> getCacheList (final String key) {
|
||||||
|
@ -159,6 +171,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key 缓存键值
|
* @param key 缓存键值
|
||||||
* @param dataSet 缓存的数据
|
* @param dataSet 缓存的数据
|
||||||
|
*
|
||||||
* @return 缓存数据的对象
|
* @return 缓存数据的对象
|
||||||
*/
|
*/
|
||||||
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final Set<T> dataSet) {
|
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final Set<T> dataSet) {
|
||||||
|
@ -169,12 +182,12 @@ public class RedisService {
|
||||||
}
|
}
|
||||||
return setOperation;
|
return setOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存Set
|
* 缓存Set
|
||||||
*
|
*
|
||||||
* @param key 缓存键值
|
* @param key 缓存键值
|
||||||
* @param setValue 缓存的数据
|
* @param setValue 缓存的数据
|
||||||
|
*
|
||||||
* @return 缓存数据的对象
|
* @return 缓存数据的对象
|
||||||
*/
|
*/
|
||||||
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final T setValue) {
|
public <T> BoundSetOperations<String, T> setCacheSet (final String key, final T setValue) {
|
||||||
|
@ -182,12 +195,12 @@ public class RedisService {
|
||||||
setOperation.add(setValue);
|
setOperation.add(setValue);
|
||||||
return setOperation;
|
return setOperation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存Set
|
* 缓存Set
|
||||||
*
|
*
|
||||||
* @param key 缓存键值
|
* @param key 缓存键值
|
||||||
* @param setValue 缓存的数据
|
* @param setValue 缓存的数据
|
||||||
|
*
|
||||||
* @return 缓存数据的对象
|
* @return 缓存数据的对象
|
||||||
*/
|
*/
|
||||||
public <T> void deleteSet(String key, String setValue) {
|
public <T> void deleteSet(String key, String setValue) {
|
||||||
|
@ -195,11 +208,11 @@ public class RedisService {
|
||||||
BoundSetOperations setOperations = redisTemplate.boundSetOps(key);
|
BoundSetOperations setOperations = redisTemplate.boundSetOps(key);
|
||||||
setOperations.remove(setValue);
|
setOperations.remove(setValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得缓存的set
|
* 获得缓存的set
|
||||||
*
|
*
|
||||||
* @param key
|
* @param key
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public <T> Set<T> getCacheSet (final String key) {
|
public <T> Set<T> getCacheSet (final String key) {
|
||||||
|
@ -207,6 +220,7 @@ public class RedisService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存Map
|
* 缓存Map
|
||||||
*
|
*
|
||||||
|
@ -223,6 +237,7 @@ public class RedisService {
|
||||||
* 获得缓存的Map
|
* 获得缓存的Map
|
||||||
*
|
*
|
||||||
* @param key
|
* @param key
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public <T> Map<String, T> getCacheMap (final String key) {
|
public <T> Map<String, T> getCacheMap (final String key) {
|
||||||
|
@ -245,6 +260,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param hKey Hash键
|
* @param hKey Hash键
|
||||||
|
*
|
||||||
* @return Hash中的对象
|
* @return Hash中的对象
|
||||||
*/
|
*/
|
||||||
public <T> T getCacheMapValue (final String key, final String hKey) {
|
public <T> T getCacheMapValue (final String key, final String hKey) {
|
||||||
|
@ -257,6 +273,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param hKeys Hash键集合
|
* @param hKeys Hash键集合
|
||||||
|
*
|
||||||
* @return Hash对象集合
|
* @return Hash对象集合
|
||||||
*/
|
*/
|
||||||
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<Object> hKeys) {
|
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<Object> hKeys) {
|
||||||
|
@ -268,6 +285,7 @@ public class RedisService {
|
||||||
*
|
*
|
||||||
* @param key Redis键
|
* @param key Redis键
|
||||||
* @param hKey Hash键
|
* @param hKey Hash键
|
||||||
|
*
|
||||||
* @return 是否成功
|
* @return 是否成功
|
||||||
*/
|
*/
|
||||||
public boolean deleteCacheMapValue (final String key, final String hKey) {
|
public boolean deleteCacheMapValue (final String key, final String hKey) {
|
||||||
|
@ -278,6 +296,7 @@ public class RedisService {
|
||||||
* 获得缓存的基本对象列表
|
* 获得缓存的基本对象列表
|
||||||
*
|
*
|
||||||
* @param pattern 字符串前缀
|
* @param pattern 字符串前缀
|
||||||
|
*
|
||||||
* @return 对象列表
|
* @return 对象列表
|
||||||
*/
|
*/
|
||||||
public Collection<String> keys (final String pattern) {
|
public Collection<String> keys (final String pattern) {
|
||||||
|
@ -285,8 +304,21 @@ public class RedisService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVinAndUserId(RealTimeDataRequest realTimeDataRequest) {
|
public void setVinAndUserId(RealTimeDataRequest realTimeDataRequest) {
|
||||||
String key = "vin:" + realTimeDataRequest.getVin();
|
String key = "vin"+realTimeDataRequest.getVin();
|
||||||
redisTemplate.opsForSet().add(key, realTimeDataRequest);
|
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<String> wrappedJson = Collections.singletonList(jsonStr);
|
||||||
|
|
||||||
|
// 将List作为Set的唯一元素添加到Redis中
|
||||||
|
setOperations.add(key, wrappedJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopViewingData(String vin) {
|
public void stopViewingData(String vin) {
|
||||||
|
|
|
@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author fufanrui
|
* @author fufanrui
|
||||||
|
@ -67,9 +68,9 @@ public class VehicleDetectionController {
|
||||||
* @date
|
* @date
|
||||||
*/
|
*/
|
||||||
@PostMapping("/monitorinDataList/{vin}")
|
@PostMapping("/monitorinDataList/{vin}")
|
||||||
public Result<List<CoupletMsgData>> monitorinDataList(@PathVariable String vin){
|
public Result<Set<CoupletMsgData>> monitorinDataList(@PathVariable String vin){
|
||||||
List<CoupletMsgData> monitorinDataList = vehicleDetectionService.monitorinDataList(vin);
|
Set<CoupletMsgData> monitorinDataList = vehicleDetectionService.monitorinDataList(vin);
|
||||||
Result<List<CoupletMsgData>> success = Result.success(monitorinDataList);
|
Result<Set<CoupletMsgData>> success = Result.success(monitorinDataList);
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.couplet.common.domain.CoupletVehicleData;
|
||||||
import com.couplet.common.domain.Vehicle;
|
import com.couplet.common.domain.Vehicle;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public interface VehicleDetectionService {
|
public interface VehicleDetectionService {
|
||||||
|
|
||||||
|
@ -13,7 +14,9 @@ public interface VehicleDetectionService {
|
||||||
|
|
||||||
void stopViewingData(String vin);
|
void stopViewingData(String vin);
|
||||||
|
|
||||||
List<CoupletMsgData> monitorinDataList(String vin);
|
|
||||||
|
|
||||||
void monitorinData(String vin);
|
void monitorinData(String vin);
|
||||||
|
|
||||||
|
Set<CoupletMsgData> monitorinDataList(String vin);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,12 +50,12 @@ public class VehicleDetectionServiceImpl implements VehicleDetectionService{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CoupletMsgData> monitorinDataList(String vin) {
|
public Set<CoupletMsgData> monitorinDataList(String vin) {
|
||||||
String key = "vin:query:" + vin;
|
String key = "vin:query:" + vin;
|
||||||
log.info("key为:"+key);
|
log.info("key为:"+key);
|
||||||
Set<CoupletMsgData> cacheSet = redisService.getCacheSet(key);
|
Set<CoupletMsgData> cacheSet = redisService.getCacheSet(key);
|
||||||
ArrayList<CoupletMsgData> coupletMsgData = new ArrayList<>(cacheSet);
|
|
||||||
return coupletMsgData;
|
return cacheSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue