Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
ff26dfdd
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
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,发现更多精彩内容 >>
提交
ff26dfdd
编写于
1月 03, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added cache for path pattern tokenization
Issue: SPR-11258
上级
240819f9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
55 addition
and
11 deletion
+55
-11
spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
...rc/main/java/org/springframework/util/AntPathMatcher.java
+55
-11
未找到文件。
spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
浏览文件 @
ff26dfdd
...
...
@@ -50,13 +50,13 @@ import java.util.regex.Pattern;
*/
public
class
AntPathMatcher
implements
PathMatcher
{
/** Default path separator: "/" */
public
static
final
String
DEFAULT_PATH_SEPARATOR
=
"/"
;
private
static
final
int
CACHE_TURNOFF_THRESHOLD
=
65536
;
private
static
final
Pattern
VARIABLE_PATTERN
=
Pattern
.
compile
(
"\\{[^/]+?\\}"
);
/** Default path separator: "/" */
public
static
final
String
DEFAULT_PATH_SEPARATOR
=
"/"
;
private
String
pathSeparator
=
DEFAULT_PATH_SEPARATOR
;
...
...
@@ -64,8 +64,9 @@ public class AntPathMatcher implements PathMatcher {
private
volatile
Boolean
cachePatterns
;
final
Map
<
String
,
AntPathStringMatcher
>
stringMatcherCache
=
new
ConcurrentHashMap
<
String
,
AntPathStringMatcher
>(
256
);
private
final
Map
<
String
,
String
[]>
tokenizedPatternCache
=
new
ConcurrentHashMap
<
String
,
String
[]>(
256
);
final
Map
<
String
,
AntPathStringMatcher
>
stringMatcherCache
=
new
ConcurrentHashMap
<
String
,
AntPathStringMatcher
>(
256
);
/**
...
...
@@ -81,7 +82,7 @@ public class AntPathMatcher implements PathMatcher {
* Default is {@code true}.
*/
public
void
setTrimTokens
(
boolean
trimTokens
)
{
this
.
trimTokens
=
trimTokens
;
this
.
trimTokens
=
trimTokens
;
}
/**
...
...
@@ -99,6 +100,12 @@ public class AntPathMatcher implements PathMatcher {
this
.
cachePatterns
=
cachePatterns
;
}
private
void
deactivatePatternCache
()
{
this
.
cachePatterns
=
false
;
this
.
tokenizedPatternCache
.
clear
();
this
.
stringMatcherCache
.
clear
();
}
@Override
public
boolean
isPattern
(
String
path
)
{
...
...
@@ -128,8 +135,8 @@ public class AntPathMatcher implements PathMatcher {
return
false
;
}
String
[]
pattDirs
=
StringUtils
.
tokenizeToStringArray
(
pattern
,
this
.
pathSeparator
,
this
.
trimTokens
,
true
);
String
[]
pathDirs
=
StringUtils
.
tokenizeToStringArray
(
path
,
this
.
pathSeparator
,
this
.
trimTokens
,
true
);
String
[]
pattDirs
=
tokenizePattern
(
pattern
);
String
[]
pathDirs
=
tokenizePath
(
path
);
int
pattIdxStart
=
0
;
int
pattIdxEnd
=
pattDirs
.
length
-
1
;
...
...
@@ -248,6 +255,44 @@ public class AntPathMatcher implements PathMatcher {
return
true
;
}
/**
* Tokenize the given path pattern into parts, based on this matcher's settings.
* <p>Performs caching based on {@link #setCachePatterns}, delegating to
* {@link #tokenizePath(String)} for the actual tokenization algorithm.
* @param pattern the pattern to tokenize
* @return the tokenized pattern parts
*/
protected
String
[]
tokenizePattern
(
String
pattern
)
{
String
[]
tokenized
=
null
;
Boolean
cachePatterns
=
this
.
cachePatterns
;
if
(
cachePatterns
==
null
||
cachePatterns
.
booleanValue
())
{
tokenized
=
this
.
tokenizedPatternCache
.
get
(
pattern
);
}
if
(
tokenized
==
null
)
{
tokenized
=
tokenizePath
(
pattern
);
if
(
cachePatterns
==
null
&&
this
.
tokenizedPatternCache
.
size
()
>=
CACHE_TURNOFF_THRESHOLD
)
{
// Try to adapt to the runtime situation that we're encountering:
// There are obviously too many different patterns coming in here...
// So let's turn off the cache since the patterns are unlikely to be reoccurring.
deactivatePatternCache
();
return
tokenized
;
}
if
(
cachePatterns
==
null
||
cachePatterns
.
booleanValue
())
{
this
.
tokenizedPatternCache
.
put
(
pattern
,
tokenized
);
}
}
return
tokenized
;
}
/**
* Tokenize the given path String into parts, based on this matcher's settings.
* @param path the path to tokenize
* @return the tokenized path parts
*/
protected
String
[]
tokenizePath
(
String
path
)
{
return
StringUtils
.
tokenizeToStringArray
(
path
,
this
.
pathSeparator
,
this
.
trimTokens
,
true
);
}
/**
* Tests whether or not a string matches against a pattern.
* @param pattern the pattern to match against (never {@code null})
...
...
@@ -281,10 +326,9 @@ public class AntPathMatcher implements PathMatcher {
matcher
=
new
AntPathStringMatcher
(
pattern
);
if
(
cachePatterns
==
null
&&
this
.
stringMatcherCache
.
size
()
>=
CACHE_TURNOFF_THRESHOLD
)
{
// Try to adapt to the runtime situation that we're encountering:
// There are obviously too many different pat
h
s coming in here...
// There are obviously too many different pat
tern
s coming in here...
// So let's turn off the cache since the patterns are unlikely to be reoccurring.
this
.
cachePatterns
=
false
;
this
.
stringMatcherCache
.
clear
();
deactivatePatternCache
();
return
matcher
;
}
if
(
cachePatterns
==
null
||
cachePatterns
.
booleanValue
())
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录