热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

深入解析Spring3.1缓存机制与应用技巧

下面简单介绍一下spring3.1.M1中的cache功能。spring3.1.M1中负责cache的模块是org.springframework.context-3.1.0.M

下面简单介绍一下spring3.1.M1中的cache功能。

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中声明的cache的name

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

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

例如:

//将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key
@Cacheable(value="andCache",key="#userId + 'findById'")
public SystemUser findById(String userId) {
SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);
return user ;
}
//将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key
&#64;Cacheable(value&#61;"andCache",condition&#61;"#userId.length <32")
public boolean isReserved(String userId) {
System.out.println("hello andCache"&#43;userId);
return false;
}

&#64;CacheEvict 支持如下几个参数&#xff1a;

value&#xff1a;缓存位置名称&#xff0c;不能为空&#xff0c;同上

key&#xff1a;缓存的key&#xff0c;默认为空&#xff0c;同上

condition&#xff1a;触发条件&#xff0c;只有满足条件的情况才会清除缓存&#xff0c;默认为空&#xff0c;支持SpEL

allEntries&#xff1a;true表示清除value中的全部缓存&#xff0c;默认为false

例如&#xff1a;

//清除掉指定key的缓存
&#64;CacheEvict(value&#61;"andCache",key&#61;"#user.userId &#43; &#39;findById&#39;")
public void modifyUserRole(SystemUser user) {
System.out.println("hello andCache delete"&#43;user.getUserId());
}

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

一般来说&#xff0c;我们的更新操作只需要刷新缓存中某一个值&#xff0c;所以定义缓存的key值的方式就很重要&#xff0c;最好是能够唯一&#xff0c;因为这样可以准确的清除掉特定的缓存&#xff0c;而不会影响到其它缓存值 &#xff0c;

比如我这里针对用户的操作&#xff0c;使用(userId&#43;方法名称)的方式设定key值 &#xff0c;当然&#xff0c;你也可以找到更适合自己的方式去设定。

了解了cache的注解之后&#xff0c;接下来说说如何使注解生效&#xff0c;其实就是需要在spring的配置文件中增加一些配置。

1.spring-cache

首先我们来看一下如何使用spring3.1自己的cache&#xff0c;

需要在命名空间中增加cache的配置

Xml代码

  1. xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:p&#61;"http://www.springframework.org/schema/p"
  2. xmlns:cache&#61;"http://www.springframework.org/schema/cache"
  3. xsi:schemaLocation&#61;"
  4. http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  5. http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

之后添加如下声明&#xff1a;

Xml代码

  1. class&#61;"org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
  2. p:name&#61;"default"/>
  3. class&#61;"org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
  4. p:name&#61;"andCache"/>

2.spring-ehcache

接下来说说对ehcache的支持&#xff0c;其实只需要把cacheManager换成EHCache的cacheManager即可&#xff0c;如下&#xff1a;

Java代码

  1. p:configLocation&#61;"classpath:/config/ehcache.xml"/>
  2. p:cacheManager-ref&#61;"cacheManagerFactory"/>

ehcache.xml

Xml代码
  1. xsi:noNamespaceSchemaLocation&#61;"ehcache.xsd"updateCheck&#61;"true"
  2. monitoring&#61;"autodetect">
  3. timeToIdleSeconds&#61;"120"timeToLiveSeconds&#61;"120"overflowToDisk&#61;"true"
  4. maxElementsOnDisk&#61;"10000000"diskPersistent&#61;"false"
  5. diskExpiryThreadIntervalSeconds&#61;"120"memoryStoreEvictionPolicy&#61;"LRU"/>
  6. maxElementsOnDisk&#61;"1000"eternal&#61;"false"overflowToDisk&#61;"true"
  7. diskSpoolBufferSizeMB&#61;"20"timeToIdleSeconds&#61;"300"timeToLiveSeconds&#61;"600"
  8. memoryStoreEvictionPolicy&#61;"LFU"/>

ok&#xff0c;这样注解缓存就生效了。


推荐阅读
author-avatar
skyyyf
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有