二。ehcache集群配置
在配置Ehcache前,请打开hibernate的二级缓存
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate-config/hibernate.cfg.xml"> </property> <property name="hibernateProperties"> <value> hibernate.cache.use_query_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.dialect=org.hibernate.dialect.Oracle9Dialect hibernate.show_sql=true hibernate.format_sql=false </value> </property> </bean>
在实体类配置文件中添加<cache usage="read-only"/>
如果是注解方式,那么添加@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
缓存详细使用,请参考相应资料 ,此处只是为了测试。
ehcache的集群缓存设置有多种方式。详细请参考 ehcache.org。
此处是以RMI方式配置
ehcache配置文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStore path="c://ehcache/"/> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" Properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,timeToLive=32"/> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=192.168.1.100,port=40001,socketTimeoutMillis=120000" /> <!-- setup cache's default data overdue tactics --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" > <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </defaultCache> <!-- setup cache's default data overdue tactics --> <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="50" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true" > <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </cache> <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true" > <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </cache> <!-- user-defined search cache's tactics via query.setCacheRegion("myCacheRegion"); to use it --> <cache name="myCacheRegion" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" > <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </cache> </ehcache>
如有疑问,请发送站内短信