zoukankan      html  css  js  c++  java
  • 1.Ehcache(01)——简介、基本操作

    转自: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里面的。关于配置文件如何配置的问题将在后续的文章中再做一个详细的讲解,这里先来简单看一个配置文件。

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

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

     1 package com.asm;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.URL;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 import net.sf.ehcache.config.CacheConfiguration;
    11 import net.sf.ehcache.config.Configuration;
    12 
    13 public class Test {
    14 
    15     @org.junit.Test
    16     public void test() {
    17         CacheManager cacheManager = new CacheManager();
    18         // 输出当前cacheManager正在使用的配置对应的Xml格式文本
    19         System.out.println(cacheManager.getActiveConfigurationText());
    20     }
    21 
    22 
    23 }

    2.以Configuration作为参数

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

     1 package com.asm;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.URL;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 import net.sf.ehcache.config.CacheConfiguration;
    11 import net.sf.ehcache.config.Configuration;
    12 
    13 public class Test {
    14 
    15 
    16 
    17     @org.junit.Test
    18     public void test1() {
    19         // 新建一个缓存的配置信息
    20         CacheConfiguration cacheConfiguration = new CacheConfiguration("test",
    21                 100000);
    22         CacheManager cacheManager = new CacheManager();
    23         // 通过缓存配置创建缓存
    24         Cache cache = new Cache(cacheConfiguration);
    25         cacheManager.addCache(cache);
    26         cache.put(new Element("test", "test"));
    27         System.out.println(cache.get("test").getObjectValue());
    28 
    29     }
    30 
    31 
    32 }

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

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

     1 package com.asm;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.URL;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 import net.sf.ehcache.config.CacheConfiguration;
    11 import net.sf.ehcache.config.Configuration;
    12 
    13 public class Test {
    14 
    15 
    16 
    17     @org.junit.Test
    18     public void test2() throws IOException {
    19 
    20         InputStream is = this.getClass().getClassLoader()
    21                 .getResourceAsStream("/ehcache.xml");
    22         CacheManager cacheManager = new CacheManager(is);
    23         System.out.println(cacheManager.getActiveConfigurationText());
    24     }
    25 
    26 
    27 
    28 }

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

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

     1 package com.asm;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.URL;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 import net.sf.ehcache.config.CacheConfiguration;
    11 import net.sf.ehcache.config.Configuration;
    12 
    13 public class Test {
    14 
    15 
    16     @org.junit.Test
    17     public void test4() {
    18         URL url = this.getClass().getResource("/ehcache.xml");
    19         System.out.println(url);
    20         CacheManager cacheManager = new CacheManager(url);
    21         System.out.println(cacheManager.getActiveConfigurationText());
    22     }
    23 }

    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。

     1 @org.junit.Test
     2     public void test5(){
     3                   //以默认配置创建一个CacheManager单例
     4                   CacheManager cacheManager = CacheManager.create();
     5                  
     6                   //以config对应的配置创建CacheManager单例
     7                   Configuration config = ...;//以某种方式获取的Configuration对象
     8                   cacheManager = CacheManager.create(config);
     9                  
    10                   //以configurationFileName对应的xml文件定义的配置创建CacheManager单例
    11                   String configurationFileName = ...;//xml配置文件对应的文件名称,包含路径
    12                   cacheManager = CacheManager.create(configurationFileName);
    13                  
    14                   //以is对应的配置信息创建CacheManager单例
    15                   InputStream is = ...; //以某种方式获取到的Xml配置信息对应的输入流
    16                   cacheManager = CacheManager.create(is);
    17                  
    18                   //以URL对应的配置信息创建CacheManager单例
    19                   URL url = ...;  //以某种方式获取到的Xml配置信息对应的URL
    20                   cacheManager = CacheManager.create(url);
    21     }

    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是线程安全的。

     1 package com.asm;
     2 
     3 import java.io.IOException;
     4 import java.io.InputStream;
     5 import java.net.URL;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 import net.sf.ehcache.config.CacheConfiguration;
    11 import net.sf.ehcache.config.Configuration;
    12 
    13 public class Test {
    14 
    15 
    16     @org.junit.Test
    17     public void cache(){
    18         
    19         //内存中保存的Element的最大数量  
    20         int maxEntriesLocalHeap = 10000;
    21         
    22         CacheConfiguration cacheConfiguration = new CacheConfiguration("test",maxEntriesLocalHeap);
    23         cacheConfiguration.overflowToDisk(false);
    24         Cache cache = new Cache(cacheConfiguration);
    25         
    26         CacheManager cacheManager = CacheManager.create();
    27         cacheManager.addCache(cache);
    28           cache.put(new Element("key", "value"));  
    29         System.out.println(cache.get("key"));
    30 
    31     }
    32     
    33 
    34 }

    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对象。

     1 package com.asm;
     2 
     3 import org.junit.After;
     4 import org.junit.Before;
     5 import org.junit.Test;
     6 
     7 import net.sf.ehcache.Cache;
     8 import net.sf.ehcache.CacheManager;
     9 import net.sf.ehcache.Element;
    10 
    11 public class CacheCRUDTest {
    12 
    13     private CacheManager cacheManager;
    14 
    15     @Before
    16     public void before() {
    17         cacheManager = CacheManager.create();
    18 //        System.out.println(cacheManager);
    19         cacheManager.addCache("cache");
    20 //        System.out.println(cacheManager);
    21     }
    22 
    23     @After
    24     public void after() {
    25         cacheManager.shutdown();
    26     }
    27 
    28     @Test
    29     public void test() {
    30 
    31         Cache cache = cacheManager.getCache("cache");
    32         Element ele = new Element("key", "value");
    33         // 把ele放入缓存cache中
    34         cache.put(ele);
    35      36     }
    37 }

     

    (2)获取元素

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

    Java代码  

      

     1    /**
     2     * 从Cache中读取元素
     3     */
     4    @Test
     5    public void read() {
     6       Cache cache = cacheManager.getCache("cache");
     7       //通过key来获取缓存中对应的元素
     8       Element ele = cache.get("key");
     9       System.out.println(ele);
    10       if (ele != null) {//当缓存的元素存在时获取缓存的值
    11          System.out.println(ele.getObjectValue());
    12       }
    13    }

    (3)更新元素

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

    Java代码  
     1    /**
     2     * 更新元素
     3     */
     4    @Test
     5    public void update() {
     6       Cache cache = cacheManager.getCache("cache");
     7       cache.put(new Element("key", "value1"));
     8       System.out.println(cache.get("key"));
     9       //当添加元素的时候,如果缓存中已经存在相同key的元素则会将后者覆盖前者
    10       cache.put(new Element("key", "value2"));
    11       System.out.println(cache.get("key"));
    12    }

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

    Java代码  
     1    /**
     2     * 更新元素
     3     */
     4    @Test
     5    public void update() {
     6       Cache cache = cacheManager.getCache("cache");
     7       cache.put(new Element("key", "value1"));
     8       System.out.println(cache.get("key"));
     9       //当添加元素的时候,如果缓存中已经存在相同key的元素则会将后者覆盖前者
    10       cache.put(new Element("key", "value2"));
    11       System.out.println(cache.get("key"));
    12    }

    (4)删除元素

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

    Java代码  
     1 /** 
     2  * 根据key来移除一个元素 
     3  */  
     4 @Test  
     5 public void delete() {  
     6    Cache cache = cacheManager.getCache("cache");  
     7    //根据key来移除一个元素  
     8    cache.remove("key");  
     9    System.out.println(cache.get("key"));  
    10 }  

    ehcache.2.8.1.jar

  • 相关阅读:
    hdu 2141 二分搜索
    什么是你的核心竞争力之一?
    Ubuntu系统开机后显示器提示“不能显示此视频模式,请将电脑显示输入设置为1920×1080@60Hz”
    [置顶] java高级工程师struts的内部运行机制详解
    一步步理解Linux进程(3)内核中进程的实现
    2013年4月19日佳都新太笔试题+解答
    Android 通过Service单独进程模仿离线推送 Server Push
    缓存研究
    解决MDK4以上版本没法对STM32软件仿真
    windows调试器之Visual C++
  • 原文地址:https://www.cnblogs.com/sharpest/p/5613959.html
Copyright © 2011-2022 走看看