您的位置:首页 > 其它

缓存数据 ehcache

2016-03-09 15:42 232 查看
ehcache-core-2.6.5.jar

1.创建CacheManager

CacheManager manager = CacheManager.create();
CacheManager manager = CacheManager.create("xx/xx/xx/ehcache.xml");
InputStream fis = new FileInputStream(new File("xx/xx/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}


读取配置文件中的 Cache

Cache ca = manager.getCache("accesstokenCache");
Element  element = ca.get(weixintokenkey);
if(element == null){

}else{
String token  = element.getValue().toString();

}


2.1 创建1个Cache

CacheManager manager = CacheManager.create();
Cache cache = new Cache("test", 1, true, false, 5, 2);
manager.addCache(cache);

//这里在CacheManager中直接加入了一个的cache。如果不设置其他参数,
//只给了一个名字,所以系统会把defaultCache的设置给它clone一份。
//使用方法,像字典一样使用就行:

String names[] = manager.getCacheNames();
for(int i=0;i<names.length;i++){
System.out.println(names[i]);
}


3.放入元素

Element  element = new Element(key,value);
demo.put(element);


4.关闭

manager.shutdown();


xml文件的样子。

<ehcache>
<diskStore path="java.io.tempdir" />
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="ehcacheName" maxElementsInMemory="10000"
eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
overflowToDisk="true" />
</ehcache>




<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true">

<diskStore path="java.io.tmpdir"/>

<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600">
</defaultCache>

<!--system cache name sysCache-->
<cache name="sysCache"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="20000"
eternal="false"
overflowToDisk="false"
diskSpoolBufferSizeMB="50"
timeToIdleSeconds="1800"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off"
/>

<cache name="accesstokenCache"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="20000"
eternal="false"
overflowToDisk="false"
diskSpoolBufferSizeMB="50"
timeToIdleSeconds="7000"
timeToLiveSeconds="7000"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off"
/>

</ehcache>
<!-- maxEntriesLocalHeap="10000" 内存最多1W条-->
<!-- maxEntriesLocalDisk="20000" 磁盘最多2W条-->
<!-- eternal="false" 是否自动过期-->
<!-- overflowToDisk="true" 是否自动存磁盘-->
<!-- diskSpoolBufferSizeMB="50" DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore-->
<!-- timeToIdleSeconds="1800" 空闲过期时间-->
<!-- timeToLiveSeconds="3600" 最长使用时间-->
<!-- memoryStoreEvictionPolicy="LFU" 缓存策略-->
<!-- transactionalMode="off" 使ehcache作为JTA事务的参与者-->
<!-- FIFO  (先进先出). -->
<!-- LFU 最少使用) -->
<!-- LRU 最近最少使用(ehcache 默认值) -->


spring文件中的配置

如果配置文件配置了的话

直接用

CacheManager manager = CacheManager.create(); 就好了

声明cacheManager
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"></bean>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" ></bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: