Ehcache 3.x 快速使用

本文介绍了Ehcache 3.x的快速使用,包括简介和Hello World示例。讲解了直接编码配置和通过XML配置两种方式,并概述了不同的缓存策略如TTI、TTL和none。此外,还提到了heap、off-heap和disk三种存储类型的特性。
摘要由CSDN通过智能技术生成

Ehcache 3.X快速使用

简介

Ehcache 是一个开源的高性能缓存,拥有很高的拓展性和伸缩性,广泛使用各种 Java 项目中(如 Hibernate 默认使用 Ehcache作为二级缓存),在目前基于 Java 的缓存方案里,几乎是性能最高的实现,目前新版本的 Ehcache 3.X 通过支持 Terracotta 改善了2.X 版本体验不佳的分布式缓存支持;

Ehcahe 3.X 和 Ehache 2.X 的 API 差异比较大,以下示例以 Ehcache 3.x 为主;

Ehcache 官网: http://www.ehcache.org
Ehcache 3.X 技术文档: http://www.ehcache.org/documentation/

使用 Ehcache 需要导入依赖:org.ehcache:ehcache
如在 Gradle 中:
 
dependencies {
           
    compile 'org.ehcache:ehcache:3.4.0'
}

Hello world

以下通过一个简单的示例,来演示 Ehcache 3.X 的基本使用,Ehcache支持2种配置方式:直接通过编码配置,通过XML配置;

直接编码配置

 
//构建一个缓存管理器,创建一个默认的缓存 "preConfigured"
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .withCache("preConfigured",         //缓存别名
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,   
                        ResourcePoolsBuilder.heap(100))         //设置缓存堆容纳元素个数
                        .build())
        .build(true);               //创建之后立即初始化
//从缓存管理器中获取预定的缓存
Cache<Long, String> preConfigured
        = cacheManager.getCache("preConfigured", Long.class, String.class);
//直接从缓存管理器创建一个新的缓存
Cache<Long, String> myCache = cacheManager.createCache("myCache",
        CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
                ResourcePoolsBuilder.heap
EhCache3.X可以在Java代码中使用编程方式配置,而不需要使用XML文件。以下是使用Java代码配置EhCache3.X的示例: ```java 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.ConfigurationBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.expiry.Duration; import org.ehcache.expiry.Expirations; import java.util.concurrent.TimeUnit; public class EhCacheExample { public static void main(String[] args) { // Create a cache configuration CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder( Long.class, String.class, // Set the maximum number of entries in the cache to 100 CacheConfigurationBuilder.newResourcePoolsBuilder().heap(100, EntryUnit.ENTRIES)) // Set the duration of the entries to be 5 minutes .withExpiry(Expirations.timeToLiveExpiration(Duration.of(5, TimeUnit.MINUTES))) .build(); // Create a cache manager configuration Configuration configuration = ConfigurationBuilder.newConfigurationBuilder().build(); // Create a cache manager CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfiguration) .withConfiguration(configuration) .build(true); // Get the cache Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); // Put an entry in the cache cache.put(1L, "Hello, world!"); // Get an entry from the cache String value = cache.get(1L); // Print the value System.out.println(value); // Close the cache manager cacheManager.close(); } } ``` 在上面的示例中,我们使用`CacheConfigurationBuilder`和`ConfigurationBuilder`创建了缓存缓存管理器的配置,并使用`CacheManagerBuilder`创建了缓存管理器。我们还设置了缓存的最大条目数和条目的过期时间。最后,我们将一个条目放入缓存中并从缓存中获取它。最后,我们关闭了缓存管理器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值