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 ็Ž‹ไป•้พ™ * 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() { @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 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); } } }