我想在我的分布式Web应用程序中使用spring cache abstraction.
我的Web应用程序由一个Web应用程序组成,该应用程序在带有负载均衡器的3个不同的tomcats上运行.
现在,我的问题是当另一个tomcat执行更新时,@Evict如何在所有tomcats中缓存?
春天是否支持这种事情?
谢谢!
如果你告诉Spring使用它是EHCache,那么EHCache支持跨不同物理服务器的多个缓存实例的复制.我在使用多播发现的RMI Replicated Caching方面取得了一些成功.从一个缓存中逐出将自动复制到其他缓存中 - 同样在添加到缓存时也是如此.
就Spring配置而言,您需要设置各种配置元素和bean:
<cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManager" /> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:/ehcache.xml"/> </bean>
其余配置在ehcache.xml
文件中完成.复制缓存的示例ehcache.xml
可能如下所示:
<cache name="example" maxElementsInMemory="1000" eternal="false" overflowToDisk="false" timeToIdleSeconds="0" timeToLiveSeconds="600"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/> </cache>
然后,您需要将复制设置添加到ehcache.xml
可能如下所示:
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.2, multicastGroupPort=4455, timeToLive=1" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=localhost, port=40001, socketTimeoutMillis=2000" />
如文档中所述,还有其他方法可以在EHCache中配置复制,但上面描述的RMI方法相对简单并且对我来说效果很好.