Ehcache分布式缓存的最小化配置
注意:分布式配置依赖于ehcache-1.3.0.jar、backport-util-concurrent.jar包,ehcache必须是1.3以上。
配置ehcache作为分布式缓存,需要修改ehcache.xml,增加下面的内容:
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446"/> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>
and then at least one cache declaration with
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
in it. An example cache is:
<cache name="sampleDistributedCache1" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" overflowToDisk="false"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> </cache>
Each server in the cluster can have the same config.
Ehcache API的用法:
Set set=new HashSet();
set.add("广州");
set.add("香港");net.sf.ehcache.CacheManager manager = new net.sf.ehcache.CacheManager();
Cache cache = manager.getCache("a");
Element element = new Element("key1", set);
cache.put(element);
Element element2 = cache.get("key1");
Serializable value = element2.getValue();
System.out.println("value:"+value);