读书人

Spring AOP+ehCache简略缓存系统解决方

发布时间: 2012-10-12 10:17:04 作者: rapoo

Spring AOP+ehCache简单缓存系统解决方案

转自:http://blog.csdn.net/liuzhenwen/archive/2009/03/12/3983952.aspx

需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create /update/delete方法),则刷新cache中相应的内容。

根据需求,计划使用Spring AOP + ehCache来实现这个功能,采用ehCache原因之一是Spring提供了ehCache的支持,至于为何仅仅支持ehCache而不支持 osCache和JBossCache无从得知(Hibernate???),但毕竟Spring提供了支持,可以减少一部分工作量:)。二是后来实现了 OSCache和JBoss Cache的方式后,经过简单测试发现几个Cache在效率上没有太大的区别(不考虑集群),决定采用ehCahce。

AOP嘛,少不了拦截器,先创建一个实现了MethodInterceptor接口的拦截器,用来拦截Service/DAO的方法调用,拦截到方法后,搜索该方法的结果在cache中是否存在,如果存在,返回cache中的缓存结果,如果不存在,返回查询数据库的结果,并将结果缓存到cache 中。

MethodCacheInterceptor.java

,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cache is configured异常。另外,由于使用拦截器来刷新Cache内容,因此在定义cache生命周期时可以定义较大的数值,timeToIdleSeconds="300000" timeToLiveSeconds="600000",好像还不够大?Spring AOP+ehCache简略缓存系统解决方案

然后,在将Cache和两个拦截器配置到Spring,这里没有使用2.0里面AOP的标签。
cacheContext.xml

.可以看到,第一步执行getAllObject(),执行TestServiceImpl内的方法,并创建了cache,在第二次执行 getAllObject()方法时,由于cache有该方法的缓存,直接从cache中get出方法的结果,所以没有打印出 TestServiceImpl中的内容,而第三步,调用了updateObject方法,和TestServiceImpl相关的cache被 remove,所以在第四步执行时,又执行TestServiceImpl中的方法,创建Cache。

网上也有不少类似的例子,但是很多都不是很完备,自己参考了一些例子的代码,其实在spring-modules中也提供了对几种cache的支持,ehCache,OSCache,JBossCache这些,看了一下,基本上都是采用类似的方式,只不过封装的更完善一些,主要思路也还是 Spring的AOP,有兴趣的可以研究一下。

?

hibernate缓存

1.在applicationContext.xml中引入

<bean id="sessionFactory" ref="dataSource"/><property name="configLocation" value="${hibernate.config.location}" /><property name="mappingLocations"><list><value>classpath:/com/mysougou/mtmibp/center/core/entity/*.hbm.xml</value><value>classpath:/com/mysougou/mtmibp/cms/cms/entity/*.hbm.xml</value><value>classpath:/com/mysougou/mtmibp/cms/article/entity/*.hbm.xml</value><value>classpath:/com/mysougou/mtmibp/cms/download/entity/*.hbm.xml</value><value>classpath:/com/mysougou/mtmibp/cms/auxiliary/entity/*.hbm.xml</value></list></property><property name="hibernateProperties"><!--hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect--><!--hibernate.dialect=org.hibernate.dialect.SQLServerDialect--><value>hibernate.dialect=org.hibernate.dialect.SQLServerDialecthibernate.show_sql=truehibernate.format_sql=falsehibernate.query.substitutions=true 1, false 0hibernate.jdbc.batch_size=20hibernate.cache.provider_class=org.hibernate.cache.EhCacheProviderhibernate.cache.provider_configuration_file_resource_path=/ehcache-hibernate.xml</value></property><property name="entityInterceptor">   <ref local="treeInterceptor"/></property></bean>
ehcache-hibernate.xml

?

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">    <diskStore path="java.io.tmpdir/mysougou2/hibernate"/><cacheManagerEventListenerFactory properties=""/>    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/><cache name="com.mysougou.mtmibp.core.entity.Website" maxElementsInMemory="100" eternal="true" overflowToDisk="true"/><cache name="com.mysougou.mtmibp.core.entity.Global" maxElementsInMemory="1" eternal="true" overflowToDisk="true"/><cache name="com.mysougou.mtmibp.core.entity.Function" maxElementsInMemory="3000" eternal="true" overflowToDisk="true"/><cache name="com.mysougou.mtmibp.core.entity.Function.child" maxElementsInMemory="3000" eternal="true" overflowToDisk="true"/></ehcache>

?

读书人网 >软件架构设计

热点推荐