All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gnss.common.utils.RedisUtil Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package com.gnss.common.utils;

import com.gnss.common.proto.LocationProto;
import com.gnss.common.proto.LocationProtoDTO;
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"; /** * Redis终端信息缓存Key */ private static final String TERMINAL_INFO_REDIS_KEY = "terminal-info"; /** * Redis手机号缓存Key */ private static final String SIMCARD_REDIS_KEY = "simcard"; /** * Redis终端号码缓存Key */ private static final String TERMINAL_NUM_REDIS_KEY = "terminal-num"; /** * Redis车牌号码缓存Key */ private static final String VEHICLE_NUM_REDIS_KEY = "vehicle-num"; /** * Redis终端位置缓存Key */ private static final String LOCATION_REDIS_KEY = "location"; /** * 缓存在线终端 * * @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); } }); } /** * 缓存终端信息 * * @param terminalHashOperations Hash操作 * @param terminalInfo 终端信息 */ public static void putTerminalInfo(HashOperations terminalHashOperations, TerminalProto terminalInfo) { terminalHashOperations.put(TERMINAL_INFO_REDIS_KEY, String.valueOf(terminalInfo.getTerminalId()), terminalInfo); terminalHashOperations.put(SIMCARD_REDIS_KEY, String.valueOf(terminalInfo.getTerminalSimCode()), terminalInfo); terminalHashOperations.put(TERMINAL_NUM_REDIS_KEY, String.valueOf(terminalInfo.getTerminalNum()), terminalInfo); terminalHashOperations.put(VEHICLE_NUM_REDIS_KEY, String.valueOf(terminalInfo.getVehicleNum()), terminalInfo); } /** * 删除所有终端信息 * * @param terminalHashOperations Hash操作 */ public static void deleteAllTerminalInfo(HashOperations terminalHashOperations) { terminalHashOperations.entries(TERMINAL_INFO_REDIS_KEY) .forEach((k, v) -> terminalHashOperations.delete(TERMINAL_INFO_REDIS_KEY, k)); terminalHashOperations.entries(SIMCARD_REDIS_KEY) .forEach((k, v) -> terminalHashOperations.delete(SIMCARD_REDIS_KEY, k)); terminalHashOperations.entries(TERMINAL_NUM_REDIS_KEY) .forEach((k, v) -> terminalHashOperations.delete(TERMINAL_NUM_REDIS_KEY, k)); terminalHashOperations.entries(VEHICLE_NUM_REDIS_KEY) .forEach((k, v) -> terminalHashOperations.delete(VEHICLE_NUM_REDIS_KEY, k)); } /** * 获取终端信息 * * @param terminalHashOperations Hash操作 * @param terminalId 终端ID * @return 终端信息 */ public static TerminalProto getTerminalInfo(HashOperations terminalHashOperations, Long terminalId) { return terminalHashOperations.get(TERMINAL_INFO_REDIS_KEY, String.valueOf(terminalId)); } /** * 获取终端信息 * * @param terminalHashOperations Hash操作 * @param simCode 终端手机号 * @return 终端信息 */ public static TerminalProto getTerminalInfoBySimCode(HashOperations terminalHashOperations, String simCode) { return terminalHashOperations.get(SIMCARD_REDIS_KEY, simCode); } /** * 获取终端信息 * * @param terminalHashOperations Hash操作 * @param terminalNum 终端号码 * @return 终端信息 */ public static TerminalProto getTerminalInfoByTerminalNum(HashOperations terminalHashOperations, String terminalNum) { return terminalHashOperations.get(TERMINAL_NUM_REDIS_KEY, terminalNum); } /** * 获取终端信息 * * @param terminalHashOperations Hash操作 * @param vehicleNum 车牌号码 * @return 终端信息 */ public static TerminalProto getTerminalInfoByVehicleNum(HashOperations terminalHashOperations, String vehicleNum) { return terminalHashOperations.get(VEHICLE_NUM_REDIS_KEY, vehicleNum); } /** * 缓存终端位置 * * @param locationHashOperations Hash操作 * @param locationProtoDTO 位置信息 */ public static void putLastLocation(HashOperations locationHashOperations, LocationProtoDTO locationProtoDTO) { locationHashOperations.put(LOCATION_REDIS_KEY, String.valueOf(locationProtoDTO.getTerminalProto().getTerminalId()), locationProtoDTO.getLocationProto()); } /** * 获取终端最新位置 * * @param locationHashOperations Hash操作 * @param terminalId 终端ID * @return */ public static LocationProto getLastLocation(HashOperations locationHashOperations, Long terminalId) { return locationHashOperations.get(LOCATION_REDIS_KEY, String.valueOf(terminalId)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy