java spring 使用注解来实现缓存
这里举例使用spring3.1.4 + ehcache
注解的方式使用cache 是在spring3.1加入的
使用方法:
1.ehcache依赖+spring依赖
<!-- ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<!-- 这里有注解类需要的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
2.spring+ehcache的简单配置
Ehcache的基本配置,具体参数意思看:http://www.ehcache.org/documentation/configuration/configuration
| name |
cache的唯一标识 |
| maxElementsInMemory |
最大创建条数 |
| eternal |
缓存是否是永久的,默认false |
| timeToLiveSeconds |
缓存的存活时间 |
| timeToIdleSeconds |
多长时间不访问就清楚该缓存 |
| overflowToDisk |
内存不足是否写入磁盘,默认False |
| maxElementsOnDisk |
持久化到硬盘最大条数 |
| memoryStoreEvictionPolicy |
缓存满了后,清除缓存的规则, 自带三种:FIFO(先进先出),LFU(最少使用),LRU(最近最少使用) |
| diskSpoolBufferSizeMB |
磁盘缓存的缓存区大小,默认30M |
| diskExpiryThreadIntervalSeconds |
磁盘失效线程时间间隔 |
2.1 首先建立一个ehcache.xml的配置文件
<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/> <cache
name="onecache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"/>
</ehcache>
2.2在spring的apllication.xml 加入注入的cache
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true"/>
这里还需要在配置文件头部引入
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
...
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
"
在mvc-servlet.xml 里加入
<mvc:annotation-driven/> <!-- 挺重要的,不加注解不会被扫描-->
3.注解使用
3.1 用于对象 class
@Cacheable(value = "onecache")
class A1{}
这种情况类中方法中返回值都会被缓存,
3.2用于方法method
@Cacheable(value = "onecache", key = "#name",condition = "#age < 25")
public xx findEmployeeBySurname(String firstName, String name, int age) {
return xxx
}
这个就会将age小于25的值,按name为key缓存
3.3 清除
@CacheEvict(value="onecache",key="#name + 'haha'")
public void delete(String name) {
System.out.println("delete one name");
}
还可使用下面的清除全部
@CacheEvict(value="oneCache",allEntries=true)
4.代码调用
@Autowired
private CacheManager cacheManager; //获取当前时间
public String getABCCache() {
cacheManager.getCache("ccc").put("hello", new Date().toString());
return (String) cacheManager.getCache("ccc").get("hello").get();
}
java spring 使用注解来实现缓存的更多相关文章
- java Spring 基于注解的配置(一)
注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Java 必须掌握的 20+ 种 Spring 常用注解
Spring部分 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @C ...
- Java 必须掌握的 12 种 Spring 常用注解!
1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller ...
- spring整合ehcache注解实现查询缓存,并实现实时缓存更新或删除
转载: http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天 ...
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主题有所感触.不多说了,开干! 注:引入jar <!-- 引入ehcach ...
- 2.spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除
转自:http://www.importnew.com/23358.html 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天主 ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)
Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...
- 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)
硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...
随机推荐
- 1045 整数礼物 c语言
描述 Na 给准备送给ZZ两个整数,a, b,他还计算了这两个整数的平均值c,碰巧c也是整数. 但是Na 突然把b给弄丢了,你要帮助Na通过a, c计算出来b的值. 输入 输入为一行,两个用空格隔开的 ...
- Entity Framework 5.0
今天 VS2012 .net Framework 4.5 Entity Framework 5.0 三者共同发布了. ( EF5 Released ) 在介绍新特性之前,先与大家回顾一下EF版 ...
- Loadrunner模拟Json请求
一.loadrunner脚本创建 1.Insert - New step -选择Custom Request - web_custom_request 2.填入相应参数 3.生成脚本,并修改如下(参数 ...
- Java中Runnable和Thread的区别(转)
http://developer.51cto.com/art/201203/321042.htm 第一种方式:使用Runnable接口创建线程 第二种方式:直接继承Thread类创建对象 使用Runn ...
- 王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上
王家林 Spark公开课大讲坛第一期:Spark把云计算大数据速度提高100倍以上 http://edu.51cto.com/lesson/id-30815.html Spark实战高手之路 系列书籍 ...
- 第二百九十五天 how can i 坚持
买了个小米电话卡,写的让周六日送,非得今天给送来,浪费了1块钱.买回来还没法激活,这.. 昨天差点挂掉,今天感觉好多了,不过今天好冷,回来快冻死了. 今天啊,年终奖订下来了,没有想象的高 啊,有点小失 ...
- DelphiXE8新建AVD
相关资料: 1.http://jingyan.baidu.com/article/ea24bc398576b3da62b33107.html
- Oracle中纵横表的转化
横表就是普通的建表方式,如一个表结构为:主键.字段1.字段2.字段3......如果变成纵表后, 则表结构为:主键.字段代码.字段值.而字段代码则为字段1.字段2.字段3. 纵表对从数据库到内存的映射 ...
- 条件编译#ifdef的妙用详解_透彻
这几个宏是为了进行条件编译.一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部 分内容指定编译的条件,这就是“条件编译”.有时,希望当满足某条 ...
- codeforces 624B Making a String
Making a String time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...