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 缓存配置(一)
硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...
随机推荐
- vim简单使用教程
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- Spring 事务管理原理探究
此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...
- Android欢迎界面的创建方法
1.制作一张启动图片splash.png,放置在res->drawable-hdpi文件夹中.2.新建布局文件splash.xml <?xml version="1.0" ...
- 【转】ASP.NET网站怎么发布web项目程序和怎么部署
如何发布: http://jingyan.baidu.com/article/ca00d56c7303ffe99eebcfb0.html windows7 iis安装与配置 如何部署: http:// ...
- IOS-day03_OC中的get和set
OC中的get和set实质和C#/java中的一样 只是表现形式不同而已 如下: @interface Car : NSObject { int wheels; } -(void) run; -(vo ...
- 网页抓取:PHP实现网页爬虫方式小结
来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...
- WinDriver&PCIE
1.安装VS2012 安装VS2012略过,主要用它来做数据传输应用程序的,WINDRIVER提供了一系列API接口,方便了用户,使用户能直接进入用户态的编程,因为内核态的编程它已做好,不需要进行修改 ...
- [apkAnalyzer] 查看APK包名
最近项目中要用到APK的包名,必应到apkAnalyzer这个软件可以用,下载解压后,看到这么些jar,bat文件不知道怎么用. 参考了这篇文章,看的也不甚明白,最后还是试出来了,这里记录一下. 首先 ...
- anaconda在linux下的安装注意事项
不应该做什么 官网原文: Installation Instructions Linux Install These instructions explain how to install Anaco ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...