LoadCenter/src/main/java/com/lyh/gateway/cache/GatewayNodeInfoCache.java

63 lines
1.5 KiB
Java

package com.lyh.gateway.cache;
import com.alibaba.fastjson2.JSON;
import com.lyh.gateway.cache.abs.GatewayCacheAbs;
import com.lyh.gateway.mode.GatewayNodeInfo;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @ProjectName: LoadCenter
* @Author: LiuYunHu
* @CreateTime: 2024/4/18
* @Description: 网关节点缓存
*/
@Component
public class GatewayNodeInfoCache extends GatewayCacheAbs<String> {
//redis Key
private final static String gatewayLoadInfoKey = "NodeInfo";
@Override
public String getPre() {
return "gateway:load:";
}
/*
* @Author: LiuYunHu
* @Date: 2024/4/18 15:16
* @Description: 增加缓存数据
* @Param: 节点信息
* @Return: void
**/
public void put(List<GatewayNodeInfo> gatewayNodeInfos) {
this.remove();
redisService.setCacheList(encode(gatewayLoadInfoKey), gatewayNodeInfos);
}
/*
* @Author: LiuYunHu
* @Date: 2024/4/18 15:19
* @Description: 获取缓存数据
* @Param: 节点id
* @Return: 节点信息
**/
public List<GatewayNodeInfo> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayLoadInfoKey));
return JSON.parseArray(JSON.toJSONString(cacheList), GatewayNodeInfo.class);
}
/*
* @Author: LiuYunHu
* @Date: 2024/4/18 15:19
* @Description: 删除缓存数据
* @Param: 节点id
* @Return: void
**/
public void remove() {
redisService.deleteObject(encode(gatewayLoadInfoKey));
}
}