springmvc和encache集成
虽然目前已经出了 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 © 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
springmvc和encache集成的更多相关文章
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- SpringMVC 3.2集成Spring Security 3.2
参考:http://www.cnblogs.com/Beyond-bit/p/springmvc_and_springsecurity.html SpringMVC 3.2集成Spring Secur ...
- SpringMVC 3.1集成Spring Security 3.1
这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...
- springmvc+mybatis集成配置
简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求.闲暇时间把项目配置文件共享出来,供大家参看: 1.首先我们来看下依赖的pom: <!-- spri ...
- Spring4 SpringMVC Hibernate4 Freemaker 集成示例
变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...
- SpringMVC+Spring+Mybatis -- 集成之旅
准备 首先介绍一下,我的工具使用的是STS, 需要的童鞋可以到官网下载:http://spring.io/tools/sts/all 使用STS是因为她集成了Maven进行 “包“ 管理以及自带 We ...
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
上篇博文我们介绍了mybatis的基本概念与原理,这篇博文我们通过Spring与Mybatis集成,开发一个简单用户增删改查的Web项目. 基本准备工作 1.安装JDK1.6以上版本,安装与配置 2. ...
- shiro在springmvc里面的集成使用【转】
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-coll ...
- Spring+SpringMvc+Mybatis框架集成搭建教程一(项目创建)
一.框架搭建环境 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Idea 15.04 二 ...
随机推荐
- 解决IntelliJ IDEA控制台乱码问题[包含程序运行时的log4j日志以及tomcat日志乱码]
这里使用的IntelliJ IDEA版本为[IntelliJ IDEA 14.1.4]: 一.控制台打印的程序运行时的log4j日志中包含中文乱码 在IDEA安装目录的bin目录下找到名为" ...
- ostream_iterator的可能实现
当我们要输出一个容器的内容时,可以使用std::copy函数,如下: vector <string> myvector; std::copy(myvector.begin(), myvec ...
- Java自动创建多层文件目录
// 创建文件上传路径 public static void mkdir(String path) { File fd = null; try { fd = new File(path); if (! ...
- Unique constraint on single String column with GreenDao2
转:http://software.techassistbox.com/unique-constraint-on-single-string-column-with-greendao_384521.h ...
- 14款超时尚的HTML5时钟动画
时钟动画在网页应用中也非常广泛,在一些个人博客中,我们经常会看到一些相当个性化的HTML5时钟动画.今天我们向大家分享了14款形态各异的超时尚HTML5时钟动画,其中有圆盘时钟.3D时钟.个性化时钟等 ...
- Java操作memcache
[本文出自天外归云的博客园] 准备工作 Java操作memcache需要spymemcache类库的支持,在Eclipse中修改maven项目的pom.xml文件—— 添加仓库: <reposi ...
- java工具类POI导出word
1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...
- [转]BigDecimal不整除异常
通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的 异 常 :java.lang.ArithmeticException: Non-terminatin ...
- JAVA传入一个字符串,返回一个字符串中的大写字母
/** * * @param 传入一个字符串 * @return 返回一个字符串中的大写字母 */ private static String str ...
- C++实现的服务不可用发送报警邮件
正在看C++,我主业是Java开发,但是最近服务不太稳定,自己用C++造了个小轮子. 配置好要监控的访问路径,IP.端口号和路径.涉及到的知识点有以下几个部分: 构造函数与析构函数 内存的分配与释放( ...