zoukankan      html  css  js  c++  java
  • api-gateway-engine知识点(2)

    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也可以吧?

  • 相关阅读:
    Spring AOP 随记
    Java设计模式系列 — 构造器模式
    【Java线程安全】 — 常用数据结构及原理(未完结)
    【最佳实践】好用的Quartz管理器类
    Timer和时间调度
    Java9之HashMap与ConcurrentHashMap
    记一次maven的包冲突经历
    hbase高可用集群部署(cdh)
    HBase 1.2.6 完全分布式集群安装部署详细过程
    hadoop-2.7.3完全分布式部署
  • 原文地址:https://www.cnblogs.com/myfrank/p/7800745.html
Copyright © 2011-2022 走看看