欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java進(jìn)程內(nèi)緩存框架EhCache詳解

 更新時(shí)間:2021年12月13日 10:07:22   作者:myseries  
這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一:目錄

EhCache 簡(jiǎn)介

Hello World 示例

Spring 整合

二: 簡(jiǎn)介

2.1、基本介紹

EhCache 是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)CacheProvider。Ehcache是一種廣泛使用的開源Java分布式緩存。主要面向通用緩存,Java EE和輕量級(jí)容器。它具有內(nèi)存和磁盤存儲(chǔ),緩存加載器,緩存擴(kuò)展,緩存異常處理程序,一個(gè)gzip緩存servlet過濾器,支持REST和SOAP api等特點(diǎn)。

Spring 提供了對(duì)緩存功能的抽象:即允許綁定不同的緩存解決方案(如Ehcache),但本身不直接提供緩存功能的實(shí)現(xiàn)。它支持注解方式使用緩存,非常方便。

2.2、主要的特性

  1. 快速
  2. 簡(jiǎn)單
  3. 多種緩存策略
  4. 緩存數(shù)據(jù)有兩級(jí):內(nèi)存和磁盤,因此無需擔(dān)心容量問題
  5. 緩存數(shù)據(jù)會(huì)在虛擬機(jī)重啟的過程中寫入磁盤
  6. 可以通過RMI、可插入API等方式進(jìn)行分布式緩存
  7. 具有緩存和緩存管理器的偵聽接口
  8. 支持多緩存管理器實(shí)例,以及一個(gè)實(shí)例的多個(gè)緩存區(qū)域
  9. 提供Hibernate的緩存實(shí)現(xiàn)

2.3、 集成

可以單獨(dú)使用,一般在第三方庫(kù)中被用到的比較多(如mybatis、shiro等)ehcache 對(duì)分布式支持不夠好,多個(gè)節(jié)點(diǎn)不能同步,通常和redis一塊使用

2.4、 ehcache 和 redis 比較

ehcache直接在jvm虛擬機(jī)中緩存,速度快,效率高;但是緩存共享麻煩,集群分布式應(yīng)用不方便。

redis是通過socket訪問到緩存服務(wù),效率比Ehcache低,比數(shù)據(jù)庫(kù)要快很多,處理集群和分布式緩存方便,有成熟的方案。如果是單個(gè)應(yīng)用或者對(duì)緩存訪問要求很高的應(yīng)用,用ehcache。如果是大型系統(tǒng),存在緩存共享、分布式部署、緩存內(nèi)容很大的,建議用redis。

ehcache也有緩存共享方案,不過是通過RMI或者Jgroup多播方式進(jìn)行廣播緩存通知更新,緩存共享復(fù)雜,維護(hù)不方便;簡(jiǎn)單的共享可以,但是涉及到緩存恢復(fù),大數(shù)據(jù)緩存,則不合適。

三:事例

3.1、在pom.xml中引入依賴

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.2</version>
</dependency>

3.2、在src/main/resources/創(chuàng)建一個(gè)配置文件 ehcache.xml

默認(rèn)情況下Ehcache會(huì)自動(dòng)加載classpath根目錄下名為ehcache.xml文件,也可以將該文件放到其他地方在使用時(shí)指定文件的位置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

  <!-- 磁盤緩存位置 -->
  <diskStore path="java.io.tmpdir/ehcache"/>

  <!-- 默認(rèn)緩存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>

  <!-- helloworld緩存 -->
  <cache name="HelloWorldCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="5"
         timeToLiveSeconds="5"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>

3.3、測(cè)試類

import entity.Dog;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
 
public class CacheTest {
    public static void main(String[] args) {
        // 1. 創(chuàng)建緩存管理器
        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");
         
        // 2. 獲取緩存對(duì)象
        Cache cache = cacheManager.getCache("HelloWorldCache");
         
        // 3. 創(chuàng)建元素
        Element element = new Element("key1", "value1");
         
        // 4. 將元素添加到緩存
        cache.put(element);
         
        // 5. 獲取緩存
        Element value = cache.get("key1");
        System.out.println("value: " + value);
        System.out.println(value.getObjectValue());
         
        // 6. 刪除元素
        cache.remove("key1");
         
        Dog dog = new Dog("xiaohei", "black", 2);
        Element element2 = new Element("dog", dog);
        cache.put(element2);
        Element value2 = cache.get("dog");
        System.out.println("value2: "  + value2);
        Dog dog2 = (Dog) value2.getObjectValue();
        System.out.println(dog2);
         
        System.out.println(cache.getSize());
         
        // 7. 刷新緩存
        cache.flush();
         
        // 8. 關(guān)閉緩存管理器
        cacheManager.shutdown();
 
    }
}
public class Dog {
    private String name;
    private String color;
    private int age;
     
    public Dog() {
    }
     
    public Dog(String name, String color, int age) {
        super();
        this.name = name;
        this.color = color;
        this.age = age;
    }
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
     
    @Override
    public String toString() {
        return "Dog [name=" + name + ", color=" + color + ", age=" + age + "]";
    }
}

3.4、緩存配置

一:xml配置方式:

diskStore : ehcache支持內(nèi)存和磁盤兩種存儲(chǔ)

path :指定磁盤存儲(chǔ)的位置
defaultCache : 默認(rèn)的緩存

maxEntriesLocalHeap=“10000”
eternal=“false”
timeToIdleSeconds=“120”
timeToLiveSeconds=“120”
maxEntriesLocalDisk=“10000000”
diskExpiryThreadIntervalSeconds=“120”
memoryStoreEvictionPolicy=“LRU”
cache :自定的緩存,當(dāng)自定的配置不滿足實(shí)際情況時(shí)可以通過自定義(可以包含多個(gè)cache節(jié)點(diǎn))

name : 緩存的名稱,可以通過指定名稱獲取指定的某個(gè)Cache對(duì)象

maxElementsInMemory :內(nèi)存中允許存儲(chǔ)的最大的元素個(gè)數(shù),0代表無限個(gè)

clearOnFlush:內(nèi)存數(shù)量最大時(shí)是否清除。

eternal :設(shè)置緩存中對(duì)象是否為永久的,如果是,超時(shí)設(shè)置將被忽略,對(duì)象從不過期。根據(jù)存儲(chǔ)數(shù)據(jù)的不同,例如一些靜態(tài)不變的數(shù)據(jù)如省市區(qū)等可以設(shè)置為永不過時(shí)

timeToIdleSeconds : 設(shè)置對(duì)象在失效前的允許閑置時(shí)間(單位:秒)。僅當(dāng)eternal=false對(duì)象不是永久有效時(shí)使用,可選屬性,默認(rèn)值是0,也就是可閑置時(shí)間無窮大。

timeToLiveSeconds :緩存數(shù)據(jù)的生存時(shí)間(TTL),也就是一個(gè)元素從構(gòu)建到消亡的最大時(shí)間間隔值,這只能在元素不是永久駐留時(shí)有效,如果該值是0就意味著元素可以停頓無窮長(zhǎng)的時(shí)間。

overflowToDisk :內(nèi)存不足時(shí),是否啟用磁盤緩存。

maxEntriesLocalDisk:當(dāng)內(nèi)存中對(duì)象數(shù)量達(dá)到maxElementsInMemory時(shí),Ehcache將會(huì)對(duì)象寫到磁盤中。

maxElementsOnDisk:硬盤最大緩存?zhèn)€數(shù)。

diskSpoolBufferSizeMB:這個(gè)參數(shù)設(shè)置DiskStore(磁盤緩存)的緩存區(qū)大小。默認(rèn)是30MB。每個(gè)Cache都應(yīng)該有自己的一個(gè)緩沖區(qū)。

diskPersistent:是否在VM重啟時(shí)存儲(chǔ)硬盤的緩存數(shù)據(jù)。默認(rèn)值是false。

diskExpiryThreadIntervalSeconds:磁盤失效線程運(yùn)行時(shí)間間隔,默認(rèn)是120秒。

二:編程方式配置

Cache cache = manager.getCache("mycache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setmaxEntriesLocalHeap(10000);
config.setmaxEntriesLocalDisk(1000000);

3.5、Ehcache API

CacheManager:Cache的容器對(duì)象,并管理著(添加或刪除)Cache的生命周期。

// 可以自己創(chuàng)建一個(gè)Cache對(duì)象添加到CacheManager中
public void addCache(Cache cache);
public synchronized void removeCache(String cacheName);

Cache: 一個(gè)Cache可以包含多個(gè)Element,并被CacheManager管理。它實(shí)現(xiàn)了對(duì)緩存的邏輯行為

Element:需要緩存的元素,它維護(hù)著一個(gè)鍵值對(duì), 元素也可以設(shè)置有效期,0代表無限制

獲取CacheManager的方式:

可以通過create()或者newInstance()方法或重載方法來創(chuàng)建獲取CacheManager的方式:

public static CacheManager create();
public static CacheManager create(String configurationFileName);
public static CacheManager create(InputStream inputStream);
public static CacheManager create(URL configurationFileURL);
 
public static CacheManager newInstance();

Ehcache的CacheManager構(gòu)造函數(shù)或工廠方法被調(diào)用時(shí),會(huì)默認(rèn)加載classpath下名為ehcache.xml的配置文件。

如果加載失敗,會(huì)加載Ehcache jar包中的ehcache-failsafe.xml文件,這個(gè)文件中含有簡(jiǎn)單的默認(rèn)配置?! ?/p>

// CacheManager.create() == CacheManager.create("./src/main/resources/ehcache.xml")
// 使用Ehcache默認(rèn)配置新建一個(gè)CacheManager實(shí)例
CacheManager cacheManager = CacheManager.create();
cacheManager = CacheManager.newInstance();
 
cacheManager = CacheManager.newInstance("./src/main/resources/ehcache.xml");
 
InputStream inputStream = new FileInputStream(new File("./src/main/resources/ehcache.xml"));
cacheManager = CacheManager.newInstance(inputStream);
 
String[] cacheNames = cacheManager.getCacheNames();  // [HelloWorldCache]

四:Spring整合

項(xiàng)目結(jié)構(gòu):

4.1、pom.xml 引入spring和ehcache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.gdut.yh</groupId>
  <artifactId>EhcacheSpringTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>4.10</junit.version>
    <spring.version>4.2.3.RELEASE</spring.version>
  </properties>
  
  <dependencies>
      <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
    </dependency>
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-test</artifactId>
         <version>${spring.version}</version>
     </dependency>
     
     <!-- springframework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.10.3</version>
    </dependency>
  </dependencies>
</project>

4.2、在src/main/resources添加ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

  <!-- 磁盤緩存位置 -->
  <diskStore path="java.io.tmpdir/ehcache" />

  <!-- 默認(rèn)緩存 -->
  <defaultCache
          maxEntriesLocalHeap="10000"
          eternal="false"
          timeToIdleSeconds="120"
          timeToLiveSeconds="120"
          maxEntriesLocalDisk="10000000"
          diskExpiryThreadIntervalSeconds="120"
          memoryStoreEvictionPolicy="LRU">
    <persistence strategy="localTempSwap"/>
  </defaultCache>

  <!-- helloworld緩存 -->
  <cache name="HelloWorldCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="5"
         timeToLiveSeconds="5"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>

  <cache name="UserCache"
         maxElementsInMemory="1000"
         eternal="false"
         timeToIdleSeconds="1800"
         timeToLiveSeconds="1800"
         overflowToDisk="false"
         memoryStoreEvictionPolicy="LRU"/>
</ehcache>

4.3、在src/main/resources/conf/spring中配置spring-base.xml和spring-ehcache.xml

spring-base.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="com.gdut.*"/>
</beans>

spring-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

  <description>ehcache緩存配置管理文件</description>

  <!-- 啟用緩存注解開關(guān) -->
  <cache:annotation-driven cache-manager="cacheManager"/>

  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
  </bean>

  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
  </bean>

</beans>

4.4、在src/main/java/com.mengdee.manager.service/下 創(chuàng)建EhcacheService和EhcacheServiceImpl

EhcacheService.java

public interface EhcacheService {
 
    // 測(cè)試失效情況,有效期為5秒
    public String getTimestamp(String param);
 
    public String getDataFromDB(String key);
 
    public void removeDataAtDB(String key);
 
    public String refreshData(String key);
 
 
    public User findById(String userId);
 
    public boolean isReserved(String userId);
 
    public void removeUser(String userId);
 
    public void removeAllUser();
}

EhcacheServiceImpl.java

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
@Service
public class EhcacheServiceImpl implements EhcacheService{
 
    // value的值和ehcache.xml中的配置保持一致
    @Cacheable(value="HelloWorldCache", key="#param")
    public String getTimestamp(String param) {
        Long timestamp = System.currentTimeMillis();
        return timestamp.toString();
    }
 
    @Cacheable(value="HelloWorldCache", key="#key")
    public String getDataFromDB(String key) {
        System.out.println("從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)...");
        return key + ":" + String.valueOf(Math.round(Math.random()*1000000));
    }
 
    @CacheEvict(value="HelloWorldCache", key="#key")
    public void removeDataAtDB(String key) {
        System.out.println("從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù)");
    }
 
    @CachePut(value="HelloWorldCache", key="#key")
    public String refreshData(String key) {
        System.out.println("模擬從數(shù)據(jù)庫(kù)中加載數(shù)據(jù)");
        return key + "::" + String.valueOf(Math.round(Math.random()*1000000));
    }
 
    // ------------------------------------------------------------------------
    @Cacheable(value="UserCache", key="'user:' + #userId")   
    public User findById(String userId) { 
        System.out.println("模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)");
        return new User(1, "mengdee");          
    } 
 
    @Cacheable(value="UserCache", condition="#userId.length()<12")   
    public boolean isReserved(String userId) {   
        System.out.println("UserCache:"+userId);   
        return false;   
    }
 
    //清除掉UserCache中某個(gè)指定key的緩存   
    @CacheEvict(value="UserCache",key="'user:' + #userId")   
    public void removeUser(String userId) {   
        System.out.println("UserCache remove:"+ userId);   
    }   
 
    //allEntries:true表示清除value中的全部緩存,默認(rèn)為false
    //清除掉UserCache中全部的緩存   
    @CacheEvict(value="UserCache", allEntries=true)   
    public void removeAllUser() {   
       System.out.println("UserCache delete all");   
    }
}

User .java

public class User {
    private int id;
    private String name;
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
     
    public User() {
    }
     
    public User(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
     
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }
}

#注解基本使用方法

Spring對(duì)緩存的支持類似于對(duì)事務(wù)的支持。

首先使用注解標(biāo)記方法,相當(dāng)于定義了切點(diǎn),然后使用Aop技術(shù)在這個(gè)方法的調(diào)用前、調(diào)用后獲取方法的入?yún)⒑头祷刂?,進(jìn)而實(shí)現(xiàn)了緩存的邏輯。

@Cacheable

表明所修飾的方法是可以緩存的:當(dāng)?shù)谝淮握{(diào)用這個(gè)方法時(shí),它的結(jié)果會(huì)被緩存下來,在緩存的有效時(shí)間內(nèi),以后訪問這個(gè)方法都直接返回緩存結(jié)果,不再執(zhí)行方法中的代碼段。

這個(gè)注解可以用condition屬性來設(shè)置條件,如果不滿足條件,就不使用緩存能力,直接執(zhí)行方法。

可以使用key屬性來指定key的生成規(guī)則。

@Cacheable 支持如下幾個(gè)參數(shù):

  • value:緩存位置名稱,不能為空,如果使用EHCache,就是ehcache.xml中聲明的cache的name, 指明將值緩存到哪個(gè)Cache中
  • key:緩存的key,默認(rèn)為空,既表示使用方法的參數(shù)類型及參數(shù)值作為key,支持SpEL,如果要引用參數(shù)值使用井號(hào)加參數(shù)名,如:#userId,一般來說,我們的更新操作只需要刷新緩存中某一個(gè)值,所以定義緩存的key值的方式就很重要,最好是能夠唯一,因?yàn)檫@樣可以準(zhǔn)確的清除掉特定的緩存,而不會(huì)影響到其它緩存值 ,本例子中使用實(shí)體加冒號(hào)再加ID組合成鍵的名稱,如"user:1"、"order:223123"等
  • condition:觸發(fā)條件,只有滿足條件的情況才會(huì)加入緩存,默認(rèn)為空,既表示全部都加入緩存,支持SpEL
// 將緩存保存到名稱為UserCache中,鍵為"user:"字符串加上userId值,如 'user:1'
@Cacheable(value="UserCache", key="'user:' + #userId")
public User findById(String userId) {
    return (User) new User("1", "mengdee");
}
 
// 將緩存保存進(jìn)UserCache中,并當(dāng)參數(shù)userId的長(zhǎng)度小于12時(shí)才保存進(jìn)緩存,默認(rèn)使用參數(shù)值及類型作為緩存的key
// 保存緩存需要指定key,value, value的數(shù)據(jù)類型,不指定key默認(rèn)和參數(shù)名一樣如:"1"
@Cacheable(value="UserCache", condition="#userId.length() < 12")
public boolean isReserved(String userId) {
    System.out.println("UserCache:"+userId);
    return false;
}

@CachePut

與@Cacheable不同,@CachePut不僅會(huì)緩存方法的結(jié)果,還會(huì)執(zhí)行方法的代碼段。它支持的屬性和用法都與@Cacheable一致。

@CacheEvict

與@Cacheable功能相反,@CacheEvict表明所修飾的方法是用來刪除失效或無用的緩存數(shù)據(jù)。

@CacheEvict 支持如下幾個(gè)參數(shù):

  • value:緩存位置名稱,不能為空,同上
  • key:緩存的key,默認(rèn)為空,同上
  • condition:觸發(fā)條件,只有滿足條件的情況才會(huì)清除緩存,默認(rèn)為空,支持SpEL
  • allEntries:true表示清除value中的全部緩存,默認(rèn)為false
//清除掉UserCache中某個(gè)指定key的緩存   
@CacheEvict(value="UserCache",key="'user:' + #userId")   
public void removeUser(User user) {   
    System.out.println("UserCache"+user.getUserId());   
}   
 
//清除掉UserCache中全部的緩存   
@CacheEvict(value="UserCache", allEntries=true)   
public final void setReservedUsers(String[] reservedUsers) {   
   System.out.println("UserCache deleteall");   
}

5、測(cè)試

SpringTestCase.java  

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 
@ContextConfiguration(locations = {"classpath:spring-base.xml","classpath:spring-ehcache.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTestCase extends AbstractJUnit4SpringContextTests{
 
}

EhcacheServiceTest.java

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.gdut.ehcache.EhcacheService;
 
 
 
public class EhcacheServiceTest extends SpringTestCase{
 
    @Autowired //@Autowired 是通過 byType 的方式去注入的, 使用該注解,要求接口只能有一個(gè)實(shí)現(xiàn)類。
    private EhcacheService ehcacheService;
 
    // 有效時(shí)間是5秒,第一次和第二次獲取的值是一樣的,因第三次是5秒之后所以會(huì)獲取新的值
    @Test
    public void testTimestamp() throws InterruptedException{
        System.out.println("第一次調(diào)用:" + ehcacheService.getTimestamp("param"));
        Thread.sleep(2000);
        System.out.println("2秒之后調(diào)用:" + ehcacheService.getTimestamp("param"));
        Thread.sleep(4000);
        System.out.println("再過4秒之后調(diào)用:" + ehcacheService.getTimestamp("param"));
    }
 
    @Test
    public void testCache(){
        String key = "zhangsan";
        String value = ehcacheService.getDataFromDB(key); // 從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)...
        ehcacheService.getDataFromDB(key);  // 從緩存中獲取數(shù)據(jù),所以不執(zhí)行該方法體
        ehcacheService.removeDataAtDB(key); // 從數(shù)據(jù)庫(kù)中刪除數(shù)據(jù)
        ehcacheService.getDataFromDB(key);  // 從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù)...(緩存數(shù)據(jù)刪除了,所以要重新獲取,執(zhí)行方法體)
    }
 
    @Test
    public void testPut(){
        String key = "mengdee";
        ehcacheService.refreshData(key);  // 模擬從數(shù)據(jù)庫(kù)中加載數(shù)據(jù)
        String data = ehcacheService.getDataFromDB(key);
        System.out.println("data:" + data); // data:mengdee::103385
 
        ehcacheService.refreshData(key);  // 模擬從數(shù)據(jù)庫(kù)中加載數(shù)據(jù)
        String data2 = ehcacheService.getDataFromDB(key);
        System.out.println("data2:" + data2);   // data2:mengdee::180538   
    }
 
 
    @Test
    public void testFindById(){
        ehcacheService.findById("2"); // 模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)
        ehcacheService.findById("2");
    }
 
    @Test
    public void testIsReserved(){
        ehcacheService.isReserved("123");
        ehcacheService.isReserved("123");
    }
 
    @Test
    public void testRemoveUser(){
        // 線添加到緩存
        ehcacheService.findById("1");
 
        // 再刪除
        ehcacheService.removeUser("1");
 
        // 如果不存在會(huì)執(zhí)行方法體
        ehcacheService.findById("1");
    }
 
    @Test
    public void testRemoveAllUser(){
        ehcacheService.findById("1");
        ehcacheService.findById("2");
 
        ehcacheService.removeAllUser();
 
        ehcacheService.findById("1");
        ehcacheService.findById("2");
 
//      模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)
//      模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)
//      UserCache delete all
//      模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)
//      模擬從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù)
    }
 
}

以上所述是小編給大家介紹的Java進(jìn)程內(nèi)緩存框架EhCache詳解,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論