Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
faef363e
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,发现更多精彩内容 >>
提交
faef363e
编写于
7月 16, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Evaluate @Cacheable(condition) once per method invocation only
Issue: SPR-17024
上级
93d91219
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
9 deletion
+66
-9
spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java
...springframework/cache/interceptor/CacheAspectSupport.java
+13
-5
spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
...framework/cache/config/EnableCachingIntegrationTests.java
+53
-4
未找到文件。
spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java
浏览文件 @
faef363e
...
...
@@ -693,6 +693,9 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
private
final
Collection
<
String
>
cacheNames
;
@Nullable
private
Boolean
conditionPassing
;
public
CacheOperationContext
(
CacheOperationMetadata
metadata
,
Object
[]
args
,
Object
target
)
{
this
.
metadata
=
metadata
;
this
.
args
=
extractArgs
(
metadata
.
method
,
args
);
...
...
@@ -733,12 +736,17 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
}
protected
boolean
isConditionPassing
(
@Nullable
Object
result
)
{
if
(
StringUtils
.
hasText
(
this
.
metadata
.
operation
.
getCondition
()))
{
EvaluationContext
evaluationContext
=
createEvaluationContext
(
result
);
return
evaluator
.
condition
(
this
.
metadata
.
operation
.
getCondition
(),
this
.
metadata
.
methodKey
,
evaluationContext
);
if
(
this
.
conditionPassing
==
null
)
{
if
(
StringUtils
.
hasText
(
this
.
metadata
.
operation
.
getCondition
()))
{
EvaluationContext
evaluationContext
=
createEvaluationContext
(
result
);
this
.
conditionPassing
=
evaluator
.
condition
(
this
.
metadata
.
operation
.
getCondition
(),
this
.
metadata
.
methodKey
,
evaluationContext
);
}
else
{
this
.
conditionPassing
=
true
;
}
}
return
t
rue
;
return
t
his
.
conditionPassing
;
}
protected
boolean
canPutToCache
(
@Nullable
Object
value
)
{
...
...
spring-context/src/test/java/org/springframework/cache/config/EnableCachingIntegrationTests.java
浏览文件 @
faef363e
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicLong;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.CacheTestUtils
;
...
...
@@ -33,7 +34,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.core.env.Environment
;
import
org.springframework.mock.env.MockEnvironment
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
cache
.
CacheTestUtils
.*;
/**
...
...
@@ -45,6 +49,7 @@ public class EnableCachingIntegrationTests {
private
ConfigurableApplicationContext
context
;
@After
public
void
closeContext
()
{
if
(
this
.
context
!=
null
)
{
...
...
@@ -52,6 +57,7 @@ public class EnableCachingIntegrationTests {
}
}
@Test
public
void
fooServiceWithInterface
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
FooConfig
.
class
);
...
...
@@ -77,22 +83,48 @@ public class EnableCachingIntegrationTests {
}
@Test
public
void
beanCondition
()
{
public
void
beanCondition
Off
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
BeanConditionConfig
.
class
);
Cache
cache
=
getCache
();
FooService
service
=
this
.
context
.
getBean
(
FooService
.
class
);
Cache
cache
=
getCache
();
Object
key
=
new
Object
();
service
.
getWithCondition
(
key
);
assertCacheMiss
(
key
,
cache
);
service
.
getWithCondition
(
key
);
assertCacheMiss
(
key
,
cache
);
assertEquals
(
2
,
this
.
context
.
getBean
(
BeanConditionConfig
.
Bar
.
class
).
count
);
}
@Test
public
void
beanConditionOn
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
setEnvironment
(
new
MockEnvironment
().
withProperty
(
"bar.enabled"
,
"true"
));
ctx
.
register
(
BeanConditionConfig
.
class
);
ctx
.
refresh
();
this
.
context
=
ctx
;
FooService
service
=
this
.
context
.
getBean
(
FooService
.
class
);
Cache
cache
=
getCache
();
Object
key
=
new
Object
();
Object
value
=
service
.
getWithCondition
(
key
);
assertCacheHit
(
key
,
value
,
cache
);
value
=
service
.
getWithCondition
(
key
);
assertCacheHit
(
key
,
value
,
cache
);
assertEquals
(
2
,
this
.
context
.
getBean
(
BeanConditionConfig
.
Bar
.
class
).
count
);
}
private
Cache
getCache
()
{
return
this
.
context
.
getBean
(
CacheManager
.
class
).
getCache
(
"testCache"
);
}
@Configuration
static
class
SharedConfig
extends
CachingConfigurerSupport
{
@Override
@Bean
public
CacheManager
cacheManager
()
{
...
...
@@ -100,34 +132,42 @@ public class EnableCachingIntegrationTests {
}
}
@Configuration
@Import
(
SharedConfig
.
class
)
@EnableCaching
static
class
FooConfig
{
@Bean
public
FooService
fooService
()
{
return
new
FooServiceImpl
();
}
}
@Configuration
@Import
(
SharedConfig
.
class
)
@EnableCaching
(
proxyTargetClass
=
true
)
static
class
FooConfigCglib
{
@Bean
public
FooService
fooService
()
{
return
new
FooServiceImpl
();
}
}
private
interface
FooService
{
Object
getSimple
(
Object
key
);
Object
getWithCondition
(
Object
key
);
}
@CacheConfig
(
cacheNames
=
"testCache"
)
private
static
class
FooServiceImpl
implements
FooService
{
private
final
AtomicLong
counter
=
new
AtomicLong
();
@Override
...
...
@@ -143,17 +183,25 @@ public class EnableCachingIntegrationTests {
}
}
@Configuration
@Import
(
FooConfig
.
class
)
@EnableCaching
static
class
BeanConditionConfig
{
@Autowired
Environment
env
;
@Bean
public
Bar
bar
()
{
return
new
Bar
(
false
);
return
new
Bar
(
Boolean
.
valueOf
(
env
.
getProperty
(
"bar.enabled"
))
);
}
static
class
Bar
{
public
int
count
;
private
final
boolean
enabled
;
public
Bar
(
boolean
enabled
)
{
...
...
@@ -161,6 +209,7 @@ public class EnableCachingIntegrationTests {
}
public
boolean
isEnabled
()
{
this
.
count
++;
return
this
.
enabled
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录