From 7c236ca8f74e6797dac4d432a37294ee8a4529d5 Mon Sep 17 00:00:00 2001 From: Ian Luo Date: Tue, 19 Feb 2019 23:09:54 +0800 Subject: [PATCH] fix issue#1293: A question for ReferenceConfigCache. (#3505) --- .../config/utils/ReferenceConfigCache.java | 55 ++++++++++++------- .../utils/ReferenceConfigCacheTest.java | 8 +++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java index 5ca96987242..09f12dadeca 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/utils/ReferenceConfigCache.java @@ -39,28 +39,25 @@ public class ReferenceConfigCache { *

* key example: group1/org.apache.dubbo.foo.FooService:1.0.0. */ - public static final KeyGenerator DEFAULT_KEY_GENERATOR = new KeyGenerator() { - @Override - public String generateKey(ReferenceConfig referenceConfig) { - String iName = referenceConfig.getInterface(); - if (StringUtils.isBlank(iName)) { - Class clazz = referenceConfig.getInterfaceClass(); - iName = clazz.getName(); - } - if (StringUtils.isBlank(iName)) { - throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig); - } - - StringBuilder ret = new StringBuilder(); - if (!StringUtils.isBlank(referenceConfig.getGroup())) { - ret.append(referenceConfig.getGroup()).append("/"); - } - ret.append(iName); - if (!StringUtils.isBlank(referenceConfig.getVersion())) { - ret.append(":").append(referenceConfig.getVersion()); - } - return ret.toString(); + public static final KeyGenerator DEFAULT_KEY_GENERATOR = referenceConfig -> { + String iName = referenceConfig.getInterface(); + if (StringUtils.isBlank(iName)) { + Class clazz = referenceConfig.getInterfaceClass(); + iName = clazz.getName(); } + if (StringUtils.isBlank(iName)) { + throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig); + } + + StringBuilder ret = new StringBuilder(); + if (!StringUtils.isBlank(referenceConfig.getGroup())) { + ret.append(referenceConfig.getGroup()).append("/"); + } + ret.append(iName); + if (!StringUtils.isBlank(referenceConfig.getVersion())) { + ret.append(":").append(referenceConfig.getVersion()); + } + return ret.toString(); }; static final ConcurrentMap cacheHolder = new ConcurrentHashMap(); private final String name; @@ -115,6 +112,22 @@ public T get(ReferenceConfig referenceConfig) { return (T) config.get(); } + /** + * Fetch cache with the specified key. The key is decided by KeyGenerator passed-in. If the default KeyGenerator is + * used, then the key is in the format of group/interfaceClass:version + * + * @param key cache key + * @param type object class + * @param object type + * @return object from the cached ReferenceConfig + * @see KeyGenerator#generateKey(ReferenceConfig) + */ + @SuppressWarnings("unchecked") + public T get(String key, Class type) { + ReferenceConfig config = cache.get(key); + return (config != null) ? (T) config.get() : null; + } + void destroyKey(String key) { ReferenceConfig config = cache.remove(key); if (config == null) { diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java index af5659eb2b7..2af6da720ec 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/utils/ReferenceConfigCacheTest.java @@ -58,6 +58,14 @@ public void testGetCacheDiffReference() throws Exception { assertEquals("1", value); } + @Test + public void testGetCacheWithKey() throws Exception { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + MockReferenceConfig config = buildMockReferenceConfig("FooService", "group1", "1.0.0"); + String value = cache.get(config); + assertEquals(value, cache.get("group1/FooService:1.0.0", String.class)); + } + @Test public void testGetCacheDiffName() throws Exception { ReferenceConfigCache cache = ReferenceConfigCache.getCache();