@@ -39,28 +39,25 @@ public class ReferenceConfigCache {
3939 * <p>
4040 * key example: <code>group1/org.apache.dubbo.foo.FooService:1.0.0</code>.
4141 */
42- public static final KeyGenerator DEFAULT_KEY_GENERATOR = new KeyGenerator () {
43- @ Override
44- public String generateKey (ReferenceConfig <?> referenceConfig ) {
45- String iName = referenceConfig .getInterface ();
46- if (StringUtils .isBlank (iName )) {
47- Class <?> clazz = referenceConfig .getInterfaceClass ();
48- iName = clazz .getName ();
49- }
50- if (StringUtils .isBlank (iName )) {
51- throw new IllegalArgumentException ("No interface info in ReferenceConfig" + referenceConfig );
52- }
53-
54- StringBuilder ret = new StringBuilder ();
55- if (!StringUtils .isBlank (referenceConfig .getGroup ())) {
56- ret .append (referenceConfig .getGroup ()).append ("/" );
57- }
58- ret .append (iName );
59- if (!StringUtils .isBlank (referenceConfig .getVersion ())) {
60- ret .append (":" ).append (referenceConfig .getVersion ());
61- }
62- return ret .toString ();
42+ public static final KeyGenerator DEFAULT_KEY_GENERATOR = referenceConfig -> {
43+ String iName = referenceConfig .getInterface ();
44+ if (StringUtils .isBlank (iName )) {
45+ Class <?> clazz = referenceConfig .getInterfaceClass ();
46+ iName = clazz .getName ();
6347 }
48+ if (StringUtils .isBlank (iName )) {
49+ throw new IllegalArgumentException ("No interface info in ReferenceConfig" + referenceConfig );
50+ }
51+
52+ StringBuilder ret = new StringBuilder ();
53+ if (!StringUtils .isBlank (referenceConfig .getGroup ())) {
54+ ret .append (referenceConfig .getGroup ()).append ("/" );
55+ }
56+ ret .append (iName );
57+ if (!StringUtils .isBlank (referenceConfig .getVersion ())) {
58+ ret .append (":" ).append (referenceConfig .getVersion ());
59+ }
60+ return ret .toString ();
6461 };
6562 static final ConcurrentMap <String , ReferenceConfigCache > cacheHolder = new ConcurrentHashMap <String , ReferenceConfigCache >();
6663 private final String name ;
@@ -115,6 +112,22 @@ public <T> T get(ReferenceConfig<T> referenceConfig) {
115112 return (T ) config .get ();
116113 }
117114
115+ /**
116+ * Fetch cache with the specified key. The key is decided by KeyGenerator passed-in. If the default KeyGenerator is
117+ * used, then the key is in the format of <code>group/interfaceClass:version</code>
118+ *
119+ * @param key cache key
120+ * @param type object class
121+ * @param <T> object type
122+ * @return object from the cached ReferenceConfig
123+ * @see KeyGenerator#generateKey(ReferenceConfig)
124+ */
125+ @ SuppressWarnings ("unchecked" )
126+ public <T > T get (String key , Class <T > type ) {
127+ ReferenceConfig <?> config = cache .get (key );
128+ return (config != null ) ? (T ) config .get () : null ;
129+ }
130+
118131 void destroyKey (String key ) {
119132 ReferenceConfig <?> config = cache .remove (key );
120133 if (config == null ) {
0 commit comments