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 { 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 ipAndWeights) { this.remove(); redisService.setCacheList(encode(gatewayIpAndWeightKey), ipAndWeights); } /** * 获取缓存数据 * * @Date: 2024/4/18 21:37 * @Param: [] * @Return: java.util.List **/ public List get() { List 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)); } }