Ehcache 介紹(3)--Ehcache3 基本使用

且行且码發表於2024-03-10

本文主要介紹 Ehacche3 的基本使用,文中所使用到的軟體版本:Java 1.8.0_341、Ehcache 3.10.8。

1、引入依賴

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.10.8</version>
    <exclusions>
        <exclusion>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2、快取配置和建立

2.1、XML 方式配置快取

A、在 src/main/resources 目錄下新建 ehcache3.xml:

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

        xsi:schemaLocation="http://www.ehcache.org/v3
        http://www.ehcache.org/schema/ehcache-core-3.10.xsd
        http://www.ehcache.org/v3/jsr107
        http://www.ehcache.org/schema/ehcache-107-ext-3.10.xsd">

    <persistence directory="D:\temp"/>

    <cache alias="myCache">
        <key-type>java.lang.Integer</key-type>
        <value-type>java.lang.String</value-type>
        <expiry>
            <tti unit="minutes">5</tti>
        </expiry>
        <resources>
            <heap unit="MB">10</heap>
            <offheap unit="MB">50</offheap>
            <disk persistent="true" unit="MB">500</disk>
        </resources>
    </cache>
</config>

B、載入配置

@Test
public void test01() {
    URL url = this.getClass().getClassLoader().getResource("ehcache3.xml");
    XmlConfiguration xmlConfiguration = new XmlConfiguration(url);
    CacheManager cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
    cacheManager.init();
    Cache<Integer, String> myCache = cacheManager.getCache("myCache", Integer.class, String.class);
    myCache.put(1, "aaa");
    log.info(myCache.get(1));
    cacheManager.close();
}

2.2、API 方式配置快取

A、方式一

@Test
public void test02() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .with(CacheManagerBuilder.persistence("d:/temp"))
            .build(true);
    ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
            .heap(1, MemoryUnit.MB)
            .disk(10, MemoryUnit.MB, true)
            .build();
    CacheConfiguration<Integer, String> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Integer.class, String.class, resourcePools)
            .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(100)))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(500)))
            .build();
    Cache<Integer, String> myCache = cacheManager.createCache("myCache", cacheConfiguration);
    myCache.put(1, "aaa");

    log.info(myCache.get(1));
    cacheManager.close();
}

B、方式二

@Test
public void test03() {
    ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
            .heap(1, MemoryUnit.MB)
            .disk(10, MemoryUnit.MB, true)
            .build();
    CacheConfiguration<Integer, String> cacheConfiguration = CacheConfigurationBuilder
            .newCacheConfigurationBuilder(Integer.class, String.class, resourcePools)
            .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(100)))
            .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(500)))
            .build();

    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .with(CacheManagerBuilder.persistence("d:/temp"))
            .withCache("myCache", cacheConfiguration)
            .build(true);
    Cache<Integer, String> myCache = cacheManager.getCache("myCache", Integer.class, String.class);
    //myCache.put(1, "aaa");
    log.info(myCache.get(1));
    cacheManager.close();
}

3、Ehcache3 配置引數說明

引數 說明
persistence 直接配置
directory 資料儲存目錄
cache 快取配置
alias 快取名稱
key-type 快取 key 的型別
value-type 快取 value 的型別
expiry 快取過期配置
tti 快取中條目的最大空閒時間
ttl 快取中條目的最大存活時間
resources 資源配置
heap 堆記憶體配置
unit 大小單位
offheap 堆外存配置
unit 大小單位
disk 磁碟配置
persistence 是否持久化
unit 大小單位

相關文章