0
点赞
收藏
分享

微信扫一扫

ehcache缓存的使用及配置


一.介绍与应用场景

ehcache是一开源缓存工具,其许可证为Apache License, Version 2.0,非常友好的许可。在 sourceforge​​.NET​​ 上可找到它的最新版本。

缓存应用在多个领域并发挥作用,ehcache可应用于​​数据库​​访问缓存,安全认证缓存,web缓存,soap 和 RESTFul 服务缓存,应用程序持久对象缓存以及分布式缓存。

二.架设开发环境

无它,只需把ehcache的相关jar包放到classpath下,把配置文件ehcache.xml放在classpath下就可以进行应用开发了。下面是配置文件中默认配置的xml节点的内容



ehcache.xml文件配置


<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">

<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="20000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="eternalCache"
maxElementsInMemory="20000"
eternal="true"
overflowToDisk="true"
diskPersistent="false"
timeToLiveSeconds="0"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>


放在ServletContextListener实现类中启动,这里我不使用spring。需要的话你可以放在spring的配置文件中。


package com.dm.core.listener;

import com.dm.core.system.task.AutoTradeTask;
import net.sf.ehcache.CacheManager;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

/**
* @version 1.0
* @project: autoTrade
* @author:QC
* @date:2017/8/4
* @time:11:13
*/
public class AutoTradeListener implements ServletContextListener {
/**
* 定时器
*/
private Timer timer;

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("-------------AutoTradeListener.init-------------");
//初始化缓存对象
CacheManager ehcacheManager = createCache();

Calendar calendar = Calendar.getInstance();
Date firstTime = calendar.getTime();
//定义定时器,同时传入缓存对象
timer = new Timer();
long period = 1000 * 5;
timer.schedule(new AutoTradeTask(ehcacheManager), firstTime, period);
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
timer.cancel(); // 定时器销毁
//关闭缓存
CacheManager.getInstance().shutdown();
}

/**
* 创建缓存对象
*
* @return
*/
public CacheManager createCache() {
URL url = getClass().getResource("/ehcache.xml");
// CacheManager ehcacheManager = CacheManager.create(url);
// ehcacheManager.addCache("eternalCache");
return CacheManager.create(url);
}
}



web.xml中加入


<listener>
<listener-class>com.dm.core.listener.AutoTradeListener</listener-class>
</listener>



用法:


必须要有的属性:

name: cache的名字,用来识别不同的cache,必须惟一。

maxElementsInMemory: 内存管理的缓存元素数量最大限值。

maxElementsOnDisk: 硬盘管理的缓存元素数量最大限值。默认值为0,就是没有限制。

eternal: 设定元素是否持久话。若设为true,则缓存元素不会过期。

overflowToDisk: 设定是否在内存填满的时候把数据转到磁盘上。



1. //1.
2. //创建CacheManager单例对象,使用默认配置
3. CacheManager.create();
4. String[] cacheNames1 = CacheManager.getInstance().getCacheNames();
5. //关闭
6. CacheManager.getInstance().shutdown();
7.
8. //2.
9. //用默认配置创建对象可用此创建多例
10. new CacheManager();
11. String[] cacheNames2 = manager2.getCacheNames();
12. manager2.shutdown();
13.
14. //3.加载配置的方式
15. //3.1 用默认配置加载在上面已经提到。即需要在classpath下放置ehcache.xml配置文件
16. //3.2 用指定配置文件路径的方式创建
17. new CacheManager("src/ehcache.xml");
18. manager31.shutdown();
19. //3.2 用classpath下的配置文件生成
20. class.getClassLoader().getResource("/ehcache.xml");
21. new CacheManager(url);
22. manager32.shutdown();
23. //3.3 通过输入流生成
24. new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
25. try {
26. new CacheManager(fis);
27. manager33.shutdown();
28. finally {
29. fis.close();
30. }
31.
32. //4. 以编程的方式添加和删除cache
33. CacheManager singletonManager4 = CacheManager.create();
34. //4.1 用默认配置添加
35. "test41");
36. "test41");
37. null);
38. //4.2 创建一个cache并添加到CacheManager
39. new Cache("test42", 5000, false, false, 5, 2);
40. singletonManager4.addCache(memoryOnlyCache);
41. "test42");
42. //4.3 删除cache
43. "sampleCache1");
44. singletonManager4.shutdown();

举报

相关推荐

0 条评论