2011-09-30 144 views
3

我有2個不同的webapps在相同的Tomcat 6實例中運行。兩者共享一個使用hibernate進行持久化的庫。我想使用ehcache啓用Hibernate 2nd緩存,但我不希望每個webapp都有自己的緩存。在多個Tomcat webapps中共享本地ehcache

任何想法我可能會實現這個?我安裝了Ehcache核到$ CATALINA_HOME/lib和每個彈簧的應用程序配置Hibernate來使用的Ehcache這樣的:

<property name="hibernateProperties"> 
<props>  
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
    <prop key="hibernate.cache.use_query_cache">true</prop> 
    <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop> 
</props> 
</property> 

這兩個應用程序正在使用的Ehcache,但每次還是有自己獨特的緩存(在一個修改項目應用程序和陳舊的價值仍然會出現在其他應用程序)

我ehcache.xml中(也是在$ CATALINA_HOME/lib中)看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="ehcache.xsd" 
     name="mySharedCache" 
     updateCheck="false" monitoring="autodetect" 
     dynamicConfig="false"> 
    <diskStore path="java.io.tmpdir"/> 
    <defaultCache maxElementsInMemory="10000" 
        eternal="false" 
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120" 
        overflowToDisk="true" 
        diskPersistent="false" 
        diskExpiryThreadIntervalSeconds="120"> 
    </defaultCache 
</ehcache> 

更多細節:

  • 的Tomcat 6
  • 春3.0.5
  • 的Hibernate 3.6.5
  • 的Ehcache 2.4.5

回答

4

有可能配置了Ehcache在集羣環境中使用。每個應用程序都有自己的緩存,但對一個緩存的更改會複製到羣集中的其他緩存。要使用的JGroups爲此,你可以添加類似下面你ehcache.xml中:

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory" properties="connect=UDP(mcast_addr=231.12.21.132;mcast_port=45566;ip_ttl=32; mcast_send_buf_size=150000;mcast_recv_buf_size=80000): PING(timeout=2000;num_initial_members=6): MERGE2(min_interval=5000;max_interval=10000): FD_SOCK:VERIFY_SUSPECT(timeout=1500): pbcast.NAKACK(gc_lag=10;retransmit_timeout=3000): UNICAST(timeout=5000): pbcast.STABLE(desired_avg_gossip=20000): FRAG: pbcast.GMS(join_timeout=5000;join_retry_timeout=2000; shun=false;print_local_addr=true)" propertySeparator="::"/> 

添加您defaultcache元素中的以下內容:

<cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true"/> 

我得知這個情況從一個章節Java持久性與Hibernate,我會推薦閱讀。它說上面的例子來自EhCache文檔。

如果您不希望每個應用都有自己的緩存,請閱讀Infinispan。