在软件开发中使用缓存已经有一个非常久的历史了。缓存是一种很好的设计思想,一旦你用了他,你将会发现他确实很有用。Spring3.1版本的核心对缓存做了实现。在Java推出Annotation特性之前,实现缓存的一个难点在于它与业务逻辑代码的耦合性太强。
然而,Spring3.1中使用@Cacheable 和@CacheEvict实现缓存在某种程度上解决了这个问题,基本思想是在方法加上@Cacheable注解,这个方法的返回值将具有缓存特性。
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Cacheable
(value =
"employee"
)
public
class
EmployeeDAO {
public
Person findEmployee(String firstName, String surname,
int
age) {
return
new
Person(firstName, surname, age);
}
public
Person findAnotherEmployee(String firstName, String surname,
int
age) {
return
new
Person(firstName, surname, age);
}
}
|
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。
1
2
3
4
5
|
@Cacheable
(value =
"employee"
)
public
Person findEmployee(String firstName, String surname,
int
age) {
return
new
Person(firstName, surname, age);
}
|
任何存储在缓存中的数据为了高速访问都需要一个key。Spring默认使用被@Cacheable注解的方法的签名来作为key,当然你可以重写key,自定义key可以使用SpEL表达式。
1
2
3
4
|
<span style=
"font-size:14px;"
>
@Cacheable
(value =
"employee"
, key =
"#surname"
)</span>
public
Person findEmployeeBySurname(String firstName, String surname,
int
age) {
return
new
Person(firstName, surname, age);
}
|
在findEmployeeBySurname()的注解中"#surname"是一个SpEL表达式,他将使用findEmployeeBySurname()方法中的surname参数作为key。
@Cacheable的最后一个参数是condition(可选),同样的,也是引用一个SpEL表达式。但是这个参数将指明方法的返回结果是否被缓存。
1
2
3
4
5
|
@Cacheable
(value =
"employee"
, condition =
"#age < 25"
)
public
Person findEmployeeByAge(String firstName, String surname,
int
age) {
return
new
Person(firstName, surname, age);
}
|
1
2
3
4
5
6
7
8
|
@Test
public
void
testCache() {
Person employee1 = instance.findEmployee(
"John"
,
"Smith"
,
33
);
Person employee2 = instance.findEmployee(
"John"
,
"Smith"
,
33
);
assertEquals(employee1, employee2);
}
|
1
2
3
4
5
6
7
8
|
@Test
public
void
testCacheWithAgeAsCondition() {
Person employee1 = instance.findEmployeeByAge(
"John"
,
"Smith"
,
33
);
Person employee2 = instance.findEmployeeByAge(
"John"
,
"Smith"
,
33
);
assertEquals(employee1, employee2);
}
|
1
2
3
4
5
6
7
8
|
@Test
public
void
testCacheOnSurnameAsKey() {
Person employee1 = instance.findEmployeeBySurname(
"John"
,
"Smith"
,
22
);
Person employee2 = instance.findEmployeeBySurname(
"Jack"
,
"Smith"
,
55
);
assertEquals(employee1, employee2);
}
|