ehcache(1)---简介

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/83187770

1、什么是ehcache?

ehcache是用来管理缓存的一个工具,其缓存的数据可以放在内存里面,也可以放在硬盘上。

ehcache的核心是cacheManager,一切的ehcache的应用都是从cacheManager开始的。

2、ehcache、cacheManager和cache三者之间的关系?

ehcache的核心是cacheManager,cacheManager是用来管理cache(缓存)的。

一个应用下可以有多个cacheManager,而一个cacheManager下又可以有多个cache

cache内部保存的是一个的element,一个element中保存的是一个key和value的配对。

3、什么是cacheManager?

cacheManager是ehcache的核心,它的主要职责是对cache的创建、移除和访问。

只有cacheManager里面的cache才能实现缓存数据的功能。

4、cacheManager的创建

我们可以直接通过其构造方法来构建一个CacheManager,也可以通过CacheManager提供的静态方法来构建CacheManager

4.1、构造方法构建

使用构造方法构建cacheManager时每次都会产生一个新的CacheManager,并且会以该CacheManager对应的name作为key保存该cacheManager.

构建cacheManager时,如果已经存在name相同且正在使用的cacheManager,则会抛出异常。

此外,当多个cacheManager对应的storePath相同时,则他们存放在磁盘上包含缓存信息的文件将会相互覆盖。

4.1.1、使用默认配置构建cacheManager

使用cacheManager的无参构造方法构造cacheManager时就是使用的默认配置。这种情况下最终还是会寻找默认的胚子文件进行配置。

ehcache首先会到类根路径下寻找一个叫ehcache.xml的配置文件来配置cacheManager,如果没有找到该文件,则会加载cacheManager的默认配置ehcache-failsafe.xml文件,这个文件在ehcache.jar里面。

示例如下:

步骤1、新建web工程,名为ehcacheDemo,然后导入jar包,如下:

步骤2、在src下新建源文件config,然后在该文件夹下新建ehcache配置文件ehcache.xml

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

步骤3、在src下新建包com.cn.test,然后在该包下新建类DefaultTest,代码如下:

package com.cn.test;

import net.sf.ehcache.CacheManager;
import org.junit.Test;
/**
 * 无参构造cacheManager的方式1:使用默认配置
 * */
public class DefaultTest {
	@Test
	public void defTest(){
		CacheManager cacheManager=new CacheManager();
		//输出当前cacheManager正在使用的配置对应的xml格式文本
		System.out.println(cacheManager.getActiveConfigurationText());
		
	}
}

执行结果如下:

4.1.2、以configuration作为参数构建cacheManager

package com.cn.test;

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;
import net.sf.ehcache.config.MemoryUnit;

import org.junit.Test;

/**
 * 以Configuration作为参数来构建cacheManager
 * */
public class ConfigurationTest {
	@Test
	public void test(){
		//1.新建一个cacheManager的配置信息
		Configuration configuration=new Configuration();
		//2.新建一个缓存的配置信息
		CacheConfiguration cacheConfiguration=new CacheConfiguration().name("test");
		//3.指定当前缓存的最大堆内存值为100M
		cacheConfiguration.maxBytesLocalHeap(100, MemoryUnit.MEGABYTES);
		//4.添加一个cache
		configuration.addCache(cacheConfiguration);
		configuration.dynamicConfig(false);  //不允许动态修改配置信息
		//5.通过配置文件创建cacheManager
		CacheManager cacheManager=new CacheManager(configuration);
		//6.获取cacheManager下的cache
		Cache cache=cacheManager.getCache("test");
		//7.往cache里面添加element元素
		cache.put(new Element("key1","value1"));
		System.out.println(cache.get("key1").getObjectValue());
	}
}

执行结果如下:

4.1.3、以xml格式的配置对应的inputStream作为参数

package com.cn.test;

import java.io.IOException;
import java.io.InputStream;

import net.sf.ehcache.CacheManager;

import org.junit.Test;

/**
 * 3、以inputStream作为参数创建cacheManager
 * */
public class InputStreamTest {
	@Test
	public void test() throws IOException{
		InputStream is=this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
		CacheManager cacheManager=new CacheManager(is);
		//关闭流
		is.close();
		System.out.println(cacheManager.getActiveConfigurationText());
	}
}

执行结果如下:

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

package com.cn.test;

import net.sf.ehcache.CacheManager;

import org.junit.Test;

/**
 * 以xml格式配置文件对应的路径作为参数来创建cacheManager
 * */
public class PathTest {
	@Test
	public void test(){
		//这个文件路径可以是相对路径,也可以是绝对路径,这里是绝对路径
		CacheManager cacheManager=new CacheManager("G:\\workPlace\\taotao2\\ehcacheDemo\\config\\ehcache.xml");
		System.out.println(cacheManager.getActiveConfigurationText());
	}
}

执行结果如下:

4.1.5、以xml格式的配置对应的url作为参数

package com.cn.test;

import java.net.URL;

import net.sf.ehcache.CacheManager;

import org.junit.Test;

/**
 * 以xml格式的配置对应的url作为参数创建cacheManager
 * */
public class UrlTest {
	@Test
	public void test(){
		URL url=this.getClass().getResource("ehcache.xml");
		CacheManager cacheManager=new CacheManager(url);
		System.out.println(cacheManager.getActiveConfigurationText());
	}
}

执行结果如下:

4.2、静态方法构建cacheManager

静态方法构建cacheManager,主要分为2类,一类是通过create()方法及其重载方法构建的,一类是通过newInstance()方法及其重载方法构建的。

两者的区别在于:create()方法构建的都是单例,而newInstance()方法构建的可能是单例,也可能是多例。

在cacheManager内部持有一个CacheManager类型的singleton对象,每次我们调用create()方法及其重载方法时,ehcache都会判断当前的singleton对象是否非空,如果非空则直接返回,否则以相应配置构建一个cacheManager对象赋给singleton并进行返回。

在调用newInstance()方法及其重载方法构建cacheManager时,ehcahe首先会判断我们之前是否创建过且还存在同样名称的cacheManager对象,如果有则直接返回cacheManager对象,否则将新建一个cacheManager进行返回。

newInstance()方法和直接调用构造方法构造cacheManager对象的区别在于:

newInstance()方法创建时如有同名的存在,会直接返回先前的,而不会抛出异常。此外cacheManager内部还定义了一个getInstance()静态方法,调用它时相当于调用了不带参数的create()方法。

4.2.1、create()方法创建cacheManager

在cacheManager内部一共定义了5个create()方法,分别对应于cacheManager的5个newInstance()方法

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

package com.cn.test2;

import net.sf.ehcache.CacheManager;
import org.junit.Test;

/**
 * 静态方法创建cacheManager方式1:create()方法
 * */
public class CreateCacheManager {
	@Test
	public void test(){
		CacheManager cacheManager=CacheManager.create();
		System.out.println(cacheManager.getActiveConfigurationText());
	}
}

执行结果如下:

configuration配置文件创建、以inputStream作为参数创建、以ehcache.xml配置文件的路径作为参数创建、以ehcache.xml配置文件对应的url作为参数进行创建的方式此处省略

 //以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);

4.2.2、newInstance()方法创建cacheManager

调用newInstance()方法时,ehcache会查看cacheManager内部是否保存有曾经新建的且同名的cacheManager,

如果有则返回该对象,否则构建一个新的cacheManager对象进行返回,所以newInstance()方法并不一定会产生一个新的对象。

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

猜你喜欢

转载自blog.csdn.net/sinat_23490433/article/details/83187770
今日推荐