王振威 在 Spring 3.1 中使用 @Cacheable 实现缓存
2012-10-23 16:04
513 查看
在软件开发中使用缓存已经有一个非常久的历史了。缓存是一种很好的设计思想,一旦你用了他,你将会发现他确实很有用。Spring3.1版本的核心对缓存做了实现。在Java推出Annotation特性之前,实现缓存的一个难点在于它与业务逻辑代码的耦合性太强。
然而,Spring3.1中使用@Cacheable和@CacheEvict实现缓存在某种程度上解决了这个问题,基本思想是在方法加上@Cacheable注解,这个方法的返回值将具有缓存特性。
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
@Cacheable注解有三个参数,value是必须的,还有key和condition。第一个参数,也就是value指明了缓存将被存到什么地方。
上面的代码保证findEmployee的返回值Person对象将被存储在"employee"中。
任何存储在缓存中的数据为了高速访问都需要一个key。Spring默认使用被@Cacheable注解的方法的签名来作为key,当然你可以重写key,自定义key可以使用SpEL表达式。
在findEmployeeBySurname()的注解中"#surname"是一个SpEL表达式,他将使用findEmployeeBySurname()方法中的surname参数作为key。
@Cacheable的最后一个参数是condition(可选),同样的,也是引用一个SpEL表达式。但是这个参数将指明方法的返回结果是否被缓存。
上面的例子中,只有年龄小于25的时候才被缓存。
在快速看完了如何使用缓存后,我们接下来看看缓存带来的效果。
上面的例子很简单,第一次调用findEmployee,findEmployee方法将被执行,Spring将他的返回值一个person对象存入缓存。第二次调用findEmployee的时候findEmployee将不被执行,Spring直接将缓存中的数据作为返回值返回。所以employee1和employee2引用了同样的对象。
而下面的例子中,我们将年龄小于25作为缓存条件,就将得到不同的结果。
下面的例子我们在findEmployeeBySurname的方法的注解中自定义了key,我们使用了自定义的key生成方式,以确保不同的surname将会指向不同的人。看下面的程序
我们想找到两个不同的人,但是两个人的surname是相同的,你将发现两次调用返回了相同的结果,这不是Spring的问题,而是我们的cachekey的生成方式有问题。所以在我们定义key的时候要小心注意key的生成策略,避免造成这种问题。
最后总结一下流程,当执行到一个被@Cacheable注解的方法时,Spring首先检查condition条件是否满足,如果不满足,执行方法,返回;如果满足,在value所命名的缓存空间中查找使用key存储的对象,如果找到,将找到的结果返回,如果没有找到执行方法,将方法的返回值以key-对象的方式存入value缓存中,然后方法返回。
上文仅仅是@Cacheable的使用方法,但是我们怎么使用@CacheEvict注解来清除缓存呢?另外,还有一个问题,如何选择一个缓存的实现,并配置Spring的缓存呢?欲知后事如何,且听下回分解。
然而,Spring3.1中使用@Cacheable和@CacheEvict实现缓存在某种程度上解决了这个问题,基本思想是在方法加上@Cacheable注解,这个方法的返回值将具有缓存特性。
@Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了。当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
01 | @Cacheable (value= "employee" ) |
02 | public class EmployeeDAO{ |
03 |
04 | public PersonfindEmployee(StringfirstName,Stringsurname, int age){ |
05 |
06 | return new Person(firstName,surname,age); |
07 | } |
08 |
09 | public PersonfindAnotherEmployee(StringfirstName,Stringsurname, int age){ |
10 |
11 | return new Person(firstName,surname,age); |
12 | } |
13 | } |
1 | @Cacheable (value= "employee" ) |
2 | public PersonfindEmployee(StringfirstName,Stringsurname, int age){ |
3 |
4 | return new Person(firstName,surname,age); |
5 | } |
任何存储在缓存中的数据为了高速访问都需要一个key。Spring默认使用被@Cacheable注解的方法的签名来作为key,当然你可以重写key,自定义key可以使用SpEL表达式。
1 | <spanstyle= "font-size:14px;" > @Cacheable (value= "employee" ,key= "#surname" )</span> public PersonfindEmployeeBySurname(StringfirstName,Stringsurname, int age){ |
2 |
3 | return new Person(firstName,surname,age); |
4 | } |
@Cacheable的最后一个参数是condition(可选),同样的,也是引用一个SpEL表达式。但是这个参数将指明方法的返回结果是否被缓存。
1 | @Cacheable (value= "employee" ,condition= "#age<25" ) |
2 | public PersonfindEmployeeByAge(StringfirstName,Stringsurname, int age){ |
3 |
4 | return new Person(firstName,surname,age); |
5 | } |
在快速看完了如何使用缓存后,我们接下来看看缓存带来的效果。
1 | <ahref= "http://my.oschina.net/test45" target= "_blank" rel= "nofollow" > @Test </a> |
2 | public void testCache(){ |
3 |
4 | Personemployee1=instance.findEmployee( "John" , "Smith" , 33 ); |
5 | Personemployee2=instance.findEmployee( "John" , "Smith" , 33 ); |
6 |
7 | assertEquals(employee1,employee2); |
8 | } |
而下面的例子中,我们将年龄小于25作为缓存条件,就将得到不同的结果。
1 | <ahref= "http://my.oschina.net/test45" target= "_blank" rel= "nofollow" > @Test </a> |
2 | public void testCacheWithAgeAsCondition(){ |
3 |
4 | Personemployee1=instance.findEmployeeByAge( "John" , "Smith" , 33 ); |
5 | Personemployee2=instance.findEmployeeByAge( "John" , "Smith" , 33 ); |
6 |
7 | assertEquals(employee1,employee2); |
8 | } |
1 | <ahref= "http://my.oschina.net/test45" target= "_blank" rel= "nofollow" > @Test </a> |
2 | public void testCacheOnSurnameAsKey(){ |
3 |
4 | Personemployee1=instance.findEmployeeBySurname( "John" , "Smith" , 22 ); |
5 | Personemployee2=instance.findEmployeeBySurname( "Jack" , "Smith" , 55 ); |
6 |
7 | assertEquals(employee1,employee2); |
8 | } |
最后总结一下流程,当执行到一个被@Cacheable注解的方法时,Spring首先检查condition条件是否满足,如果不满足,执行方法,返回;如果满足,在value所命名的缓存空间中查找使用key存储的对象,如果找到,将找到的结果返回,如果没有找到执行方法,将方法的返回值以key-对象的方式存入value缓存中,然后方法返回。
上文仅仅是@Cacheable的使用方法,但是我们怎么使用@CacheEvict注解来清除缓存呢?另外,还有一个问题,如何选择一个缓存的实现,并配置Spring的缓存呢?欲知后事如何,且听下回分解。
相关文章推荐
- 在 Spring 3.1 中使用 @Cacheable 实现缓存
- Spring 3.1 中使用 @Cacheable 实现缓存
- 在 Spring 3.1 中使用 @Cacheable 实现缓存
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用
- @Cacheable注解在spring3中的使用-实现缓存
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- @Cacheable注解在spring3中的使用-实现缓存
- @Cacheable注解在spring3中的使用-实现缓存
- 《partner4java 讲述Spring入门》之:spring cache支持(spring3.1如何使用cache 缓存)
- 关于Spring 如何使用@cacheable、@cacheEvict、@cacheput操作缓存
- Spring 3.1 中使用 @Cacheable
- @Cacheable注解在spring3中的使用-实现缓存
- 8 -- 深入使用Spring -- 5...2 使用@Cacheable执行缓存
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- spring cache支持(spring3.1如何使用cache 缓存)
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用