commit upd

server_five_fufanrui
ffr 2024-04-09 18:40:56 +08:00
parent 0b3ed78d08
commit b293555663
4 changed files with 73 additions and 37 deletions

View File

@ -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,13 +26,14 @@ public class RedisService {
public RedisTemplate redisTemplate; public RedisTemplate redisTemplate;
// ... 其他已有方法 ... // ... 其他已有方法 ...
/** /**
* truefalse * truefalse
* *
* @param setKey * @param setKey
* @param value * @param value
* @return truefalse * @return truefalse
*/ */
public boolean addToSetIfNotExists(String setKey, String value) { public boolean addToSetIfNotExists(String setKey, String value) {
@ -43,7 +47,7 @@ public class RedisService {
* @param key * @param key
* @param value * @param value
*/ */
public <T> void setCacheObject(final String key, final T value) { public <T> void setCacheObject (final String key, final T value) {
redisTemplate.opsForValue().set(key, value); redisTemplate.opsForValue().set(key, value);
} }
@ -55,7 +59,7 @@ public class RedisService {
* @param timeout * @param timeout
* @param timeUnit * @param timeUnit
*/ */
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) { public <T> void setCacheObject (final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit); redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
} }
@ -64,9 +68,10 @@ 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) {
return expire(key, timeout, TimeUnit.SECONDS); return expire(key, timeout, TimeUnit.SECONDS);
} }
@ -76,9 +81,10 @@ 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) {
return redisTemplate.expire(key, timeout, unit); return redisTemplate.expire(key, timeout, unit);
} }
@ -86,9 +92,10 @@ public class RedisService {
* *
* *
* @param key Redis * @param key Redis
*
* @return * @return
*/ */
public long getExpire(final String key) { public long getExpire (final String key) {
return redisTemplate.getExpire(key); return redisTemplate.getExpire(key);
} }
@ -96,9 +103,10 @@ 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) {
return redisTemplate.hasKey(key); return redisTemplate.hasKey(key);
} }
@ -106,9 +114,10 @@ public class RedisService {
* *
* *
* @param key * @param key
*
* @return * @return
*/ */
public <T> T getCacheObject(final String key) { public <T> T getCacheObject (final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue(); ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key); return operation.get(key);
} }
@ -118,7 +127,7 @@ public class RedisService {
* *
* @param key * @param key
*/ */
public boolean deleteObject(final String key) { public boolean deleteObject (final String key) {
return redisTemplate.delete(key); return redisTemplate.delete(key);
} }
@ -126,9 +135,10 @@ public class RedisService {
* *
* *
* @param collection * @param collection
*
* @return * @return
*/ */
public boolean deleteObject(final Collection collection) { public boolean deleteObject (final Collection collection) {
return redisTemplate.delete(collection) > 0; return redisTemplate.delete(collection) > 0;
} }
@ -137,9 +147,10 @@ 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) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList); Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count; return count == null ? 0 : count;
} }
@ -148,9 +159,10 @@ 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) {
return redisTemplate.opsForList().range(key, 0, -1); return redisTemplate.opsForList().range(key, 0, -1);
} }
@ -159,9 +171,10 @@ 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) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator(); Iterator<T> it = dataSet.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -169,25 +182,25 @@ 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) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
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,25 +208,26 @@ 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) {
return redisTemplate.opsForSet().members(key); return redisTemplate.opsForSet().members(key);
} }
/** /**
* Map * Map
* *
* @param key * @param key
* @param dataMap * @param dataMap
*/ */
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) { public <T> void setCacheMap (final String key, final Map<String, T> dataMap) {
if (dataMap != null) { if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap); redisTemplate.opsForHash().putAll(key, dataMap);
} }
@ -223,9 +237,10 @@ 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) {
return redisTemplate.opsForHash().entries(key); return redisTemplate.opsForHash().entries(key);
} }
@ -236,7 +251,7 @@ public class RedisService {
* @param hKey Hash * @param hKey Hash
* @param value * @param value
*/ */
public <T> void setCacheMapValue(final String key, final String hKey, final T value) { public <T> void setCacheMapValue (final String key, final String hKey, final T value) {
redisTemplate.opsForHash().put(key, hKey, value); redisTemplate.opsForHash().put(key, hKey, value);
} }
@ -245,9 +260,10 @@ 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) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey); return opsForHash.get(key, hKey);
} }
@ -257,9 +273,10 @@ 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) {
return redisTemplate.opsForHash().multiGet(key, hKeys); return redisTemplate.opsForHash().multiGet(key, hKeys);
} }
@ -268,9 +285,10 @@ 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) {
return redisTemplate.opsForHash().delete(key, hKey) > 0; return redisTemplate.opsForHash().delete(key, hKey) > 0;
} }
@ -278,19 +296,33 @@ public class RedisService {
* *
* *
* @param pattern * @param pattern
*
* @return * @return
*/ */
public Collection<String> keys(final String pattern) { public Collection<String> keys (final String pattern) {
return redisTemplate.keys(pattern); return redisTemplate.keys(pattern);
} }
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) {
String key = "vin:" + vin; String key = "vin:"+vin;
redisTemplate.delete(key); redisTemplate.delete(key);
} }

View File

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

View File

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

View File

@ -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