Ehcache 整合Spring 使用

本文参考:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html

Ehcache可以对页面、对象、数据进行缓存,同时支持集群/分布式缓存。如果整合Spring、Hibernate也非常的简单,Spring对Ehcache的支持也非常好。EHCache支持内存和磁盘的缓存,支持LRU、LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件。同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度

一、准备工作

如果你的系统中已经成功加入Spring、Hibernate;那么你就可以进入下面Ehcache的准备工作。

1、 下载jar包

Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache-core-2.5.2-distribution.tar.gz

2、 需要添加如下jar包到lib目录下

ehcache-core-2.5.2.jar

ehcache-spring-annotations-1.2.0.jar----spring
3.0.5的,更细颗粒化的缓存设置,更方便的注解,可以具体到把每个方式的返回值做缓存, 需要 ehcache-spring-annotations-1.1.x。

3、 当前工程的src目录中加入配置文件

ehcache.xml

ehcache.xsd

这些配置文件在ehcache-core这个jar包中可以找到

二、Ehcache基本用法

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false">
  <diskStore path="java.io.tmpdir"/>
  <defaultCache eternal="false"
   maxElementsInMemory="1000"
   overflowToDisk="true"
   diskPersistent="false"
   timeToIdleSeconds="0"
   timeToLiveSeconds="600"
   memoryStoreEvictionPolicy="LRU"/>
<pre style="border-style: none; text-align: left; padding: 0px; line-height: 12pt; background-color: rgb(244, 244, 244); margin: 0em; width: 100%; direction: ltr; color: black; font-size: 10pt; overflow: visible;">    <span style="color:#008000;"><!-- </span></span>
        配置自定义缓存
        maxElementsInMemory:缓存中允许创建的最大对象数
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。
        timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,
                    两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,
                    如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,
                    这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        overflowToDisk:内存不足时,是否启用磁盘缓存。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
    -->

<cache name="Cache1" eternal="false" maxElementsInMemory="100" overflowToDisk="true" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/> <cache name="Cache2" eternal="true" maxElementsInMemory="1000"
overflowToDisk="true" diskPersistent="false" memoryStoreEvictionPolicy="LRU"/></ehcache>

代码实现

<span style="font-size:18px;">@Service
public class CacheManagerImpl implements CacheManager{

	private static List<net.sf.ehcache.CacheManager> CACHE_MANAGERS  = net.sf.ehcache.CacheManager.ALL_CACHE_MANAGERS;
	Log logger = LogFactory.getLog(CacheManagerImpl.class);

	/**
	 * 删除指定缓存中的内容 cacheName 为 null 表示清除所有缓存内的内容
	 * @param cacheName 缓存名称
	 * @return
	 */
	public boolean remove(String cacheName) {
		try{
			if(CACHE_MANAGERS != null){
				for(net.sf.ehcache.CacheManager cacheManager: CACHE_MANAGERS){
					if("".equals(cacheName) || cacheName == null){
						String[] cacheNames = cacheManager.getCacheNames();
						if(cacheNames != null){
							for(String cache : cacheNames){
								if(cacheManager.cacheExists(cache)){
									cacheManager.getCache(cache).removeAll();
								}
							}
						}
					}else{
						if(cacheManager.cacheExists(cacheName)){
							cacheManager.getCache(cacheName).removeAll();
						}
					}
				}
			}
		}catch(Exception e){
			logger.error("删除指定缓存中的内容失败!cacheName=" + cacheName);
			return false;
		}
		return true;
	}

}</span>

Spring配置文件applicationContext.xml中配置ecache

<span style="font-size:18px;"><pre name="code" class="html">1.配置缓存bean,要注意的是:ehcache缓存是做带DAO这一层,所以应把这一段配置和dao实例化放在同一个配置文件里面
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd"
    default-autowire="byName">
.......................
  <!-- 解添方式添加缓存 -->
    <ehcache:annotation-driven cache-manager="ehCacheManager" />
     <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
       <property name="configLocation" value="classpath:ehcache.xml" />  
    </bean></span>

时间: 2024-07-31 08:44:10

Ehcache 整合Spring 使用的相关文章

Ehcache 整合Spring 使用页面、对象缓存(转载)

Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的简单,Spring对Ehcache的支持也非常好.EHCache支持内存和磁盘的缓存,支持LRU.LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件.同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度.

ehcache整合spring本地接口方式

一.简介 ehcache整合spring,可以通过使用echache的本地接口,从而达到定制的目的.在方法中根据业务逻辑进行判断,从缓存中获取数据或将数据保存到缓存.这样让程序变得更加灵活. 本例子使用maven构建,需要的依赖如下: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2

Ehcache 整合Spring 使用页面、对象缓存

Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的简单,Spring对Ehcache的支持也非常好.EHCache支持内存和磁盘的缓存,支持LRU.LFU和FIFO多种淘汰算法,支持分布式的Cache,可以作为Hibernate的缓存插件.同时它也能提供基于Filter的Cache,该Filter可以缓存响应的内容并采用Gzip压缩提高响应速度.

Ehcache整合spring

下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了. 简单的使用真的很简单.但只能做为入门级了. 1.ehcache.xml,可放classpath根目录下, <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="

ehcache整合spring注解方式

一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件配置进去即可.在spring中使用ehcache有两种方式,一种是使用spring提供的封装,使用注解的方式配置在某个方法上面,第一次调用该方法的时候,将该方法执行返回的数据缓存,当再次执行的时候如果时间没有变,就直接冲缓存获取,该方法不执行:另一种方式是获取到ehcache本地接口,直接使用ehc

spring + ehcache 整合

一:ehcache 简介 ehCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider 类似于做了数据库的一个备份,一定要注意使用ehcache时的数据脏读 二:spring 需要的知识点 1 spring AOP 应用的几种方式:ProxyFactoryBean 动态代理,2.0版本之前:<aop> 标签 2.0版本之后 2 spring 获取接入点信息:JoinPoint 和 ProceedingJoinPoint(限于环绕

Mybatis整合Spring 【转】

根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个Mybatis-Spring用来满足Mybatis用户整合Spring的需求.下面就将通过Mybatis-Spring来整合Mybatis跟Spring的用法做一个简单的介绍. MapperFactoryBean 首先,我们需要从Mybatis官网上下载Mybatis-Spring的jar包添加到我们项

Echache整合Spring缓存实例讲解

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCache 介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java EE和轻量级容器.它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序

整合spring,springmvc和mybatis

我创建的是maven项目,使用到的依赖架包有下面这些: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.2.RELEASE</version> </dependency> <dependency> <