Skip to content

Commit

Permalink
fix issue#1293: A question for ReferenceConfigCache. (apache#3505)
Browse files Browse the repository at this point in the history
  • Loading branch information
beiwei30 authored and lixiaojiee committed Feb 19, 2019
1 parent 9031a4c commit 7c236ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,25 @@ public class ReferenceConfigCache {
* <p>
* key example: <code>group1/org.apache.dubbo.foo.FooService:1.0.0</code>.
*/
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<String, ReferenceConfigCache> cacheHolder = new ConcurrentHashMap<String, ReferenceConfigCache>();
private final String name;
Expand Down Expand Up @@ -115,6 +112,22 @@ public <T> T get(ReferenceConfig<T> 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 <code>group/interfaceClass:version</code>
*
* @param key cache key
* @param type object class
* @param <T> object type
* @return object from the cached ReferenceConfig
* @see KeyGenerator#generateKey(ReferenceConfig)
*/
@SuppressWarnings("unchecked")
public <T> T get(String key, Class<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7c236ca

Please sign in to comment.