ehcache的官方说明:一个java的缓存框架。支持内存缓存和磁盘缓存
Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache because it's robust, proven, full-featured, and integrates with other popular libraries and frameworks. Ehcache scales from in-process caching, all the way to mixed in-process/out-of-process deployments with terabyte-sized caches.
支持的配置
代码语言:javascript复制heap:JVM byted-size 堆缓存,速度最快; 定义缓存的数量
off-heap:JVM 堆外内存,速度低于 heap,但是高于 disk; 堆外存储空间
disk:磁盘储存,速度最低,相对于 heap ,off-heap可以分配大量资源空间;磁盘存储空间
构建cache对象
代码语言:javascript复制package cn.beckbi.ex;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.Configuration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.spi.service.ServiceCreationConfiguration;
import org.ehcache.xml.XmlConfiguration;
/**
* @program: wj
* @description:
* @author: bikang
* @create: 2023-05-28 18:09
*/
public class EHCacheUtil {
static CacheManager manager=null;
public static synchronized CacheManager getManager() {
synchronized (EHCacheUtil.class) {
if (Objects.nonNull(manager)) {
return manager;
}
manager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence("/tmp/myData"))
.withCache("cache0",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(20)))
).withCache("cache1",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(20)))
).withCache("cache2",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(20)))
)
.build(true);
return manager;
}
}
public static void put(String cachename,String key,String value){
Cache<String, String> cacheObj = getManager().getCache(cachename, String.class, String.class);
cacheObj.put(key, value);
}
public static String get(String cachename,String key){
Cache<String, String> cacheObj = getManager().getCache(cachename, String.class, String.class);
return cacheObj.get(key);
}
public static void remove(String cachename,String key){
getManager().getCache(cachename, String.class, String.class).remove(key);
}
}
应用
代码语言:javascript复制public void testEcache() {
EHCacheUtil.put("cache0", "a", "1");
EHCacheUtil.put("cache1", "a", "2");
EHCacheUtil.put("cache2", "a", "3");
System.out.println("cache0:a=" EHCacheUtil.get("cache0", "a"));
System.out.println("cache1:a=" EHCacheUtil.get("cache1", "a"));
System.out.println("cache2:a=" EHCacheUtil.get("cache2", "a"));
System.out.println("end");
}