CacheManager是Ehcache框架的核心类和入口,它负责管理一个或多个Cache对象。要使用Ehcache框架,必须要先创建 CacheManager 对象。现在我们学习下,如何创建 CacheManager 对象。

1.使用默认配置文件创建单例对象

CacheManager提供了很多无参的static创建方法,我们可以获取唯一的实例,代码如下:

public static void main(String[] args)
{
CacheManager mgr1 = CacheManager.getInstance();
CacheManager mgr2 = CacheManager.create();
CacheManager mgr3 = CacheManager.newInstance(); System.out.println(mgr1 == mgr2);// true
System.out.println(mgr1 == mgr3);// true
}

这3种方式都是等价,获取的唯一实例也是相同的。通过源代码可以很容易的发现,这3个函数就是互相调用的关系。使用这种方式,Ehcahce会报警告,因为我们没有提供自定义的cache配置文件:

警告: No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:ehcache-2.8.1.jar!/ehcache-failsafe.xml

使用ehcache.jar中默认的缓存配置文件来创建EhcahceManager对象,这种方式实际上使用并不多。默认的配置在大多数情况下,都是不能满足应用程序的使用需要。不推荐这种使用方式。

2.使用自定义的ehcache.xml创建单例对象

使用默认的缓存配置文件,很可能不能满足应用的使用,一般来说应用很少会使用默认的ehcache-failsafe.xml 。这个时候,我们可以自己指定ehcache.xml来创建单一CacheManager对象,使用带有参数的static创建类型的方法。

URL url = TestCacheManager.class.getClassLoader().getResource("conf/ehcache.xml");
CacheManager mgr1 = CacheManager.create(url);
CacheManager mgr2 = CacheManager.create("src/conf/ehcache.xml");
CacheManager mgr3 = CacheManager.newInstance("src/conf/ehcache.xml"); System.out.println(mgr1 == mgr2);// true
System.out.println(mgr1 == mgr3);// true

可以发现:使用CacheManager的static方法,在指定了自定义的缓存配置文件的情况下,创建的仍然是唯一的单例对象。在小规模的应用中,我们可以在ehcache.xml中配置所有需要的<cache>。这样就能够使用一个CacheManager对象,来管理配置文件中的多个<cache>

3.static类型创建方法,有参和无参的区别

CacheManager类static类型的创建方法包括:create()、getInstance()、newInstance(),这些API让我们可以使用默认的缓存配置文件,也可以使用自定义的配置文件,来创建CacheManager对象。一般来说,我们在应用中不会既使用无参的static创建方法,又使用有参的static创建方法。这种使用方式是不合理的,也是没有必要。我有点细节控,还是看下能不能同时使用这2种方式。

CacheManager mgr1 = CacheManager.create("src/conf/ehcache.xml");

CacheManager mgr4 = CacheManager.create();
CacheManager mgr5 = CacheManager.getInstance();
CacheManager mgr6 = CacheManager.newInstance(); System.out.println(mgr1 == mgr4);// true
System.out.println(mgr1 == mgr5);// true
System.out.println(mgr1 == mgr6);// false

当使用ehcache.xml创建CacheManager对象的时候,CacheManager中的singleton属性会记录创建的对象值,即创建了CacheManager对象, singleton会记录该单例对象,不再是null ;

CacheManager.create()和CacheManager.getInstance()都会先判断 singleton属性是否为null,如果为null则继续调用newInstance(),如果不为null则直接返回。所以mgr1==mgr4==mgr5;

CacheManager.newInstance();不会判断 singleton是否为null,直接使用默认的ehcache-failsafe.xml,新建一个CacheManager对象,所以mgr1 != mgr 6.

通过源码可以发现,它们的调用关系如下:

4.使用构造函数创建CacheManager对象

CacheManager m1 = new CacheManager();
System.out.println(m1.getName()); CacheManager m2 = new CacheManager("src/conf/ehcache.xml");
System.out.println(m2.getName()); System.out.println(m1 == m2);// false // CacheManager m3 = new CacheManager();
// CacheManager m4 = new CacheManager("src/conf/ehcache.xml");

可以看出new出来的对象都是不同实例的,也就是说Ehcache框架支持多个CacheManager对象的情况。特别注意:

同一个缓存配置文件,只能new一个CacheManager对象。如果打开注释代码中的m3 和 m4会报错:

Another CacheManager with same name 'atyManager' already exists in the same VM .Please provide unique names for each CacheManager

Ehcache CacheManager的更多相关文章

  1. springboot整合cache报错org.springframework.cache.ehcache.EhCacheCacheManager cannot be cast to net.sf.ehcache.CacheManager

    原来的代码 private static CacheManager cacheManager = SpringContextHolder.getBean("cacheManager" ...

  2. EhCache WebCache 与 SpringMVC集成时 CacheManager冲突的问题

    转自:点击打开链接 http://www.cnblogs.com/daxin/p/3560989.html EhCache WebCache 与 SpringMVC集成时 CacheManager冲突 ...

  3. EHCache 实现通用类 CacheManager

    package com.zhubaje.api.workflow.ehcache; import java.io.Serializable; import java.util.ArrayList; i ...

  4. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  5. EhCache的配置

    JPA和Hibernate的二级缓存都是这样做的 代码目录: 这是基础的jar包,如果少的话,再去maven下载 <!-- Spring --> <dependency> &l ...

  6. Ehcache Demo

    转自: https://my.oschina.net/zb0423/blog/60957http://www.cnblogs.com/fsjin/articles/3521261.html Ehcac ...

  7. Spring整合Ehcache管理缓存(转)

    目录 前言 概述 安装 Ehcache的使用 HelloWorld范例 Ehcache基本操作 创建CacheManager 添加缓存 删除缓存 实现基本缓存操作 缓存配置 xml方式 API方式 S ...

  8. 使用ehcache持久化数据到磁盘 并且在应用服务器重启后不丢失数据

    使用ehcache时如何持久化数据到磁盘,并且在应用服务器重启后不丢失数据1.如何持久化到磁盘使用cache.flush(),每次写入到cache后调用cache.flush() ,这样ehcache ...

  9. 【Java】:ehcache

    ehcache是一个纯Java进程内缓存框架,是hibernate默认的Cacheprovider.(出自百度百科). 1. 快速2. 简单3. 多种缓存策略4. 缓存数据有两级:内存和磁盘,因此无需 ...

随机推荐

  1. xmpp 常见错误 一

    #pragma mark - 密码错误,身份验证失败 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement ...

  2. 【bzoj4197】[Noi2015]寿司晚宴 分解质因数+状态压缩dp

    题目描述 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同的寿司,编号 ...

  3. 「SDOI2010」古代猪文(bzoj1951)

    题目写了一大堆背景. 一句话题意就是求 $q^{\sum_{d|n}C_{n}^{d}} \mod 999911659$. 因为$n$是质数,只有当$q$是$n$的倍数时(此题数据范围原因,最多$q= ...

  4. noj 2069 赵信的往事 [yy题 无限gcd]

    njczy2010 2069 Accepted 31MS   224K 1351Byte G++ 2014-11-13 13:32:56.0 坑爹的无限gcd,,,尼玛想好久,原来要x对y算一次,y再 ...

  5. ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集

    Untrusted Patrol Time Limit: 3 Seconds                                     Memory Limit: 65536 KB    ...

  6. MYSQL 中GROUP BY

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  7. StoryBoard中,TableView位置总是在顶部出现空白的解决

      重设TableView的 contentInset 属性可解决. 
_tableView.contentInset = UIEdgeInsetsMake( -30, 0, 0, 0);


  8. hdu1853/ hdu 3488 有向图,取k个圈覆盖所有点一次//费用流

    哎╮(╯▽╰)╭,这是费用流基础题型,拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. 弱菜还需努力! #include& ...

  9. android studio settings

    安装 Android Studio下载地址   http://www.android-studio.org/ 1.配置JDK 2.安装 Android Studio (带SDK) 3.配置 一.Set ...

  10. STL优先队列模板

    1. 优先队列 用途:按照某一个关键字对插入元素或删除元素后的数据集进行自动排序 复杂度: logN 2. 数据声明 (1)头文件:#include<queue> (2)声明:  prio ...