虽然目前已经出了 ehcache3.x 了,但是,结合我搜索到的资料,我依旧只能先采用 ehcache2.x 来使用了

首先,在pom 中引入jar

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>

引入 ehcache2 的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="defaultCache">
<diskStore path="../temp/ehcache" /> <!--  <diskStore path="java.io.tmpdir" />  -->
<!-- 默认缓存配置. -->
<defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
overflowToDisk="true" maxEntriesLocalDisk="100000" /> <!-- 系统缓存 -->
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
</ehcache>

在spring 的配置文件中 spring-context 中引入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd"> <!-- 当前这个配置文件,是没有用到的,保留是为了方便查阅 --> <!-- 缓存配置 -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<!-- <cache:annotation-driven cache-manager="cacheManager"/> -->
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
<!--
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
-->
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<!-- 缓存配置 -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<!-- <bean id="shiroCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean> -->
<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="cacheManager"/>
</bean> </beans>

我这里使用了 shiro,为了 shiro 和 ehcache 结合,引入 shiro-ehcache 包

CacheUtils  采用工具类的模式,在需要缓存的地方,手动加入或移除缓存

/**
* Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
*/
package com.thinkgem.jeesite.common.utils; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; /**
* Cache工具类
* @author ThinkGem
* @version 2013-5-29
*/
public class CacheUtils { private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager")); private static final String SYS_CACHE = "sysCache"; /**
* 获取SYS_CACHE缓存
* @param key
* @return
*/
public static Object get(String key) {
return get(SYS_CACHE, key);
} /**
* 写入SYS_CACHE缓存
* @param key
* @return
*/
public static void put(String key, Object value) {
put(SYS_CACHE, key, value);
} /**
* 从SYS_CACHE缓存中移除
* @param key
* @return
*/
public static void remove(String key) {
remove(SYS_CACHE, key);
} /**
* 获取缓存
* @param cacheName
* @param key
* @return
*/
public static Object get(String cacheName, String key) {
Element element = getCache(cacheName).get(key);
return element==null?null:element.getObjectValue();
} /**
* 写入缓存
* @param cacheName
* @param key
* @param value
*/
public static void put(String cacheName, String key, Object value) {
Element element = new Element(key, value);
getCache(cacheName).put(element);
} /**
* 从缓存中移除
* @param cacheName
* @param key
*/
public static void remove(String cacheName, String key) {
getCache(cacheName).remove(key);
} /**
* 获得一个Cache,没有则创建一个。
* @param cacheName
* @return
*/
private static Cache getCache(String cacheName){
Cache cache = cacheManager.getCache(cacheName);
if (cache == null){
cacheManager.addCache(cacheName);
cache = cacheManager.getCache(cacheName);
cache.getCacheConfiguration().setEternal(true);
}
return cache;
} public static CacheManager getCacheManager() {
return cacheManager;
} }

手动加入和移除呢,还是太麻烦。

其实还有 spring 与 ehcache 结合的包,可以在方法上加注解的方式。参考博客:http://blog.csdn.net/u011068702/article/details/47780033

/**
* ehcache-spring-annotations简介
* @see ----------------------------------------------------------------------------------------------------------------
* @see 关于Spring与Ehcache的集成,googlecode上有一个经Apache认证的开源项目叫做ehcache-spring-annotations
* @see 目前已经到了1.2.0版本,它只是简化了Spring和Ehcache集成的复杂性(事实上我觉得完全没必要,因为它俩集成并不复杂)
* @see 尽管如此还是要提一下,它的项目主页为https://code.google.com/p/ehcache-spring-annotations/
* @see 这篇文章中描述了其用法http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/
* @see ----------------------------------------------------------------------------------------------------------------
* @see 1)使用时在项目中引入ehcache-spring-annotations-1.2.0.jar和guava-r09.jar两个jar文件
* @see 2)然后在applicationContext.xml按照如下配置
* @see <beans xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
* @see xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
* @see http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">
* @see <ehcache:annotation-driven/>
* @see <ehcache:config cache-manager="cacheManager">
* @see <ehcache:evict-expired-elements interval="60"/>
* @see </ehcache:config>
* @see <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
* @see <property name="configLocation" value="classpath:ehcache.xml"/>
* @see </bean>
* @see 3)最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解即可
* @see 经我亲测,@TriggersRemove(cacheName="..", when="..", removeAll=true)可移除缓存中的全部对象
* @see 但若写成@TriggersRemove(cacheName="..", when="..")则不会移除缓存中的单一的或所有的对象,即缓存中的对象无变化
* @see ----------------------------------------------------------------------------------------------------------------
* @create Oct 3, 2013 4:56:35 PM
* @author 玄玉<http://blog.csdn.net/jadyer>
*/

另外针对每一个方法上写一遍缓存也是够繁琐的,是否有法子采用AOP 的方式来注入缓存呢

比如 方法名为 getCacheXXX 或者 findCacheXXX 则需要缓存

update 就移除缓存 之类

等我哪天有心情研究下

-----------

Spring4整合Ehcache2.10 :http://blog.csdn.net/frankcheng5143/article/details/50776542

spring+EnCache缓存示例 :http://blog.csdn.net/zjt1388/article/details/51810270

http://blog.csdn.net/frankcheng5143/article/details/50791757

最新文章

  1. Linux中mongodb安装和导出为json
  2. dfs序
  3. androidstudio 之 svn配置 汇总
  4. aspcms 留言 搜索
  5. PLSQL Developer 不能连接 oracle 11g 64位 的解决办法
  6. HighchartsJS创建环形带标识的图表实例
  7. mysql 大数据量的处理
  8. HDU 5265 pog loves szh II (技巧)
  9. C#学习笔记(八):扩展方法
  10. transient关键字小结
  11. 计算机网络 NAT
  12. vs2013安装visual assist和viemu之后提示功能等无效解决
  13. Linux_window与linux之间文件互传,上传下载
  14. Charles 抓包工具使用部分问题总结
  15. Linux(CentOS7)yum安装卸载命令,离线下载安装包
  16. MySQL触发器在建立时,报语法错的问题
  17. MSSQL sql server order by 1,2 的具体含义
  18. Ngon 是啥
  19. 专业方向系列-00-Python与有限元初探
  20. 如何在Axure中使用FontAwesome字体图标

热门文章

  1. yum卸载失败Error in PREUN scriptlet in rpm package postgresql-server
  2. openfire 发送 接受 注册 广播 好友列表 在线状态
  3. 【Bayesian】贝叶斯决策方法(Bayesian Decision Method)
  4. 【Java】HashTable和HashMap区别
  5. SFTP文件上传与下载(window 上传文件到linux服务器)
  6. django HttpRequest对象
  7. django模型创建
  8. 分布式唯一id生成器的想法
  9. junit import org.junit.Test 报错
  10. SpringMVC RedirectView的使用以及源码分析