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();
    }

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

转载于:https://my.oschina.net/lovederh/blog/604133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值