gasWellWatch/src/main/java/com/isu/gaswellwatch/modbus/data/CacheService.java

47 lines
1.6 KiB
Java
Raw Normal View History

2024-12-09 12:57:11 +08:00
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:scwsl@foxmail.com">王仕龙</a>
2024-12-09 12:57:11 +08:00
* 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);
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);
}
}
}