EhCache常用配置详解和持久化硬盘配置

一、EhCache常用配置

EhCache 给我们提供了丰富的配置来配置缓存的设置;

这里列出一些常见的配置项:

cache元素的属性:  

    name:缓存名称  

    maxElementsInMemory:内存中最大缓存对象数  

    maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大  

    eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false  

    overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。  

    diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。  

    diskPersistent:是否缓存虚拟机重启期数据  

    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120秒  

    timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态  

   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 Ehcache 作为 JCache 的实现,可以通过以下代码方式配置持久化: ```java import javax.cache.CacheManager; import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; import javax.cache.spi.CachingProvider; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; import org.ehcache.expiry.Duration; import org.ehcache.expiry.Expirations; import org.ehcache.jsr107.Eh107Configuration; public class EhcacheJCachePersistenceExample { public static void main(String[] args) { // Create a cache manager using the CachingProvider for Ehcache CachingProvider provider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider"); CacheManager cacheManager = provider.getCacheManager(); // Configure the cache MutableConfiguration<Long, String> config = new MutableConfiguration<Long, String>() .setTypes(Long.class, String.class) .setExpiryPolicyFactory(Expirations.timeToLiveExpiration(Duration.ofSeconds(60))) .setStoreByValue(false) .setStatisticsEnabled(true) .addCacheWriterFactory(new MyCacheWriterFactory()); // Add persistence configuration CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder .newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(100, EntryUnit.ENTRIES) .offheap(1, MemoryUnit.MB)) .withExpiry(Expirations.timeToLiveExpiration(Duration.ofSeconds(60))) .withLoaderWriter(new MyCacheLoaderWriter()) .withService(new javax.cache.integration.CompositeCacheWriterConfiguration(new MyCacheWriterConfiguration())) .withService(new javax.cache.integration.CompositeCacheLoaderWriterConfiguration(new MyCacheLoaderWriterConfiguration())) .withService(new org.ehcache.jsr107.Eh107Configuration(Long.class, String.class, Expirations.timeToLiveExpiration(Duration.ofSeconds(60)))) .withDiskStore("persistent-cache"); // Create the cache javax.cache.Cache<Long, String> cache = cacheManager.createCache("myCache", Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfigurationBuilder.build()), config); // Use the cache cache.put(1L, "Hello, world!"); System.out.println(cache.get(1L)); // Close the cache manager and release resources cacheManager.close(); provider.close(); } } ``` 其中,`withDiskStore("persistent-cache")` 配置了缓存持久化到磁盘的存储目录为 "persistent-cache"。同时,还可以通过 `withLoaderWriter()` 方法配置缓存的读写器,实现将数据持久化到磁盘或数据库中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值