Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
chenpeng_fei
spring-framework
提交
75ea6f56
S
spring-framework
项目概览
chenpeng_fei
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
75ea6f56
编写于
9月 24, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revised AbstractCacheManager for consistent locking when caches get added
Issue: SPR-13492
上级
1458c7ed
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
78 addition
and
19 deletion
+78
-19
spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java
...g/springframework/cache/support/AbstractCacheManager.java
+78
-19
未找到文件。
spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java
浏览文件 @
75ea6f56
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -40,20 +40,36 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
private
final
ConcurrentMap
<
String
,
Cache
>
cacheMap
=
new
ConcurrentHashMap
<
String
,
Cache
>(
16
);
private
Set
<
String
>
cacheNames
=
new
LinkedHashSet
<
String
>(
16
);
private
volatile
Set
<
String
>
cacheNames
=
Collections
.
emptySet
(
);
// Early cache initialization on startup
@Override
public
void
afterPropertiesSet
()
{
initializeCaches
();
}
/**
* Initialize the static configuration of caches.
* <p>Triggered on startup through {@link #afterPropertiesSet()};
* can also be called to re-initialize at runtime.
* @since 4.2.2
* @see #loadCaches()
*/
public
void
initializeCaches
()
{
Collection
<?
extends
Cache
>
caches
=
loadCaches
();
// Preserve the initial order of the cache names
synchronized
(
this
.
cacheMap
)
{
this
.
cacheNames
=
Collections
.
emptySet
();
this
.
cacheMap
.
clear
();
this
.
cacheNames
.
clear
(
);
Set
<
String
>
cacheNames
=
new
LinkedHashSet
<
String
>(
caches
.
size
()
);
for
(
Cache
cache
:
caches
)
{
addCache
(
cache
);
String
name
=
cache
.
getName
();
this
.
cacheMap
.
put
(
name
,
decorateCache
(
cache
));
cacheNames
.
add
(
name
);
}
this
.
cacheNames
=
Collections
.
unmodifiableSet
(
cacheNames
);
}
}
...
...
@@ -69,37 +85,79 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
@Override
public
Cache
getCache
(
String
name
)
{
Cache
cache
=
lookupCache
(
name
);
Cache
cache
=
this
.
cacheMap
.
get
(
name
);
if
(
cache
!=
null
)
{
return
cache
;
}
else
{
Cache
missingCache
=
getMissingCache
(
name
);
if
(
missingCache
!=
null
)
{
addCache
(
missingCache
);
return
lookupCache
(
name
);
// may be decorated
// Fully synchronize now for missing cache creation...
synchronized
(
this
.
cacheMap
)
{
cache
=
this
.
cacheMap
.
get
(
name
);
if
(
cache
==
null
)
{
cache
=
getMissingCache
(
name
);
if
(
cache
!=
null
)
{
cache
=
decorateCache
(
cache
);
this
.
cacheMap
.
put
(
name
,
cache
);
updateCacheNames
(
name
);
}
}
return
cache
;
}
return
null
;
}
}
@Override
public
Collection
<
String
>
getCacheNames
()
{
return
Collections
.
unmodifiableSet
(
this
.
cacheNames
)
;
return
this
.
cacheNames
;
}
// Common cache initialization delegates
/callback
s
// Common cache initialization delegates
for subclasse
s
/**
* Check for a registered cache of the given name.
* In contrast to {@link #getCache(String)}, this method does not trigger
* the lazy creation of missing caches via {@link #getMissingCache(String)}.
* @param name the cache identifier (must not be {@code null})
* @return the associated Cache instance, or {@code null} if none found
* @since 4.1
* @see #getCache(String)
* @see #getMissingCache(String)
*/
protected
final
Cache
lookupCache
(
String
name
)
{
return
this
.
cacheMap
.
get
(
name
);
}
/**
* Dynamically register an additional Cache with this manager.
* @param cache the Cache to register
*/
protected
final
void
addCache
(
Cache
cache
)
{
this
.
cacheMap
.
put
(
cache
.
getName
(),
decorateCache
(
cache
));
this
.
cacheNames
.
add
(
cache
.
getName
());
String
name
=
cache
.
getName
();
synchronized
(
this
.
cacheMap
)
{
if
(
this
.
cacheMap
.
put
(
name
,
decorateCache
(
cache
))
==
null
)
{
updateCacheNames
(
name
);
}
}
}
protected
final
Cache
lookupCache
(
String
name
)
{
return
this
.
cacheMap
.
get
(
name
);
/**
* Update the exposed {@link #cacheNames} set with the given name.
* <p>This will always be called within a full {@link #cacheMap} lock
* and effectively behaves like a {@code CopyOnWriteArraySet} with
* preserved order but exposed as an unmodifiable reference.
* @param name the name of the cache to be added
*/
private
void
updateCacheNames
(
String
name
)
{
Set
<
String
>
cacheNames
=
new
LinkedHashSet
<
String
>(
this
.
cacheNames
.
size
()
+
1
);
cacheNames
.
addAll
(
this
.
cacheNames
);
cacheNames
.
add
(
name
);
this
.
cacheNames
=
Collections
.
unmodifiableSet
(
cacheNames
);
}
// Overridable template methods for cache initialization
/**
* Decorate the given Cache object if necessary.
* @param cache the Cache object to be added to this CacheManager
...
...
@@ -120,6 +178,7 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
* @param name the name of the cache to retrieve
* @return the missing cache or {@code null} if no such cache exists or could be
* created
* @since 4.1
* @see #getCache(String)
*/
protected
Cache
getMissingCache
(
String
name
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录