Ehcache 缓存组件
2016-05-24 13:16
344 查看
Ehcache介绍
ehcache是一个非常轻量级的缓存实现,ehcache 是一个纯Java的进程内缓存框架特别高效。Ehcache学习使用
使用ehcache最核心的一点就在于其ehcache.xml配置文件。ehcache.xml
<?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="false" name="talent-default-ehcache"> <diskStore path="D:/cache/ehcache.txt" /> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName). 通常保留defaultCache的配置 The defaultCache has an implicit name "default" which is a reserved cache name. --> <defaultCache maxElementsInMemory="1000000" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <!-- 定义缓存对象--> <!-- 商品品牌缓存 根据品牌id查询品牌name --> <cache name="goodsBrand" maxElementsInMemory="200" eternal="false" overflowToDisk="true" diskSpoolBufferSizeMB="200" timeToIdleSeconds="7200" timeToLiveSeconds="7200" memoryStoreEvictionPolicy="LRU"> </cache> <!-- 城市名 根据城市站id查询城市name --> <cache name="cityName" maxElementsInMemory="2000" eternal="false" overflowToDisk="true" diskSpoolBufferSizeMB="200" timeToIdleSeconds="7200" timeToLiveSeconds="7200" memoryStoreEvictionPolicy="LRU"> </cache> </ehcache>
缓存对象清除策略:
1、FIFO:first in first out,先进先出策略(不推荐使用)2、LFU:Less Frequently Used,最少使用原则(清除最少使用的元素对象)缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
3、LRU:Least Recently Used,最近最少使用原则,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
1、创建EhcacheManage对象
1)使用默认配置文件(类路径下的ehcache.xml)CacheManager manager = CacheManager.create();
2)使用指定的配置文件
CacheManager cacheManager = CacheManager.create(String configurationFileName);
3)使用输入流方式
CacheManager cacheManager = CacheManager.create(InputStream inputStream);
主要有两种方式,使用任何一种方式都可以。
2、创建Cache对象
1)获得配置文件中事先已经配置好的Cache缓存对象Cache cache=cacheManager.getCache(String name);
2)新建一个Cache并添加到CacheManager里
Cache cache=new Cache(String name, int maxElementsInMemory, MemoryStoreEvictionPolicy memoryStoreEvictionPolicy, boolean overflowToDisk, String diskStorePath, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds, boolean diskPersistent, long diskExpiryThreadIntervalSeconds, RegisteredEventListeners registeredEventListeners); 一大推相关的配置,推荐在cache.xml配置文件里面配置Cache对象,以便缓存对象管理。
3、使用Cache缓存对象
1)添加元素cache.put(Element element); Element类似java中的Entry对象,可以去看看Element的源码结构。
2)获取元素
Element element=cache.get(Object key);
3)删除元素
boolean flag=cache.remove(Object key);
自定义的工具类(EhCacheUtil)
/** * EhCache工具类,主要包含取元素和插入元素。 * @author xuyi * */ public class EhCacheUtil { private static CacheManager cacheManager; private EhCacheUtil() { //私有化构造方法 } //静态代码块,保证singleton。 static { cacheManager = CacheManager.create(); } /** * 根据缓存名字获得某个缓存 * @param cacheName * @return */ public static Cache getCache(String cacheName) { return cacheManager.getCache(cacheName); } /** * 根据缓存名字,元素的key值,获得缓存中对应的value值对象。 * @param cacheName * @param key * @param isRemoveKey * @return */ public static Object getValue(String cacheName, Object key, boolean isRemoveKey) { Cache cache = getCache(cacheName); Element e = null; if (isRemoveKey) { e = cache.get(key); } else { e = cache.getQuiet(key); } if (e == null) { return null; } return e.getObjectValue(); } /** * 根据缓存名字,元素的key值,获得缓存中对应的value值对象。 * @param cacheName * @param key * @return */ public static Object getValue(String cacheName, Object key) { return getValue(cacheName, key, false); } /** * 静态的获取元素,不会产生update. * @param cacheName * @param key * @return */ public static Element getElementByQuite(String cacheName, Object key) { Cache cache = getCache(cacheName); return cache.getQuiet(key); } /** * 动态的获取元素,会产生update. * @param cacheName * @param key * @return */ public static Element getElementByDynic(String cacheName, Object key){ Cache cache = getCache(cacheName); return cache.get(key); } /** * 向某个缓存中添加元素 * @param cacheName * @param key * @param value */ public static void put(String cacheName, Object key, Object value) { Element element = new Element(key, value); getCache(cacheName).put(element); } /** * 移除某个缓存中的元素 * @param cacheName * @param key */ public static void remove(String cacheName, Object key) { Cache cache = getCache(cacheName); if (cache != null) { cache.remove(key); } } /** * 判断某个缓存是否包含某个元素 * @param cacheName * @param key * @return */ public static boolean contains(String cacheName, Object key) { Cache cache = getCache(cacheName); Element e = cache.get(key); if (e != null) { return true; } return false; } @SuppressWarnings("unchecked") public static <T> List<T> getKeys(String cacheName, Class<T> t) { Cache cache = getCache(cacheName); return (List<T>) cache.getKeys(); } }
闲言碎语
备注:缓存这一思想在很多场景下都适用ehcache和redis缓存比较
ehcache直接在jvm虚拟机中缓存,速度快,效率高;但是缓存共享麻烦,集群分布式应用不方便。
redis是通过socket访问到缓存服务,效率比ecache低,比数据库要快很多,处理集群和分布式缓存方便,有成熟的方案。
取数逻辑,通常先到缓存对象中去查询,如果有查询到就直接返回,如果没有查询到就到数据库里查询,将查询结果存到缓存对象中,然后将数据返回给用户对象。
参考
https://en.wikipedia.org/wiki/Ehcachehttp://blog.csdn.net/jationxiaozi/article/details/8509732
http://blog.csdn.net/l271640625/article/details/20528573
http://raychase.iteye.com/blog/1545906
http://www.ibm.com/developerworks/cn/java/j-jcs.html
https://www.ibm.com/developerworks/cn/java/j-lo-ehcache/
相关文章推荐
- 页面缓存:内存和文件之间的那些事
- 插件管理框架 for Delphi(一)
- 浅析SQL Server中的执行计划缓存(上)
- Enterprise Library for .NET Framework 2.0缓存使用实例
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- 使用CSS框架布局的缺点和优点小结
- C#中缓存的基本用法总结
- 一起动手编写Android图片加载框架
- 基于.NET平台常用的框架和开源程序整理
- Android实现图片异步加载并缓存到本地
- wap开发中如何有效的利用缓存减少消息的传送量
- PHP基于文件存储实现缓存的方法
- smarty缓存用法分析
- 列举PHP的Yii 2框架的开发优势
- 在ASP.NET 2.0中操作数据之五十九:使用SQL缓存依赖项SqlCacheDependency
- 在ASP.NET 2.0中操作数据之五十八:在程序启动阶段缓存数据
- 在ASP.NET 2.0中操作数据之五十七:在分层架构中缓存数据
- Windows窗体的.Net框架绘图技术实现方法
- 引用全局程序集缓存内的程序集的方法