Ehcache 缓存

Posted Eee_xiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ehcache 缓存相关的知识,希望对你有一定的参考价值。

 1 package org.jeecgframework.core.util;
 2 
 3 import net.sf.ehcache.Cache;
 4 import net.sf.ehcache.CacheManager;
 5 import net.sf.ehcache.Element;
 6 
 7 /**
 8  * ehcache 缓存工具类
 9  * 
10  * cacheName在ehcache.xml中配置
11  */
12 public class EhcacheUtil {
13 
14     public static CacheManager manager = CacheManager.create();
15 
16     public static Object get(String cacheName, Object key) {
17         Cache cache = manager.getCache(cacheName);
18         if (cache != null) {
19             Element element = cache.get(key);
20             if (element != null) {
21                 return element.getObjectValue();
22             }
23         }
24         return null;
25     }
26 
27     public static void put(String cacheName, Object key, Object value) {
28         Cache cache = manager.getCache(cacheName);
29         if (cache != null) {
30             cache.put(new Element(key, value));
31         }
32     }
33 
34     public static boolean remove(String cacheName, Object key) {
35         Cache cache = manager.getCache(cacheName);
36         if (cache != null) {
37             return cache.remove(key);
38         }
39         return false;
40     }
41 
42     public static void main(String[] args) {
43         String key = "key";
44         String value = "hello";
45         EhcacheUtil.put("mytest", key, value);
46         //System.out.println(EhcacheUtil.get("mytest", key));
47     }
48 
49 }
技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache updateCheck="false">
 3 
 4     <diskStore path="java.io.tmpdir" />
 5 
 6     <!-- Cluster localhost setting -->
 7     <cacheManagerPeerProviderFactory
 8         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
 9         properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
10         multicastGroupPort=4446, timeToLive=32"/>
11     
12     <cacheManagerPeerListenerFactory
13         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
14         properties="hostName=localhost, port=40001,socketTimeoutMillis=2000" />
15 
16 
17     <cache name="dictCache" maxElementsInMemory="500" overflowToDisk="true"
18         eternal="true">
19         <cacheEventListenerFactory
20             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
21             properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
22     </cache>
23 
24     <cache name="eternalCache" maxElementsInMemory="500"
25         overflowToDisk="true" eternal="false" timeToIdleSeconds="1200"
26         timeToLiveSeconds="1200">
27         <cacheEventListenerFactory
28             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
29             properties="replicatePuts=false,replicateUpdatesViaCopy=false" />
30     </cache>
31     <!-- DefaultCache setting. Modify ehcache-safe.xml for timeToIdleSeconds,timeToLiveSecond,diskExpiryThreadIntervalSeconds 
32         Use ehcache-safe.xml default for maxElementsInMemory,maxElementsOnDisk,overflowToDisk,eternal 
33         Use ehcache default for memoryStoreEvictionPolicy,diskPersistent,. -->
34     <defaultCache maxElementsInMemory="10000" overflowToDisk="true"
35         eternal="false" memoryStoreEvictionPolicy="LRU" maxElementsOnDisk="10000000"
36         diskExpiryThreadIntervalSeconds="600" timeToIdleSeconds="3600"
37         timeToLiveSeconds="100000" diskPersistent="false" />
38 </ehcache>
View Code

 

以上是关于Ehcache 缓存的主要内容,如果未能解决你的问题,请参考以下文章

Ehcache缓存框架详解

Ehcache缓存框架具体解释

springmvc结合ehcache实现共享对象缓存

ehcache java 对象缓存怎么实现

ssm+ehcache ssm简单整合ehcache缓存

java ehcache