您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > springehcache配置的AOP的CACHE
SPRINGAOP的EHCACHE最近根据网上的文章配置了一个面向应用的EHCACHE,发现使用起来很方便。只要是继承了这个CACHE的配置,那么自动会拦截GET和FIND等方法来做CACHE下载ehcache包这些就不用说了。ehcache.xml文件配置如下,这个不用做多少改动:ehcachediskStorepath=c:\\temp\\cache/defaultCachemaxElementsInMemory=1000eternal=falsetimeToIdleSeconds=120timeToLiveSeconds=120overflowToDisk=true/!--DefaultCacheconfiguration.ThesewillappliedtocachesprogrammaticallycreatedthroughtheCacheManager.Thefollowingattributesarerequired:maxElementsInMemory-Setsthemaximumnumberofobjectsthatwillbecreatedinmemoryeternal-Setswhetherelementsareeternal.Ifeternal,timeoutsareignoredandtheelementisneverexpired.overflowToDisk-Setswhetherelementscanoverflowtodiskwhenthein-memorycachehasreachedthemaxInMemorylimit.Thefollowingattributesareoptional:timeToIdleSeconds-Setsthetimetoidleforanelementbeforeitexpires.i.e.ThemaximumamountoftimebetweenaccessesbeforeanelementexpiresIsonlyusediftheelementisnoteternal.Optionalattribute.Avalueof0meansthatanElementcanidleforinfinity.Thedefaultvalueis0.timeToLiveSeconds-Setsthetimetoliveforanelementbeforeitexpires.i.e.Themaximumtimebetweencreationtimeandwhenanelementexpires.Isonlyusediftheelementisnoteternal.Optionalattribute.Avalueof0meansthatandElementcanliveforinfinity.Thedefaultvalueis0.diskPersistent-WhetherthediskstorepersistsbetweenrestartsoftheVirtualMachine.Thedefaultvalueisfalse.diskExpiryThreadIntervalSeconds-Thenumberofsecondsbetweenrunsofthediskexpirythread.Thedefaultvalueis120seconds.--cachename=DEFAULT_CACHEmaxElementsInMemory=10000eternal=falsetimeToIdleSeconds=300000timeToLiveSeconds=600000overflowToDisk=true//ehcache需要做CACHE的servers这么配置,这是我的applicationContext-core-service.xml:?xmlversion=1.0encoding=UTF-8?!DOCTYPEbeansPUBLIC-//SPRING//DTDBEAN//EN=byNamedefault-lazy-init=truebeanid=userManagerparent=baseTxServicepropertyname=targetbeanclass=com.sillycat.core.service.impl.UserManagerImpl//property/bean/beans拦截方法做cache的配置主要就在这个baseTxService上面,我把事务拦截也放在这里了,applicationContext-transaction.xml如下:?xmlversion=1.0encoding=UTF-8?!DOCTYPEbeansPUBLIC-//SPRING//DTDBEAN//EN=byNamebeanid=baseTxServiceclass=org.springframework.transaction.interceptor.TransactionProxyFactoryBeanabstract=truepropertyname=transactionManagerref=transactionManager/propertyname=proxyTargetClassvalue=true/propertyname=transactionAttributespropspropkey=get*PROPAGATION_REQUIRED,readOnly/proppropkey=find*PROPAGATION_REQUIRED,readOnly/proppropkey=load*PROPAGATION_REQUIRED,readOnly/proppropkey=save*PROPAGATION_REQUIRED/proppropkey=update*PROPAGATION_REQUIRED/proppropkey=remove*PROPAGATION_REQUIRED/prop/props/propertypropertyname=preInterceptorslistrefbean=methodCachePointCut/refbean=methodCachePointCutAdvice//list/property/beanbeanid=transactionManagerclass=org.springframework.orm.hibernate3.HibernateTransactionManager//beans这里面的两个拦截的bean,methodCachePointCut和methodCachePointCutAdvice专门放置到一个文件中配置,applicationContext-cache.xml如下:?xmlversion=1.0encoding=UTF-8?!DOCTYPEbeansPUBLIC-//SPRING//DTDBEAN//EN!--引用ehCache的配置--beanid=defaultCacheManagerclass=org.springframework.cache.ehcache.EhCacheManagerFactoryBeanpropertyname=configLocationvalueclasspath:ehcache.xml/value/property/bean!--定义ehCache的工厂,并设置所使用的Cachename--beanid=ehCacheclass=org.springframework.cache.ehcache.EhCacheFactoryBeanpropertyname=cacheManagerreflocal=defaultCacheManager//propertypropertyname=cacheNamevalueDEFAULT_CACHE/value/property/bean!--find/createcache拦截器--beanid=methodCacheInterceptorclass=com.sillycat.plugin.cache.interceptors.MethodCacheInterceptorpropertyname=cachereflocal=ehCache//property/bean!--flushcache拦截器--beanid=methodCacheAfterAdviceclass=com.sillycat.plugin.cache.interceptors.MethodCacheAfterAdvicepropertyname=cachereflocal=ehCache//property/beanbeanid=methodCachePointCutclass=org.springframework.aop.support.RegexpMethodPointcutAdvisorpropertyname=advicereflocal=methodCacheInterceptor//propertypropertyname=patternslistvalue.*find.*/valuevalue.*get.*/value/list/property/beanbeanid=methodCachePointCutAdviceclass=org.springframework.aop.support.RegexpMethodPointcutAdvisorpropertyname=advicereflocal=methodCacheAfterAdvice//propertypropertyname=patternslistvalue.*create.*/valuevalue.*save.*/valuevalue.*insert.*/valuevalue.*update.*/valuevalue.*delete.*/valuevalue.*remove.*/value/list/property/bean/beans如上所配置,只要继承了这个配置的service,里面的findget方法就会先走cache,另外,当发生create,save,insert,updatedelete,remove等方法时,就会去flush一下。另外,两个类也贴出来,MethodCacheAfterAdvice.java:packagecom.sillycat.plugin.cache.interceptors;importjava.util.List;importnet.sf.ehcache.Cache;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframewor
本文标题:springehcache配置的AOP的CACHE
链接地址:https://www.777doc.com/doc-1084841 .html