博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
缓存框架:EhCache
阅读量:7092 次
发布时间:2019-06-28

本文共 3346 字,大约阅读时间需要 11 分钟。

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 Ehcache缓存的特点:1. 快速.2. 简单.3. 多种缓存策略4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题5. 缓存数据会在虚拟机重启的过程中写入磁盘6. 可以通过RMI、可插入API等方式进行分布式缓存7. 具有缓存和缓存管理器的侦听接口8. 支持多缓存管理器实例,以及一个实例的多个缓存区域9. 提供Hibernate的缓存实现 Ehcache缓存的使用(1) – 安装ehcacheEhcache 的特点,是一个纯Java ,过程中(也可以理解成插入式)缓存实现,单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。如项目已安装了Hibernate ,则不需要做什么,直接可以使用Ehcache 。 Ehcache缓存的使用(2) - 生成CacheManager使用CacheManager 创建并管理Cache1.创建CacheManager有4种方式:A:使用默认配置文件创建Java代码1.CacheManager manager = CacheManager.create();  B:使用指定配置文件创建Java代码1.CacheManager manager = CacheManager.create("src/config/ehcache.xml");  C:从classpath中找寻配置文件并创建Java代码1.URL url = getClass().getResource("/anothername.xml"); 2.CacheManager manager = CacheManager.create(url);  D:通过输入流创建Java代码1.InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath()); 2.try { 3.manager = CacheManager.create(fis); 4.} finally { 5.fis.close(); 6.}  Ehcache缓存的使用(3) – 解读Ehcache配置文件ehcache.xml重要的参数
 属性解释:必须属性:        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创建CacheCache 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 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");

转载于:https://my.oschina.net/vshcxl/blog/648443

你可能感兴趣的文章
点滴积累【JS】---JS小功能(onmousedown实现鼠标拖拽div移动)
查看>>
BinaryReader 、BinaryWriter是方便用二进制方式读写int,double,string之类的数据
查看>>
(转)Linux下运行python
查看>>
【转】解决weblogic启动慢和创建域慢的方法
查看>>
信息安全基础第一周作业
查看>>
GET和POST区别
查看>>
MySql 数据库系列问题
查看>>
BFC是什么?有什么作用?
查看>>
010.简单查询、分组统计查询、多表连接查询(sql实例)
查看>>
3_Windows下利用批处理文件_去除C源代码中指示行号的前导数字
查看>>
Bzoj 1853: [Scoi2010]幸运数字 容斥原理,深搜
查看>>
Hdu 4311-Meeting point-1 曼哈顿距离,前缀和
查看>>
docker网络介绍之bridge网络详解
查看>>
两个PHP方面的东西,超过2038的时间和唯一订单号算法
查看>>
凡事预则立
查看>>
函数的定义域【基础辅导】
查看>>
Selenium 自动化验收测试
查看>>
Alpha冲刺随笔集
查看>>
站立会议04
查看>>
jquery 阻止默认事件(传播和冒泡)
查看>>