package com.loadCenter.aliyun.gateway.cache; import com.alibaba.fastjson2.JSON; import com.loadCenter.aliyun.gateway.cache.abs.GatewayNodeCacheAbs; import com.loadCenter.aliyun.gateway.model.NodeLoadWeight; import org.springframework.stereotype.Component; import java.util.List; /** * @ClassName GatewayWeightCache * @Description 缓存IP和权重 * @Author ZeJinG.Su * @Date 22:37 2024/4/19 */ @Component public class GatewayWeightCache extends GatewayNodeCacheAbs { private final static String gatewayWeightKey = "ipAndWeight"; @Override public String getPre() { return "gateway:load:"; } /** * 增加缓存数据 * @param ipAndWeights */ public void put(List ipAndWeights) { this.remove(); redisService.setCacheList(encode(gatewayWeightKey), ipAndWeights); } /** * 获取缓存数据 * @return */ public List get() { List cacheList = redisService.getCacheList(encode(gatewayWeightKey)); return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadWeight.class); } /** * 删除缓存数据 */ public void remove() { redisService.deleteObject(encode(gatewayWeightKey)); } }