EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

Ehcache缓存的特点:
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现

Ehcache缓存的使用(1) – 安装ehcache
Ehcache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么,直接可以使用Ehcache 。
Ehcache缓存的使用(2) - 生成CacheManager
使用CacheManager 创建并管理Cache
1.创建CacheManager有4种方式:
A:使用默认配置文件创建
Java代码
CacheManager manager = CacheManager.create();
B:使用指定配置文件创建
Java代码
CacheManager manager = CacheManager.create("src/config/ehcache.xml");//eclipse中的路径格式。代码中使用new File()来加载配置文件。要因需调整

C:从classpath中找寻配置文件并创建
Java代码
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
D:通过输入流创建
Java代码
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}

Ehcache缓存的使用(3) – 解读Ehcache配置文件ehcache.xml
重要的参数
<diskStore path="D:/work2/renhewww/cache"/>//此处的pach属性也可以使用java.io.tmpdir
<cache name=" sampleCache1"
maxElementsInMemory="1"
maxElementsOnDisk="10000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
diskPersistent="true"
timeToIdleSeconds="43200"
timeToLiveSeconds="86400"
memoryStoreEvictionPolicy="LFU"
/>

<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" dynamicConfig="false"><diskStore path="java.io.tmpdir"/><cache name="staticResourceCache"maxElementsInMemory="1000"timeToIdleSeconds="7200"timeToLiveSeconds="7200"></cache>
</ehcache>


属性解释:

必须属性:

name:设置缓存的名称,用于标志缓存,惟一

maxElementsInMemory:在内存中最大的对象数量

maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制

eternal:设置元素是否永久的,如果为永久,则timeout忽略

overflowToDisk:是否当memory中的数量达到限制后,保存到Disk

可选的属性:

timeToIdleSeconds:设置元素过期前的空闲时间

timeToLiveSeconds:设置元素过期前的活动时间

diskPersistent:是否disk store在虚拟机启动时持久化。默认为false

diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒

memoryStoreEvictionPolicy:策略关于Eviction

缓存子元素:

cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire

bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。

Ehcache缓存的使用(4) – 创建Cache
通过CacheManager创建Cache
Cache cache = manager.getCache("sampleCache1");

Ehcache缓存的使用(5) – 利用cache存取数据
存储数据
Element element = new Element("key1", "value1");
cache.put(new Element(element);
获取数据
Element element = cache.get("key1");

缓存的创建,采用自动的方式

CacheManager singletonManager = CacheManager.create();

singletonManager.addCache("testCache");

Cache test = singletonManager.getCache("testCache");

或者直接创建Cache

CacheManager singletonManager = CacheManager.create();

Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);

manager.addCache(memoryOnlyCache);

Cache test = singletonManager.getCache("testCache");

删除cache

CacheManager singletonManager = CacheManager.create();

singletonManager.removeCache("sampleCache1");

在使用ehcache后,需要关闭

CacheManager.getInstance().shutdown()

caches 的使用

Cache cache = manager.getCache("sampleCache1");

执行crud操作

Cache.get(Object key),
Cache.put(new Element(Object key, Object value)),
Cache.remove(Object key)。

Cache cache = manager.getCache("sampleCache1");

Element element = new Element("key1", "value1");

cache.put(element);

//update

Cache cache = manager.getCache("sampleCache1");

cache.put(new Element("key1", "value1");

//This updates the entry for "key1"

cache.put(new Element("key1", "value2");

//get Serializable

Cache cache = manager.getCache("sampleCache1");

Element element = cache.get("key1");

Serializable value = element.getValue();

//get non serializable

Cache cache = manager.getCache("sampleCache1");

Element element = cache.get("key1");

Object value = element.getObjectValue();

//remove

Cache cache = manager.getCache("sampleCache1");
Element element = new Element("key1", "value1")
cache.remove("key1"); 

http://www.cnblogs.com/hubcarl/p/3257774.html

Java的进程内缓存框架:EhCache (转)相关推荐

  1. EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是hibernate中默认的CacheProvider Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是hibernate中默认的CacheProvider Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存, ...

  2. 【EhCache: 一款Java的进程内缓存框架】EhCache 是什么、代码实战、版本3的改进

    文章目录 1 EhCache 是什么 2 EhCache 版本2 代码实战 Demo pom.xml TestEH.java ehcache.xml 3 EhCache 版本3 代码实战 Demo p ...

  3. 一文深入了解史上最强的Java堆内缓存框架Caffeine

    它提供了一个近乎最佳的命中率.从性能上秒杀其他一堆进程内缓存框架,Spring5更是为了它放弃了使用多年的GuavaCache 缓存,在我们的日常开发中用的非常多,是我们应对各种性能问题支持高并发的一 ...

  4. 三大缓存框架ehcache、memcache和redis的介绍

    三大缓存框架ehcache.memcache和redis的介绍 2016-04-12 架构说 4964 阅读 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存 ...

  5. 程序员修神之路--高并发下为什么更喜欢进程内缓存

    菜菜哥,告诉你一个好消息 YY妹子,什么好消息,你有男票了? 不是啦,我做的一个网站,以前经常由于访问量太大而崩溃,现在我加上了缓存,很稳定啦 加的什么缓存呢? 我用的redis,号称业界最快的缓存组 ...

  6. 开启注解缓存_Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  7. Java 开源分布式缓存框架Ehcache

    Ehcache 是一个Java实现的开源分布式缓存框架,EhCache 可以有效地减轻数据库的负载,可以让数据保存在不同服务器的内存中,在需要数据的时候可以快速存取.同时EhCache 扩展非常简单, ...

  8. springMVC集成缓存框架Ehcache

    概述 Ehcache算是当前比较流行的缓存框架,使用缓存可以极大的缓解服务器和数据库的压力,提高访问效率,提高服务器的并发能力.接下来我们看怎么把缓存在spring mvc种使用起来. 详细 代码下载 ...

  9. 23-Mybatis集成第三方缓存框架EHCache

    上一篇:22-Mybatis缓存相关设置对一级缓存和二级缓存的影响https://blog.csdn.net/fsjwin/article/details/109685932 mybatis本身有缓存 ...

最新文章

  1. ubuntu 18.04 64bit下如何安装安卓虚拟机anbox?
  2. Sencha Touch 动态修改store的url
  3. 16 开机自动登录脚本_创建计划任务自动运行程序---运维无人值守
  4. mysql忘记密码可以卸载吗_mysql忘记密码,修改密码重新安装的一些问题
  5. Qt Creator使用灯光
  6. 格密码教程(四):SVP和CVP,Hermite定理,Blichfeld定理和Minkowski定理
  7. Linux系统调用权威指南
  8. Android学习总结(2)——App客户端与服务器交互中的token
  9. 宏图之下服务器维护,《鸿图之下》3月24日维护更新预告
  10. 为什么磁盘慢会导致Linux负载飙升?
  11. CPU自制入门 第三章 编程
  12. 简单的朴素贝叶斯算法实现英文文本分类(Python实现)
  13. stm32项目实战ST7735环境质量检测仪
  14. portal服务器信息超时,某局点iMC-EIA Portal认证提示“向Portal Server发送请求超时”经典案例...
  15. 10只小白鼠1000支药水找出毒药问题
  16. uniapp + vue3微信小程序开发(4)身份信息认证
  17. SpringCloud OpenFeign 服务调用传递 token
  18. 多目标遗传算法及MATLAB代码
  19. 2022-2027年中国报警器IC行业市场全景评估及投资潜力预测报告
  20. 笔记摘抄div+css

热门文章

  1. 文本溢出隐藏显示省略号失效
  2. b站视频突破2倍方法,3倍?4倍?可以开10倍!!!
  3. C/C++后台开发基础知识
  4. 远程链接linux桌面的软件,远程linux桌面软件
  5. PAT_乙级_1011_筱筱
  6. xml文件格式化脚本
  7. java抽象类存在的意义
  8. 新手学Python之学习官网教程(一: Whetting Your Appetite)
  9. W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419b4c50)
  10. b、B、KB、MB、GB 的关系?