java ehcache 分布式缓存配置
2012-11-12 11:13:12 来源:WEB开发网基本环境:A 分别创建两个web项目,C1和C2 分别倒入echcache的jar包;
B 本例使用了两个tomcat 分别部署C1和C2
项目配置:C1配置
A
ehcache.xml [ path= src ]
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="java.io.tmpdir/ehcache"/>
<cache name="cache"
maxElementsInMemory="19"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
</cache>
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.10.114, port=40001,socketTimeoutMillis=2000"/>
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,hostName=192.168.10.114,port=40002,rmiUrls=//192.168.10.114:40001/cache|//192.168.10.114:40002/cache,timeT
oLiv e=32" />
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
</ehcache>
这样就配置了192.168.10.114:40001和192.168.10.114:40002两个ehcache缓存实例 并且每当其中一个缓存放生改变时通过在
properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true" 这些配置策略
可以同步反应到另一个缓存中.从而达到分布式缓存同步的效果.
B web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>EhCache Cluster Demo</display-name>
<!-- listeners -->
<display-name>EhCache Cluster Demo</display-name>
<context-param>
<param-name>eccache</param-name>
<param-value>ehcache.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
C. test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%@page import="net.sf.ehcache.CacheManager"%>
更多精彩
赞助商链接