Ehcache的配置与使用

本文介绍了Ehcache缓存框架的基本配置方法及其在Java应用中的使用方式。通过示例展示了如何设置缓存参数,包括内存中缓存的最大数量、是否溢出到磁盘等,并提供了存储和读取数据的具体代码。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值