Ehcache 整合Spring 使用 谁践踏了优雅 2022-08-22 01:23 160阅读 0赞 **本文参考: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][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] > > [ > ][Link 1] 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> [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]: 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 [Link 1]: http://ehcache.org/downloads/destination?name=ehcache-web-2.0.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-web-2.0.4-distribution.tar.gz [spring]: http://lib.csdn.net/base/17
还没有评论,来说两句吧...