com.gnss.common.utils.RedisUtil Maven / Gradle / Ivy
package com.gnss.common.utils;
import com.gnss.common.proto.TerminalProto;
import org.springframework.data.redis.core.HashOperations;
import java.util.Map;
import java.util.Objects;
/**
* Description: Redis工具
* Company: www.gps-pro.cn
*
* @author huangguangbin
* @version 1.0.1
* @date 2018/11/5
*/
public class RedisUtil {
private RedisUtil() {
}
/**
* Redis在线终端缓存Key
*/
private static final String TERMINAL_ONLINE_REDIS_KEY = "terminal-online";
/**
* 缓存在线终端
* @param terminalHashOperations Hash操作
* @param terminalInfo 终端信息
*/
public static void putOnlineTerminal(HashOperations terminalHashOperations, TerminalProto terminalInfo) {
terminalHashOperations.put(TERMINAL_ONLINE_REDIS_KEY, String.valueOf(terminalInfo.getTerminalId()), terminalInfo);
}
/**
* 获取在线终端
* @param terminalHashOperations Hash操作
* @param terminalId 终端ID
* @return 终端信息
*/
public static TerminalProto getOnlineTerminal(HashOperations terminalHashOperations, Long terminalId) {
return terminalHashOperations.get(TERMINAL_ONLINE_REDIS_KEY, String.valueOf(terminalId));
}
/**
* 删除在线终端
* @param terminalHashOperations Hash操作
* @param terminalInfo 终端信息
*/
public static void deleteOnlineTerminal(HashOperations terminalHashOperations, TerminalProto terminalInfo) {
terminalHashOperations.delete(TERMINAL_ONLINE_REDIS_KEY, String.valueOf(terminalInfo.getTerminalId()));
}
/**
* 删除某个节点的所有在线终端
* @param terminalHashOperations Hash操作
* @param nodeName 节点名称
*/
public static void deleteAllOnlineTerminals(HashOperations terminalHashOperations, String nodeName) {
Map map = terminalHashOperations.entries(TERMINAL_ONLINE_REDIS_KEY);
map.forEach((k, v) -> {
if (Objects.equals(nodeName, v.getNodeName())) {
terminalHashOperations.delete(TERMINAL_ONLINE_REDIS_KEY, k);
}
});
}
}