摘要:=null){19Elementelement=cache.get(key);20if(element!=null){21returnelement.getObjectValue();22}23}24returnnull;25}2627publicstaticvoidput(StringcacheName,Objectkey,Objectvalue){28Cachecache=manager.getCache(cacheName);29if(cache!=null){30cache.put(newElement(key,value));31}32}3334publicstaticbooleanremove(StringcacheName,Objectkey){35Cachecache=manager.getCache(cacheName);36if(cache!=null){37returncache.remove(key);38}39returnfalse;40}4142publicstaticvoidmain(String[]args){43Stringkey="key";44Stringvalue="hello";45EhcacheUtil.put("mytest",key,value);46//System.out.println(EhcacheUtil.get("mytest",key));47}4849}123456˂!
1 packageorg.jeecgframework.core.util;
2
3 importnet.sf.ehcache.Cache;
4 importnet.sf.ehcache.CacheManager;
5 importnet.sf.ehcache.Element;
6
7 /**
8 * ehcache 缓存工具类
9 *
10 * cacheName在ehcache.xml中配置
11 */
12 public classEhcacheUtil {
13
14 public static CacheManager manager =CacheManager.create();
15
16 public staticObject 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 returnelement.getObjectValue();
22 }
23 }
24 return null;
25 }
26
27 public static voidput(String cacheName, Object key, Object value) {
28 Cache cache =manager.getCache(cacheName);
29 if (cache != null) {
30 cache.put(newElement(key, value));
31 }
32 }
33
34 public static booleanremove(String cacheName, Object key) {
35 Cache cache =manager.getCache(cacheName);
36 if (cache != null) {
37 returncache.remove(key);
38 }
39 return false;
40 }
41
42 public static voidmain(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 fortimeToIdleSeconds,timeToLiveSecond,diskExpiryThreadIntervalSeconds
32 Use ehcache-safe.xml default formaxElementsInMemory,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