缓存清理
This commit is contained in:
parent
82b60d235c
commit
344b3c600f
|
@ -0,0 +1,47 @@
|
|||
package com.isu.gaswellwatch.modbus.data;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.SessionCallback;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:shilong.wang@alpha-ess.com">王仕龙</a>
|
||||
* 2024/12/9 12:34
|
||||
*/
|
||||
@Service
|
||||
@SuppressWarnings("all")
|
||||
public class CacheService {
|
||||
@Resource(name = "stringRedisTemplate")
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
public void cleanDeviceCache(Long deviceId) {
|
||||
this.redisTemplate.executePipelined(new SessionCallback<String>() {
|
||||
@Override
|
||||
public String execute(RedisOperations operations) throws DataAccessException {
|
||||
operations.delete(PersistenceHandler.DEVICE_INFO_CACHE + deviceId);
|
||||
operations.delete(PersistenceHandler.DEVICE_DATA_CACHE + deviceId);
|
||||
operations.opsForHash().delete(PersistenceHandler.ONLINE_DEVICE_CACHE, deviceId);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void cleanAllDeviceCache() {
|
||||
Set<String> removeKeys = new HashSet<>();
|
||||
removeKeys.addAll(this.redisTemplate.keys(PersistenceHandler.DEVICE_INFO_CACHE + "*"));
|
||||
removeKeys.addAll(this.redisTemplate.keys(PersistenceHandler.DEVICE_DATA_CACHE + "*"));
|
||||
if (ObjectUtils.isNotEmpty(removeKeys)) {
|
||||
removeKeys.add(PersistenceHandler.ONLINE_DEVICE_CACHE);
|
||||
this.redisTemplate.delete(removeKeys);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ public interface PersistenceHandler {
|
|||
String ETC_MODBUS_TYPE = "etc";
|
||||
String SCSS_MODBUS_TYPE = "scss";
|
||||
|
||||
|
||||
String COMMAND_CACHE = "modbus:command:";
|
||||
String DEVICE_INFO_CACHE = "info:device:";
|
||||
String DEVICE_DATA_CACHE = "data:device:";
|
||||
String ONLINE_DEVICE_CACHE = "data:device:online";
|
||||
|
|
|
@ -40,7 +40,6 @@ import java.util.*;
|
|||
@SuppressWarnings("all")
|
||||
public class ModbusMessagePersistListener implements BatchMessageListener {
|
||||
|
||||
public static final String COMMAND_CACHE = "modbus:command:";
|
||||
private static final String COMMAND_SQL = "select * from commands where id = ?";
|
||||
private static final String COMMAND_POINT_SQL = "select * from command_points where command_id = ?";
|
||||
|
||||
|
@ -79,7 +78,7 @@ public class ModbusMessagePersistListener implements BatchMessageListener {
|
|||
messagePointMap = new HashMap<>();
|
||||
collectionMessage = messageSplit[5];
|
||||
commandId = NumberUtils.createLong(messageSplit[2]);
|
||||
commandString = Objects.toString(this.redisTemplate.opsForValue().get(COMMAND_CACHE + commandId), "");
|
||||
commandString = Objects.toString(this.redisTemplate.opsForValue().get(PersistenceHandler.COMMAND_CACHE + commandId), "");
|
||||
commandMap = StringUtils.isNotEmpty(commandString) ? JSONUtil.parseObj(commandString) : null;
|
||||
if (ObjectUtils.isEmpty(commandMap)) {
|
||||
commandMap = this.jdbcTemplate.queryForMap(COMMAND_SQL, commandId);
|
||||
|
@ -87,7 +86,7 @@ public class ModbusMessagePersistListener implements BatchMessageListener {
|
|||
log.error("指令[{}]不存在,数据: {}", commandId, messageString);
|
||||
continue;
|
||||
}
|
||||
this.redisTemplate.opsForValue().setIfAbsent(COMMAND_CACHE + commandId, JSONUtil.toJsonStr(commandMap), Duration.ofDays(1));
|
||||
this.redisTemplate.opsForValue().setIfAbsent(PersistenceHandler.COMMAND_CACHE + commandId, JSONUtil.toJsonStr(commandMap), Duration.ofDays(1));
|
||||
}
|
||||
try {
|
||||
String address;
|
||||
|
@ -150,7 +149,7 @@ public class ModbusMessagePersistListener implements BatchMessageListener {
|
|||
|
||||
public void decode(ModbusMessage modbusMessage) {
|
||||
Long commandId = modbusMessage.getCommandId();
|
||||
String cacheName = ModbusMessagePersistListener.COMMAND_CACHE + commandId + ":points";
|
||||
String cacheName = PersistenceHandler.COMMAND_CACHE + commandId + ":points";
|
||||
String cachePoints = Objects.toString(this.redisTemplate.opsForValue().get(cacheName), "");
|
||||
|
||||
List<Map<String, Object>> commandPointList = StringUtils.isNotEmpty(cachePoints) ? (List) JSONUtil.parseArray(cachePoints) : null;
|
||||
|
|
Loading…
Reference in New Issue