我爱新博文

Ehcache CacheManager

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 

posted @ 2016-10-16 14:40  52newblog  阅读(5677)  评论(0编辑  收藏  举报
www.52newblog.com 我爱新博文