1. 程式人生 > >api-gateway-engine知識點(2)

api-gateway-engine知識點(2)

val oca ins exc finally jedis throws import nec

GroupVersion實現engine本地緩存

package com.inspur.cloud.apigw.engine.cache;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


import com.inspur.cloud.apigw.engine.common.RedisCachePrefix;
import com.inspur.cloud.apigw.engine.common.RedisConnectionPool;

import redis.clients.jedis.Jedis;

public class GroupVersionCache {

private static ConcurrentHashMap<String, ConcurrentHashMap<String, String>> cache = new ConcurrentHashMap<String, ConcurrentHashMap<String, String>>();

private GroupVersionCache() {

}

// 初始化redis中的Frtkey到本地cache中
public static void initCache() throws Exception {
Jedis jedis = RedisConnectionPool.getConnection();
try {
Set<String> keys = jedis.keys("API:GrpVer:*");
for (String key : keys) {
ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
Map<String, String> value = jedis.hgetAll(key);
concurrentHashMap.putAll(value);
cache.put(key, concurrentHashMap);
}
} catch (Exception e) {
throw e;
} finally {
jedis.close();
}
}

// 在本地緩存中,通過groupCode和versionCode獲取VersionInfo
public static ConcurrentHashMap<String, String> getVersionInfoByCache(String groupCode, String versionCode)
throws Exception {
try {
String grpVersionKey = RedisCachePrefix.getApiGrpVerKey(groupCode, versionCode);
return cache.get(grpVersionKey);
} catch (Exception e) {
throw e;
}
}

// 版本上線時,向engine本地中增加綁定關系緩存
public static void addVersionInfoToCache(String messageKey, Map<String, String> map) throws Exception {
try {
ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
concurrentHashMap.putAll(map);
cache.put(messageKey, concurrentHashMap);
} catch (Exception e) {
throw e;
}
}

// 下線時,刪除本地緩存中的某條數據
public static void deleteCacheBykey(String messageKey) throws Exception {
try {
cache.remove(messageKey);
} catch (Exception e) {
throw e;
}
}

}

為什麽要用 conCurrentHashMap?Map或者HashMap也可以吧?

api-gateway-engine知識點(2)