Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
touzizhuo03
Dubbo
提交
331b6285
D
Dubbo
项目概览
touzizhuo03
/
Dubbo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dubbo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
331b6285
编写于
7月 10, 2012
作者:
oldratlee
🔥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加简单Cache Util实现 DUBBO-475 ReferenceConfig支持按group+interface+version缓存
上级
f69c1530
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
149 addition
and
0 deletion
+149
-0
dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java
.../com/alibaba/dubbo/config/utils/ReferenceConfigCache.java
+149
-0
未找到文件。
dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java
0 → 100644
浏览文件 @
331b6285
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com.alibaba.dubbo.config.utils
;
import
com.alibaba.dubbo.common.utils.StringUtils
;
import
com.alibaba.dubbo.config.ReferenceConfig
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
/**
* a simple util class for cache {@link ReferenceConfig}.
* <p>
* {@link ReferenceConfig} is a heavy Object, it's necessary to cache these object
* for the framework which create {@link ReferenceConfig} frequently.
* <p>
* You can implement and use your own {@link ReferenceConfig} cache if you need use complicate strategy.
*
* @author ding.lid
*/
public
class
ReferenceConfigCache
{
public
static
final
String
DEFAULT_NAME
=
"_DEFAULT_"
;
private
static
final
ConcurrentMap
<
String
,
ReferenceConfigCache
>
cacheHolder
=
new
ConcurrentHashMap
<
String
,
ReferenceConfigCache
>();
/**
* Get the cache use default name and {@link #DEFAULT_KEY_GENERATOR} to generate cache key.
* Create cache if not existed yet.
*/
public
static
ReferenceConfigCache
getCache
()
{
return
getCache
(
DEFAULT_NAME
);
}
/**
* Get the cache use specified name and {@link KeyGenerator}.
* Create cache if not existed yet.
*/
public
static
ReferenceConfigCache
getCache
(
String
name
)
{
return
getCache
(
name
,
DEFAULT_KEY_GENERATOR
);
}
/**
* Get the cache use specified {@link KeyGenerator}.
* Create cache if not existed yet.
*/
public
static
ReferenceConfigCache
getCache
(
String
name
,
KeyGenerator
keyGenerator
)
{
ReferenceConfigCache
cache
=
cacheHolder
.
get
(
name
);
if
(
cache
!=
null
)
{
return
cache
;
}
cacheHolder
.
putIfAbsent
(
name
,
new
ReferenceConfigCache
(
name
,
keyGenerator
));
return
cacheHolder
.
get
(
name
);
}
public
static
interface
KeyGenerator
{
String
generateKey
(
ReferenceConfig
<?>
referenceConfig
);
}
/**
* Create the key with the <b>Group</b>, <b>Interface</b> and <b>version</b> attribute of {@link ReferenceConfig}.
* <p>
* eg: <code>group1/com.alibaba.foo.FooService:1.0.0</code>.
*/
public
static
final
KeyGenerator
DEFAULT_KEY_GENERATOR
=
new
KeyGenerator
()
{
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
();
}
};
private
final
String
name
;
private
final
KeyGenerator
generator
;
private
ConcurrentMap
<
String
,
ReferenceConfig
<?>>
cache
=
new
ConcurrentHashMap
<
String
,
ReferenceConfig
<?>>();
private
ReferenceConfigCache
(
String
name
,
KeyGenerator
generator
)
{
this
.
name
=
name
;
this
.
generator
=
generator
;
}
public
<
T
>
T
get
(
ReferenceConfig
<
T
>
referenceConfig
)
{
String
key
=
generator
.
generateKey
(
referenceConfig
);
ReferenceConfig
<?>
config
=
cache
.
get
(
key
);
if
(
config
!=
null
)
{
return
referenceConfig
.
get
();
}
cache
.
putIfAbsent
(
key
,
referenceConfig
);
config
=
cache
.
get
(
key
);
return
(
T
)
config
.
get
();
}
void
destroyKey
(
String
key
)
{
ReferenceConfig
<?>
config
=
cache
.
remove
(
key
);
if
(
config
==
null
)
return
;
config
.
destroy
();
}
/**
* clear and destroy one {@link ReferenceConfig} in the cache.
* @param referenceConfig use for create key.
*/
public
<
T
>
void
destroy
(
ReferenceConfig
<
T
>
referenceConfig
)
{
String
key
=
generator
.
generateKey
(
referenceConfig
);
destroyKey
(
key
);
}
/**
* clear and destroy all {@link ReferenceConfig} in the cache.
*/
public
void
destroyAll
()
{
Set
<
String
>
set
=
new
HashSet
<
String
>(
cache
.
keySet
());
for
(
String
key
:
set
)
{
destroyKey
(
key
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录