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

61 lines
1.4 KiB
Java

package com.lyh.gateway.cache;
import com.alibaba.fastjson2.JSON;
import com.lyh.gateway.cache.abs.GatewayCacheAbs;
import com.lyh.gateway.mode.IpAndWeight;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 缓存IP和权重
*
* @ProjectName: LoadCenter
* @Author: LiuYunHu
* @CreateTime: 2024/4/18
*/
@Component
public class GatewayIpAndLoadWeightCache extends GatewayCacheAbs<String> {
private final static String gatewayIpAndWeightKey = "ipAndWeight";
@Override
public String getPre() {
return "gateway:load:";
}
/**
* 增加缓存数据
*
* @Date: 2024/4/18 21:37
* @Param: [gatewayNodeIps]
* @Return: void
**/
public void put(List<IpAndWeight> ipAndWeights) {
this.remove();
redisService.setCacheList(encode(gatewayIpAndWeightKey), ipAndWeights);
}
/**
* 获取缓存数据
*
* @Date: 2024/4/18 21:37
* @Param: []
* @Return: java.util.List<java.lang.String>
**/
public List<IpAndWeight> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayIpAndWeightKey));
return JSON.parseArray(JSON.toJSONString(cacheList), IpAndWeight.class);
}
/**
* 删除缓存数据
*
* @Date: 2024/4/18 21:37
* @Param: []
* @Return: void
**/
public void remove() {
redisService.deleteObject(encode(gatewayIpAndWeightKey));
}
}