- 浏览: 119130 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
awdxzc:
还能更复杂一点?
Ehcache缓存配置 -
polaris1119:
请问,用ibatis + postgreSQL,存储过程出入一 ...
postgre存储过程简单实用方法 (过程语言: PL/pgSQL) -
foxty:
name like '%'+?+'%'
这样不必对参数再进 ...
PreparedStatement 使用like 模糊查询 -
manjingtou:
呵呵,我用的就是8.3但是我没有使用 application ...
POSTGIS常用函数 -
MegRyan:
我数据库里面有754个 但是没有AddGeometryColu ...
POSTGIS常用函数
近期项目用到Ehcache,以前项目主要用到Oscache,并且缓存也是针对orm来处理,只要配置就好,不需要自定义缓存,不需管理缓存。下面描述一下,Spring+Ehcache来处理缓存。
1,首先引入jar包就不说了环境也不说了,要引入ehcache.xml文件(ehcache配置文件)。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <cacheManagerEventListenerFactory class="" properties=""/> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <Cache name="InstantCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="300" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <Cache name="fixCache" maxElementsInMemory="100000" eternal="true" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <Cache name="methodCache" maxElementsInMemory="100000" eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>
2,自定义Ehcache配置文件ehcache-cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 引用ehCache的配置 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>/WEB-INF/ehcache.xml</value> </property> </bean> <!-- 配置即时缓存 Start--> <bean id="instantCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>instantCache</value> </property> </bean> <!-- 配置即时缓存 End--> <!-- 配置固定缓存 Start--> <bean id="fixCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>fixCache</value> </property> </bean> <bean id="fixCacheInterceptor" class="FixCacheInterceptor"></bean> <!--红色字为自定类--> <bean id="fixCacheAfterAdvice" class="FixCacheAfterAdvice"></bean> <!--红色字为自定类--> <bean id="fixCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="fixCacheInterceptor" /> </property> <property name="patterns"> <list><!-- 不能为空,不然SpringHelper会报拿不到bean的错 --> <value>.*findAllConfig.*</value> <value>.*getConfig.*</value> <value>.*queryConfig.*</value> <value>.*listConfig.*</value> <value>.*searchConfig.*</value> <value>.*loadConfig.*</value> </list> </property> </bean> <bean id="fixCachePointCutAdivsor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="fixCacheAfterAdvice" /> </property> <property name="patterns"> <list> <value>.*createConfig.*</value> <!--value>.*saveConfig.*</value--> <!--value>.*addConfig.*</value--> <!--value>.*updateConfig.*</value--> <!--value>.*delConfig.*</value> <value>.*deleteConfig.*</value> <value>.*modConfig.*</value--> <value>.*freeConfig.*</value> <value>.*bindConfig.*</value> </list> </property> </bean> <!-- 配置固定缓存 End--> <!-- 配置接口缓存 Start--> <bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"> <ref local="cacheManager" /> </property> <property name="cacheName"> <value>methodCache</value> </property> </bean> <!-- find/create cache拦截器 excludeMethods 过滤的方法--> <bean id="methodCacheInterceptor" class="MethodCacheInterceptor"> <property name="excludeMethods"> <value> deleteConfig,findConfig </value> </property> </bean> <!-- flush cache拦截器 excludeMethods 过滤的方法--> <bean id="methodCacheAfterAdvice" class="MethodCacheAfterAdvice"> <property name="excludeMethods"> <value> deleteConfig,findConfig </value> </property> </bean> <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="methodCacheInterceptor" /> </property> <property name="patterns"> <list> <value>.*find.*</value> <value>.*get.*</value> <value>.*query.*</value> <value>.*list.*</value> <value>.*search.*</value> <value>.*load.*</value> </list> </property> </bean> <bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="methodCacheAfterAdvice" /> </property> <property name="patterns"> <list> <value>.*create.*</value> <value>.*save.*</value> <value>.*add.*</value> <value>.*update.*</value> <value>.*del.*</value> <value>.*delete.*</value> <value>.*mod.*</value> <value>.*free.*</value> <value>.*bind.*</value> </list> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="proxyTargetClass"> <value>false</value> </property> <property name="beanNames"> <list> <value>*BO</value> </list> </property> <property name="interceptorNames"> <list> <value>fixCachePointCut</value> <value>fixCachePointCutAdivsor</value> <value>methodCachePointCut</value> <value>methodCachePointCutAdvice</value> </list> </property> </bean> <!-- 配置接口缓存 End --> <!-- 刷新固定缓存定时器 Start--> <bean id="FixJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.ot.opf.timer.FixCacheJob</value> </property> </bean> <bean id="FixCacheTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="FixJobDetail" /> <property name="cronExpression"> <value>0 0 2 * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list><ref bean="FixCacheTrigger"/></list> </property> </bean> <!-- 刷新固定缓存定时器 End --> </beans>
3,增加相应缓存的管理类
(1)FixEhCacheManager
import java.util.List; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.apache.log4j.Logger; public class FixEhCacheManager { private static Cache fixCache = (Cache)SpringHelper.getBean("fixCache"); private static Logger logger = Logger.getLogger(FixEhCacheManager.class); private static boolean isStop = true;//初始化暂停 public synchronized static boolean isStop() { return isStop; } public synchronized static void setStop(boolean isStop) { FixEhCacheManager.isStop = isStop; } public synchronized static void put(Object key,Object value){ if(!isStop()){ Element e = new Element(key,value); logger.debug("cache object to fixCache... key:"+key+",size of value"+e.getSerializedSize()); fixCache.put(e); } } public synchronized static Object get(Object key){ if(isStop())return null; Element element = fixCache.get(key); if(element!=null)logger.debug("get object from fixCache... key:"+key); return element == null ? null : element.getObjectValue(); } public synchronized static void remove(Object key){ logger.debug("remove object from fixCache... key:"+key); fixCache.remove(key); } public synchronized static void removeAll(){ logger.debug("remove all object from fixCache"); fixCache.removeAll(); } public synchronized static List getKeys(){ return fixCache.getKeys(); } public synchronized static int getSize(){ return fixCache.getSize(); } public synchronized static String getCacheName(){ return fixCache.getName(); } public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) { StringBuffer sb = new StringBuffer("fixCache_"); sb.append(_class.getName()).append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sb.append(".").append(arguments[i]); } } return sb.toString(); } }
(2)InstantEhCacheManager
import java.util.List; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.apache.log4j.Logger; public class InstantEhCacheManager { private static Cache instantCache = (Cache)SpringHelper.getBean("instantCache"); private static Logger logger = Logger.getLogger(InstantEhCacheManager.class); private static boolean isStop = true;//初始化停止 public synchronized static boolean isStop() { return isStop; } public synchronized static void setStop(boolean isStop) { InstantEhCacheManager.isStop = isStop; } public synchronized static void put(Object key,Object value){ if(!isStop()){ Element e = new Element(key,value); logger.debug("cache object to instantCache... key:"+key+",size of value��"+e.getSerializedSize()); instantCache.put(e); } } public synchronized static Object get(Object key){ if(isStop())return null; Element element = instantCache.get(key); if(element!=null)logger.debug("get object from instantCache... key:"+key); return element == null ? null : element.getObjectValue(); } public synchronized static void remove(Object key){ logger.debug("remove object from instantCache... key:"+key); instantCache.remove(key); } public synchronized static void removeAll(){ logger.debug("remove all object from instantCache"); instantCache.removeAll(); } public synchronized static List getKeys(){ return instantCache.getKeys(); } public synchronized static int getSize(){ return instantCache.getSize(); } public synchronized static String getCacheName(){ return instantCache.getName(); } public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) { StringBuffer sb = new StringBuffer("instantCache_"); sb.append(_class.getName()).append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sb.append(".").append(arguments[i]); } } return sb.toString(); } }
(3)MethodEhCacheManager
import java.util.List; import net.sf.ehcache.Cache; import net.sf.ehcache.Element; import org.apache.log4j.Logger; public class MethodEhCacheManager { private static Cache methodCache = (Cache)SpringHelper.getBean("methodCache"); private static Logger logger = Logger.getLogger(MethodEhCacheManager.class); private static boolean isStop = true;//初始化暂停 public synchronized static boolean isStop() { return isStop; } public synchronized static void setStop(boolean isStop) { MethodEhCacheManager.isStop = isStop; } public synchronized static void put(Object key,Object value){ if(!isStop()){ Element e = new Element(key,value); logger.debug("cache object to methodCache ... key:"+key+",size of value��"+e.getSerializedSize()); methodCache.put(e); } } public synchronized static Object get(Object key){ if(isStop())return null; Element element = methodCache.get(key); if(element!=null)logger.debug("get object from methodCache... key:"+key); return element == null ? null : element.getObjectValue(); } public synchronized static void remove(Object key){ logger.debug("remove object from methodCache... key:"+key); methodCache.remove(key); } public synchronized static void removeAll(){ logger.debug("remove all object from methodCache"); methodCache.removeAll(); } public synchronized static List getKeys(){ return methodCache.getKeys(); } public synchronized static int getSize(){ return methodCache.getSize(); } public synchronized static String getCacheName(){ return methodCache.getName(); } public synchronized static String getCacheKey(Class _class, String methodName, Object... arguments) { StringBuffer sb = new StringBuffer("methodCache_"); sb.append(_class.getName()).append(".").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sb.append(".").append(arguments[i]); } } return sb.toString(); } }
4,aop相应的类,只要对上面的通过BeanName捕获到,执行相应的AOP处理相应的缓存放入和移出。
发表评论
-
Comet4J实现推送服务
2015-03-03 11:13 0Comet4J实现推送服务 -
java web 导出excel
2012-11-03 12:44 991看到这个标题,会很疑问,java导出excel 有很多文章呀, ... -
LIST合并单元格效果
2011-10-17 14:40 13541,在今天整理代码的时候,发现原来的一段代码,前台合并单元格。 ... -
hibernate 一对多集合 set查询问题
2011-08-19 17:20 1122今天解决了一个问题( ... -
hibernate lazy
2011-08-12 13:15 1188今天项目组有要使用的hibernate lazy的,我一直对这 ... -
struts2 泛型 Hibernate
2011-05-07 17:51 2786今天在整理代码的时候 ... -
org.apache.commons.beanutils.BeanUtils No value specified
2010-11-29 11:22 2072今天遇到一个问题,正如题目的在进行 form到 Bean的复制 ... -
AXIS WebService
2010-11-25 18:13 1516这段时间重构一下原来的代码,把东西整理一下。这个为AXIS的整 ... -
Web Services
2010-11-25 16:12 925web 服务是通过标准的web协议可以访问的一个应用程序组件。 ... -
Cookie 和 Session
2010-10-25 17:07 772一、cookie机制和session机制的区别******** ... -
spring 常识2
2010-10-19 23:00 12301, Spring bean 定义 spring bean ... -
spring 常识1
2010-10-19 22:06 7401,实例化bean就Spring Io ... -
Ehcache分布式缓存
2010-08-19 19:44 2014Ehcache分布式缓存也是我一直比较较关注的,以前用过osc ... -
hibernate 查询效率(1)
2010-02-24 16:47 1595相信越来越多的web开发者,在持久层都采用了hibernate ... -
servlet线程安全问题分析(网上整理)
2009-03-02 10:56 929这两天在整理些基础的资料,在网上看到了这篇文章Servlet/ ... -
httpclient (1)
2008-01-29 14:50 1124最近用到了HttpClient ,搜集的资料: HttpCl ... -
httpclient (2)
2008-01-29 14:55 1806根据以上步骤,我们来 ... -
spring ApplicationContext简单研究
2008-03-11 09:57 1377spring 研究装载配置文件 1,研究spring webA ... -
quartz在spring中的使用
2008-03-17 09:29 830(1)在spring 中的配置bean (1)在spring ... -
有关web效率
2008-03-18 10:10 10981)web开发的特点是是:没有太复杂的技术难点,一切在于迅速的 ...
相关推荐
Ehcache缓存配置
Ehcache缓存配置说明帮助你如何使用Ehcache
ehcache缓存配置详解 个人通过查资料和实践后总结
Ehcache缓存配置
ehcache缓存的jar包和配置文件。
EHcache缓存框架,ehcache介绍与说明,Ehcache详细,EHcache集群环境配置
EhCache缓存的配置文件
ehcache二级缓存配置文件
ehcache缓存的配置: name="sampleCache" maxElementsInMemory="5" maxElementsOnDisk="100" eternal="false" timeToIdleSeconds="2" timeToLiveSeconds="2" overflowToDisk="true" > <!--...
Hibernate中默认的二级缓存机制ehcache案例,属性ehcache环境搭建,配置问题。
缓存可以提高查询数据性能, 对同一批数据进行多次查询时, 第一次查询走数据库, 查询数据后,将数据保存在内存中,第二次以后查询 可以直接从内存获取数据,而不需要 和数据库进行交互。
介绍了ehcache缓存的基本原理和配置说明
ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。这里只说rmi方式。同时ehcache可以可以实现多播的方式实现集群。也可以手动指定集群主机序列实现集群,本例应用手动指定。
从sebService获得数据,分页显示,如果不用缓存刚每显示都很慢。配置文件自动清空缓存。
配置 EhCache 的实例 1、 简介: EhCache缓存插件是Hibernate的另一个项目,Hibernate框架内置了对它 的支持,对于单机应用推荐使用它做为Hibernate的二级缓存。
Hibernate配置EhCache缓存之annotation注解[归类].pdf
里面包含ehcache基本配置说明,缓存代码,缓存页面,与spring+hibernate结合等配置说明