`
m635674608
  • 浏览: 5062014 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

spring3 自带 cache 整合之方法缓存

 
阅读更多

最近项目刚好用的spring版本是Spring3.1.M1 ,好像 spring3就开始有了对缓存的整合。其实底层的实现也就是上篇文件一样。http://m635674608.iteye.com/admin/blogs/1678983

spring3.1.M1中负责cache的模块是org.springframework.context-3.1.0.M1.jar

2.5时的modules模块类似,3.1的注解缓存也是在方法上声明注解,3.1同样提供了两个注解:

@Cacheable:负责将方法的返回值加入到缓存中

@CacheEvict:负责清除缓存

@Cacheable 支持如下几个参数:

value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cachename

key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL

condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL

1 //将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key

2 @Cacheable(value="andCache",key="#userId + 'findById'")

3 public SystemUser findById(String userId) {

4 SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);

5 return user ;

6 }

7 //将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key

8 @Cacheable(value="andCache",condition="#userId.length < 32")

9 public boolean isReserved(String userId) {

10 System.out.println("hello andCache"+userId);

11 return false;

12 }

@CacheEvict 支持如下几个参数:

value:缓存位置名称,不能为空,同上

key:缓存的key,默认为空,同上

condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL

allEntriestrue表示清除value中的全部缓存,默认为false

//清除掉指定key的缓存
@CacheEvict(value="andCache",key="#user.userId + 'findById'")
public void EvictUserRole(SystemUser user) {
System.out.println("hello andCache delete"+user.getUserId());
}

//清除掉全部缓存
@CacheEvict(value="andCache",allEntries=true)
public final void EvictUsers(String[] reservedUsers) {
System.out.println("hello andCache deleteall");
}

Spring3集成了ehcache缓存。

第一步:配置Spring文件,声明支持缓存和支持注解

13 <?xml version="1.0" encoding="UTF-8"?>

14 <beans xmlns="http://www.springframework.org/schema/beans"

15 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

16 xmlns:cache="http://www.springframework.org/schema/cache"

17 xsi:schemaLocation="

18 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

19 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

20

21 <!-- 缓存注解声明,使用注解缓存 -->

22 <cache:annotation-driven cache-manager="cacheManager" />

23

24 <!-- 指定ehcache.xml的位置 -->

25 <bean id="cacheManagerFactory"

26 class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"

27 p:configLocation="classpath:/ehcache.xml" />

28

29 <!-- 声明缓存Manager -->

30 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"

31 p:cacheManager-ref="cacheManagerFactory" />

32

33 </beans>

第二步:配置Ehcache文件,定义缓存的具体策略。

34

35

36 <?xml version="1.0" encoding="UTF-8"?>

37

38

39

40

41

42 <ehcache>

43

44

45 <diskStore path="java.io.tmpdir" />

46

47

48 <defaultCache maxElementsInMemory="500" eternal="false"

49

50

51 timeToIdleSeconds="300" timeToLiveSeconds="1200" overflowToDisk="true" />

52

53

54 <cache name="DEFAULT_CACHE" maxElementsInMemory="5000" eternal="false"

55

56

57 timeToIdleSeconds="500" timeToLiveSeconds="500" overflowToDisk="true" />

58

59

60 </ehcache>

61

62

第三步:写一个使用缓存的测试方法:

63 import org.springframework.cache.annotation.Cacheable;

64 //将缓存保存进DEFAULT_CACHE,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key

65 @Cacheable(value="DEFAULT_CACHE",key="#userId + 'findById'")

66 public SystemUser findById(String userId) {

67 SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);

68 return user ;

69 }

70

在这里,value = “”,对应了ehcache中的缓存名。

这种写法spring的缓存框架是根据传入进去的参数来判断查找缓存的,比如这里的param等于admin,缓存后,spring在从ehcacheadmin 从缓存中取了。

如果你想更自主的控制缓存查找,可以使用key参数。

key使用SPEL语言(Spring开发的类似于jsp中的EL表达式的语言)。Key的默认生成如下:

1. 不提供参数,返回0

2. 只提供一个参数,直接返回该参数

3. 提供多个参数,则返回多个参数进行hash运算之后的结果。

总结:其实spring3的这个cache底层的实现就是和这里一样http://m635674608.iteye.com/admin/blogs/1678983

只是spring帮我们封装好了。当然spring封装的还是比较好的。

<!--EndFragment-->
分享到:
评论
2 楼 netwelfare 2015-04-24  
文章的格式有点乱,不如这篇文章,格式好而且也通俗易懂: Spring中@Cacheable的用法
1 楼 yujingrqyz 2014-01-13  
总结的很好很受用!

相关推荐

    SpringCache+Redis实现高可用缓存解决方案.docx

    通过上述步骤,我们可以成功地在Spring Boot项目中整合Spring Cache与Redis,实现了一个高效的分布式缓存解决方案。这种方式不仅可以提高系统的响应速度,还可以有效减轻数据库的压力,对于构建高并发、高可用的应用...

    SpringBoot2.0整合SpringCache和Redis.docx

    SpringBoot2.0在整合缓存管理方面有了很大的改进,特别是与Redis的结合,使得数据缓存更加高效。本文将详细介绍如何在SpringBoot2.0项目中整合SpringCache和Redis,以及如何利用Spring提供的缓存注解进行操作。 ...

    SpringBoot2整合thymeleaf3及thymeleaf3语法.md

    &gt; spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html &lt;!--渲染效果--&gt; ${Hello}"&gt;显示欢迎 &lt;!--不渲染效果--&gt; ${Hello}"&gt;显示欢迎 ``` ### 2. 替换属性 `...

    ssm整合中jar包作用介绍.pdf

    SSM(Spring、Struts、MyBatis)整合是Java Web开发中常见的技术栈,其中涉及到多个组件和库的集成。在SSM整合过程中,jar包的选择和作用至关重要,它们构成了整个框架的基础。以下是对部分关键jar包的详细说明: 1...

    web开发常用jar

    struts2整合spring所需要的包 xwork-2.0.7.jar xwork核心包 antlr-2.7.6.jar 一个语言转换工具, Hibernate利用它实现 HQL 到 SQL 的转换模板相关操作需要包 javassist-3.9.0.GA.jar 代码生成工具 ...

    基于springbootweb游戏管理系统

    - **Spring Cache**:Spring提供的抽象缓存层,支持多种缓存实现。 7. **分布式与集群**: - **Spring Cloud**:一套微服务解决方案,提供服务注册、服务发现、负载均衡等功能,适合大型游戏管理系统。 - **...

    【SpringBoot】四、SpringBoot中整合Freemarker

    Spring Boot 自带的 `FreeMarkerAutoConfiguration` 类负责自动配置 FreeMarker。它使用 `FreeMarkerProperties` 类来获取配置信息,如模板路径、模板后缀等。默认情况下,模板位于 `classpath:/templates/` 目录下...

    java开发常用jar包

    Hibernate可以使用不同cache缓存工具作为二级缓存。EHCache是缺省的cache缓存工具。如果没有其它的可选缓存工具,则为必需的。 hibernate3.jar hibernate3的核心类库。 itext.jar 是用于生成PDF文档的一个java...

    程序员英语手册

    **3. Abstract Base Class (ABC) 抽象基类** - **定义**:一种特殊的类,不能被实例化,通常包含一个或多个抽象方法,作为其他类的基础。 - **应用场景**:作为子类继承的基础类,强制子类实现特定的方法。 **4. ...

Global site tag (gtag.js) - Google Analytics