转自:https://blog.csdn.net/w1014074794/article/details/51086228

Ehcache简介

目录

1       CacheManager

1.1      构造方法构建

1.2      静态方法构建

2       Cache

2.1      Cache的创建

Ehcache是用来管理缓存的一个工具,其缓存的数据可以是存放在内存里面的,也可以是存放在硬盘上的。其核心是CacheManager,一切Ehcache的应用都是从CacheManager开始的。它是用来管理Cache(缓存)的,一个应用可以有多个CacheManager,而一个CacheManager下又可以有多个Cache。Cache内部保存的是一个个的Element,而一个Element中保存的是一个key和value的配对,相当于Map里面的一个Entry。

1       CacheManager

CacheManager是Ehcache的核心,它的主要职责是对Cache的创建、移除和访问。只有CacheManager里面的Cache才能实现缓存数据的功能。一切使用Ehcache的应用都是从构建CacheManager开始的。构建CacheManager时,我们可以直接通过其构造方法来进行构建,也可以通过使用CacheManager提供的静态方法来进行构建。

1.1     构造方法构建

使用构造方法构建CacheManager时每次都会产生一个新的CacheManager对象,并且会以该CacheManager对应的name作为key保存该CacheManager。当我们在构建CacheManager时如果已经存在name相同正在使用的CacheManager,则会抛出异常。此外,当多个CacheManager对应的storePath相同时,则它们存放在磁盘上包含缓存信息的文件将会相互覆盖。

1.使用默认配置

当我们使用CacheManager的无参构造方法来构造CacheManager时就是使用的默认配置。这种情况最终还是会寻找默认的配置文件进行配置。Ehcache首先会到类根路径下寻找一个叫ehcache.xml的配置文件来配置CacheManager,如果没有找到该文件,则会加载CacheManager的默认配置ehcache-failsafe.xml文件,这个文件是在ehcache.jar里面的。关于配置文件如何配置的问题将在后续的文章中再做一个详细的讲解,这里先来简单看一个配置文件。

 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <cache name="test" maxBytesLocalHeap="10M"/> </ehcache>

每一个配置文件的根元素都是ehcache,在该元素上可以指定一些CacheManager级别的参数。ehcache元素下的每一个cache元素代表一个缓存定义。cache元素上可以指定一些Cache级别的属性。下面是一个使用默认配置构建CacheManager的示例。

 package com.asm;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; public class Test { @org.junit.Test
public void test() {
CacheManager cacheManager = new CacheManager();
// 输出当前cacheManager正在使用的配置对应的Xml格式文本
System.out.println(cacheManager.getActiveConfigurationText());
} }

2.以Configuration作为参数

Configuration是用来指定CacheManager配置信息的,其它通过不同的方式所指定的构造参数最终都会转化为一个对应的Configuration对象,然后再利用该Configuration对象初始化CacheManager。

 package com.asm;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; public class Test { @org.junit.Test
public void test1() {
// 新建一个缓存的配置信息
CacheConfiguration cacheConfiguration = new CacheConfiguration("test",
100000);
CacheManager cacheManager = new CacheManager();
// 通过缓存配置创建缓存
Cache cache = new Cache(cacheConfiguration);
cacheManager.addCache(cache);
cache.put(new Element("test", "test"));
System.out.println(cache.get("test").getObjectValue()); } }

3.以xml格式的配置对应的InputStream作为参数

通过Xml格式的配置对应的InputStream作为参数时,Ehcache会对Xml进行解析,然后构造一个对应的Configuration对象。

 package com.asm;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; public class Test { @org.junit.Test
public void test2() throws IOException { InputStream is = this.getClass().getClassLoader()
.getResourceAsStream("/ehcache.xml");
CacheManager cacheManager = new CacheManager(is);
System.out.println(cacheManager.getActiveConfigurationText());
} }

4.以xml格式的配置文件对应的路径作为参数

指定xml格式的配置文件对应的路径后,Ehcache会获取指定路径对应的配置文件,然后获取其输入流,再利用InputStream的方式构造CacheManager。这里的路径可以是相对路径,也可以是绝对路径。

 package com.asm;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; public class Test { @org.junit.Test
public void test4() {
URL url = this.getClass().getResource("/ehcache.xml");
System.out.println(url);
CacheManager cacheManager = new CacheManager(url);
System.out.println(cacheManager.getActiveConfigurationText());
}
}

1.2     静态方法构建

在CacheManager内部定义了一系列的用于构建CacheManager对象的静态方法。这主要可以分为两大类,一类是通过create()方法及其重载方法构建的,一类是通过newInstance()方法及其重载方法构建的。create()方法构建的都是单例,而newInstance()方法构建的可能是单例,也可能是多例。在CacheManager内部持有一个CacheManager类型的singleton对象,每次我们调用create()方法及其重载方法时,Ehcache都会判断当前的singleton对象是否非空,如果非空则直接返回,否则则以相应的配置构建一个CacheManager对象赋给singleton并进行返回。在调用newInstance()方法及其重载方法构建CacheManager时,Ehcache首先会判断我们之前是否创建过且还存在同样名称的CacheManager对象,如果有则直接返回该CacheManager对象,否则将新建一个CacheManager进行返回。所以调用CacheManager的newInstance()系列方法构建CacheManager与直接调用CacheManager的构造方法构造CacheManager对象的区别就在于调用newInstance()系列方法时如有同名的存在,会直接返回先前的,而不会抛出异常。此外CacheManager内部还定义了一个getInstance()静态方法,调用它时相当于是调用了不带参数的create()方法。

1.create()方法

在CacheManager内部一共定义有五个create()方法,分别对应于CacheManager的五个newInstance()方法,而每一个newInstance()方法又对应于CacheManager对应的构造方法。在调用时Ehcache会先判断CacheManager内部持有的singleton是否为空,非空则直接返回singleton,否则将返回对应参数的newInstance()返回的实例对象并赋值给singleton。

 @org.junit.Test
public void test5(){
//以默认配置创建一个CacheManager单例
CacheManager cacheManager = CacheManager.create(); //以config对应的配置创建CacheManager单例
Configuration config = ...;//以某种方式获取的Configuration对象
cacheManager = CacheManager.create(config); //以configurationFileName对应的xml文件定义的配置创建CacheManager单例
String configurationFileName = ...;//xml配置文件对应的文件名称,包含路径
cacheManager = CacheManager.create(configurationFileName); //以is对应的配置信息创建CacheManager单例
InputStream is = ...; //以某种方式获取到的Xml配置信息对应的输入流
cacheManager = CacheManager.create(is); //以URL对应的配置信息创建CacheManager单例
URL url = ...; //以某种方式获取到的Xml配置信息对应的URL
cacheManager = CacheManager.create(url);
}

1.3     CacheManager的关闭

当我们不再需要使用CacheManager的时候,我们需要将CacheManager进行关闭。Ehcache为我们提供了一个关闭CacheManager的钩子,默认情况下是不可用的,通过设置系统属性net.sf.ehcache.enableShutdownHook=true就可以将该功能打开。但是官方还是推荐我们在程序里面调用CacheManager的shutdown()方法来将当前CacheManager进行关闭。

2       Cache

在Ehcache中定义了一个对缓存进行处理的接口叫Ehcache,Cache是Ehcache的一个实现类。Cache是由CacheManager进行管理的,使用CacheManager生成的就是一个Cache对象。Cache里面保存的是一个个的Element对象,这些对象通常都是保存在MemoryStore里面的,但也可以溢出到DiskStore。Element里面存放的是一个key和value的配对,其中key和value都是Object。Cache的创建可以事先在创建CacheManager的时候定义好,也可以在之后调用CacheManager实例的相关方法进行Cache的添加。Cache是线程安全的。

 package com.asm;

 import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration; public class Test { @org.junit.Test
public void cache(){ //内存中保存的Element的最大数量
int maxEntriesLocalHeap = 10000; CacheConfiguration cacheConfiguration = new CacheConfiguration("test",maxEntriesLocalHeap);
cacheConfiguration.overflowToDisk(false);
Cache cache = new Cache(cacheConfiguration); CacheManager cacheManager = CacheManager.create();
cacheManager.addCache(cache);
cache.put(new Element("key", "value"));
System.out.println(cache.get("key")); } }

2.2     Cache内容的CRUD

Cache内容的CRUD是指对Cache中保存的元素进行CRUD操作。

(1)新增元素

新增元素可以通过Cache的put(Element ele)方法来进行。Element是键值对形式,我们真正想要缓存的其实是Element的value,但是我们可以通过key来区别不同的value。同时Element中还包括我们缓存的一些额外信息,如缓存的时间等。Element的key和value类似于Map的key和value,均可为Object对象。

 package com.asm;

 import org.junit.After;
import org.junit.Before;
import org.junit.Test; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element; public class CacheCRUDTest { private CacheManager cacheManager; @Before
public void before() {
cacheManager = CacheManager.create();
// System.out.println(cacheManager);
cacheManager.addCache("cache");
// System.out.println(cacheManager);
} @After
public void after() {
cacheManager.shutdown();
} @Test
public void test() { Cache cache = cacheManager.getCache("cache");
Element ele = new Element("key", "value");
// 把ele放入缓存cache中
cache.put(ele);
}
}

 

(2)获取元素

获取元素的时候我们可以通过Cache的get()方法来进行的,其接收的参数是元素的key。

  

    /**
* 从Cache中读取元素
*/
@Test
public void read() {
Cache cache = cacheManager.getCache("cache");
//通过key来获取缓存中对应的元素
Element ele = cache.get("key");
System.out.println(ele);
if (ele != null) {//当缓存的元素存在时获取缓存的值
System.out.println(ele.getObjectValue());
}
}

(3)更新元素

当我们在往Cache里面put元素的时候,如果Cache中已经存在相同key的元素了,则会用新的元素替换旧的元素,这也就意味着之前元素的一些信息将会丢失,如被查到的次数hitCount和创建时间等。

    /**
* 更新元素
*/
@Test
public void update() {
Cache cache = cacheManager.getCache("cache");
cache.put(new Element("key", "value1"));
System.out.println(cache.get("key"));
//当添加元素的时候,如果缓存中已经存在相同key的元素则会将后者覆盖前者
cache.put(new Element("key", "value2"));
System.out.println(cache.get("key"));
}

此外,使用Cache的replace(Element ele)方法也可以更新Cache中对应的元素。与直接put更新不同的是,replace只会在Cache中拥有相同key的元素时才会对之前的元素进行更新。replace也会覆盖之前元素信息。

    /**
* 更新元素
*/
@Test
public void update() {
Cache cache = cacheManager.getCache("cache");
cache.put(new Element("key", "value1"));
System.out.println(cache.get("key"));
//当添加元素的时候,如果缓存中已经存在相同key的元素则会将后者覆盖前者
cache.put(new Element("key", "value2"));
System.out.println(cache.get("key"));
}

(4)删除元素

删除元素是通过Cache的remove()方法进行的,其接收所要删除元素的key作为参数。

 /**
* 根据key来移除一个元素
*/
@Test
public void delete() {
Cache cache = cacheManager.getCache("cache");
//根据key来移除一个元素
cache.remove("key");
System.out.println(cache.get("key"));
}

ehcache.2.8.1.jar

1.Ehcache(01)——简介、基本操作的更多相关文章

  1. Node.js 教程 01 - 简介、安装及配置

    系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...

  2. Ehcache(02)——ehcache.xml简介

    http://haohaoxuexi.iteye.com/blog/2113728 ehcache.xml简介 ehcache.xml文件是用来定义Ehcache的配置信息的,更准确的来说它是定义Ca ...

  3. Python 黑客 004 用Python构建一个SSH僵尸网络 01 简介

    用Python构建一个SSH僵尸网络 01 简介 一. 构建一个SSH僵尸网络的流程图: Created with Raphaël 2.1.0手动操作,实现通过SSH连接目标服务器(手动)用 Pexp ...

  4. Linux 时间同步 01 简介

    Linux 时间同步 01 简介 目录 Linux 时间同步 01 简介 时间同步 公共NTP服务器地址及IP 系统时间相关文件 时间同步 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同 ...

  5. 2.ehcache.xml简介

    转自:https://www.cnblogs.com/crazylqy/p/4238148.html ehcache.xml文件是用来定义Ehcache的配置信息的,更准确的来说它是定义CacheMa ...

  6. MongoDB学习笔记-01 简介、安装

    MongoDB简介 MongoDB是一种强大.灵活.可拓展的存储方式.是一个面向文档(相当于"行"的概念)的数据库. 可拓展:通过添加服务器而增加存储量. Windows下安装 版 ...

  7. ElasticSearch学习笔记-01 简介、安装、配置与核心概念

    一.简介 ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进 ...

  8. Inside Kolla - 01 简介

    简介 在 opencloud 2015 的会议上初次了解到 kolla 项目后,开始去了解和学习 kolla 的源代码和相关的知识.经过一段时间的了解,觉得 kolla 确实是一个很好的项目,它使用预 ...

  9. Objective-C:01简介

    1.Objective-C简介 Objective-C是一种面向对象的计算机语言 OC不是一门全新的语言 C语言的基础上增加了一层最小的面向对象语法 OC完全兼容C语言 可以在OC代码中混入C语言代码 ...

  10. Spring Cloud开发实践 - 01 - 简介和根模块

    简介 使用Spring Boot的提升主要在于jar的打包形式给运维带来了很大的便利, 而Spring Cloud本身的优点不是那么明显, 相对于Dubbo而言, 可能体现在跨语言的交互性上(例如可以 ...

随机推荐

  1. linux三尖刀

    序 我们都知道,一个可执行程序的基本的生命过程是如此的: (编辑)源文件--->(编译)目标文件--->(链接)可执行文件--->(调试排错)稳定执行 所以,在这个过程中,我们很容易 ...

  2. IOS-高仿bilibili项目

    高仿bilibili项目成长之路 (logo) 高仿bilibili项目 Github链接:(https://github.com/MichaelHuyp/Bilibili_Wuxianda) 目前完 ...

  3. IOS-网络(HTTP请求、同步请求、异步请求、JSON解析数据)

    // // ViewController.m // IOS_0129_HTTP请求 // // Created by ma c on 16/1/29. // Copyright © 2016年 博文科 ...

  4. Mac iStat Menu 注册码

    9185-4915-3252-3716-0000 1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 ...

  5. java Object对象的clone方法

    参考copy链接:http://blog.csdn.net/bigconvience/article/details/25025561 在看原型模式,发现要用到clone这个方法,以前和朋友聊过,没怎 ...

  6. 201621123005《Java程序设计》第十三次实验总结

    <Java程序设计>第十三周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主 ...

  7. (转)RESTful API 设计最佳实践

    原文:http://www.oschina.net/translate/best-practices-for-a-pragmatic-restful-api 数据模型已经稳定,接下来你可能需要为web ...

  8. emmc boot_config文件不存在

    /******************************************************************************* * emmc boot_config文 ...

  9. 解决使用SecureCRT出现的Generic clipboard failure错误【自己亲身经历】

    2016年11月8日:[解决办法]把金山词霸卸载了 血的教训浪费了好几个小时 相关文章 1.RecureCRT could not get data from the Clipboard 和SAP快捷 ...

  10. springboot整合mybatis增删改查(四):完善增删改查及整合swgger2

    接下来就是完成增删改查的功能了,首先在config包下配置Druid数据连接池,在配置之前先把相关配置在application.preperties中完善 application.preperties ...