Ehcache的配置与使用

Ehcache是JAVA内制的一个缓存框架!

目的:缓解频繁读取数据库的压力;

初步配置如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ehcache updateCheck="false"  name="shirocache">
 3     <diskStore path="java.io.tmpdir"/>
 4     <defaultCache    
 5         maxElementsInMemory="10000"    
 6         maxElementsOnDisk="0"    
 7         eternal="true"    
 8         overflowToDisk="true"    
 9         diskPersistent="false"    
10         timeToIdleSeconds="0"    
11         timeToLiveSeconds="0"    
12         diskSpoolBufferSizeMB="50"    
13         diskExpiryThreadIntervalSeconds="120"    
14         memoryStoreEvictionPolicy="LFU"    
15     />      
16     <cache name="shiro_cache"
17            maxElementsInMemory="2000"
18            maxEntriesLocalHeap="2000"
19            eternal="false"
20            timeToIdleSeconds="0"
21            timeToLiveSeconds="0"
22            maxElementsOnDisk="0"
23            overflowToDisk="true"
24            memoryStoreEvictionPolicy="FIFO"
25            statistics="true">
26     </cache>
27 </ehcache>

使用如下:

1、存储数据;

1                    CacheManager cacheManager = CacheManager.create(url);
2                    Cache cache=cacheManager.getCache("shiro_cache");
3                    Element element = new Element("pwd", password);
4                    cache.put(element);

2、读取数据 

1             URL url = getClass().getResource("/ehcache/ehcache.xml");
2             CacheManager cacheManager = CacheManager.create(url);
3             Cache cache=cacheManager.getCache("shiro_cache");
4             Element element = cache.get("pwd");
5             System.out.println(element.toString());
原文地址:https://www.cnblogs.com/XinHuai/p/6866216.html