suzejing/src/main/java/com/loadCenter/aliyun/gateway/cache/GatewayWeightCache.java

50 lines
1.3 KiB
Java

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<String> {
private final static String gatewayWeightKey = "ipAndWeight";
@Override
public String getPre() {
return "gateway:load:";
}
/**
* 增加缓存数据
* @param ipAndWeights
*/
public void put(List<NodeLoadWeight> ipAndWeights) {
this.remove();
redisService.setCacheList(encode(gatewayWeightKey), ipAndWeights);
}
/**
* 获取缓存数据
* @return
*/
public List<NodeLoadWeight> get() {
List<Object> cacheList = redisService.getCacheList(encode(gatewayWeightKey));
return JSON.parseArray(JSON.toJSONString(cacheList), NodeLoadWeight.class);
}
/**
* 删除缓存数据
*/
public void remove() {
redisService.deleteObject(encode(gatewayWeightKey));
}
}