JFinal中EhCache利用RMI方式分布式缓存配置

1. 环境二台pc,ip地址分别为ip1:192.168.2.100, ip2: 192.168.2.249

2. JFinal框架的开发环境

3. 配置XML

ip2的xml配置只要将ip地址调换过来即可。

其中 ip1机器的xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="false" monitoring="autodetect"
         dynamicConfig="true">
        
    <diskStore path="java.io.tmpdir"/>
   
    <defaultCache
           maxEntriesLocalHeap="10000"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="20"
           timeToLiveSeconds="60">
    </defaultCache>
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,
        rmiUrls=//192.168.2.249:40001/loginCache|//192.168.2.249:40001/answer"
        propertySeparator="," 
         />
       
    <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=192.168.2.100,
                 port=40001,
                 socketTimeoutMillis=2000" />
     <!--
     ·    将系统登陆用户信息写到缓存里面 ,可以进行验证
      -->
     <cache name="loginCache"
           maxEntriesLocalHeap="500"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="1"
           memoryStoreEvictionPolicy="LFU"
            >     
            <cacheEventListenerFactory 
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
                properties="replicateAsynchronously=true, replicatePuts=true, 
                            replicatePutsViaCopy=true, replicateUpdates=true, 
                            replicateUpdatesViaCopy=true, replicateRemovals=true, 
                            asynchronousReplicationIntervalMillis=200"/> 
        <bootstrapCacheLoaderFactory 
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> 
      </cache>
    <!--
    Sample cache named sampleCache2
    This cache has a maximum of 1000 elements in memory. There is no overflow to disk, so 1000
    is also the maximum cache size. Note that when a cache is eternal, timeToLive and
    timeToIdle are not used and do not need to be specified.
        答题验证
    -->
    <cache name="answer"
           maxEntriesLocalHeap="1000"
           eternal="true"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="FIFO"
            >
             <searchable/>
    </cache>

   </ehcache>
4. 在JFinal启动的时候进行缓存预处理,在Config文件中加入如下代码。这样在项目启动后即可将缓存加载。

@Override
    public void afterJFinalStart() {
        //系统启动后做缓存数据处理代码
        CacheManager.LoadCache();
        super.afterJFinalStart();
    }

说明: 在项目启动后,会进行一次缓存同步。之后当缓存中有更新,增加,删除等操作会自动进行同步。

你可能感兴趣的:(JFinal中EhCache利用RMI方式分布式缓存配置)