本文主要介绍 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 | 大小单位 |
标签:Ehcache,缓存,--,配置,Ehcache3,build,cacheManager,class,myCache From: https://www.cnblogs.com/wuyongyin/p/18001313