【发布时间】:2022-12-14 08:30:08
【问题描述】:
我正在为我的项目做 poc。我打算使用 EH-cache。 我创建了下面的代码来检查不同层的分配/使用大小
爪哇:11
ehcache 版本:<ehcache3.version>3.7.0</ehcache3.version>
代码 :
public class BasicProgrammatic {
private static final Logger LOGGER = getLogger(BasicProgrammatic.class);
//https://www.ehcache.org/documentation/3.4/getting-started.html
//https://www.ehcache.org/documentation/2.8/code-samples.html#creating-caches-programmatically
public static void main(String[] args) {
LOGGER.info("Creating cache manager programmatically");
StatisticsService statisticsService = new DefaultStatisticsService();
PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.with(CacheManagerBuilder.persistence(new File("/tmp", "jktestcache")))
.withCache("threeTieredCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1, EntryUnit.ENTRIES)
.offheap(2, MemoryUnit.MB)
.disk(1, MemoryUnit.GB)
)
).build(true);
Cache<String, String> threeTieredCache = persistentCacheManager.getCache("threeTieredCache", String.class,
String.class);
LOGGER.info("Putting to cache");
threeTieredCache.put("on1", "da one!");
threeTieredCache.put("on2", "da towo!");
threeTieredCache.put("on3", "da 3!");
threeTieredCache.put("on4", "da 4!");
String value = threeTieredCache.get("on4");
LOGGER.info("Retrieved '{}'", value);
CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("threeTieredCache");
LOGGER.info("getOccupiedByteSize on-heap " + ehCacheStat.getTierStatistics().get("OnHeap").getOccupiedByteSize());
LOGGER.info("getOccupiedByteSize off-heap " + ehCacheStat.getTierStatistics().get("OffHeap").getOccupiedByteSize());
LOGGER.info("getAllocatedByteSize Disk " + ehCacheStat.getTierStatistics().get("Disk").getAllocatedByteSize());
LOGGER.info("getOccupiedByteSize Disk " + ehCacheStat.getTierStatistics().get("Disk").getOccupiedByteSize());
LOGGER.info("Closing cache manager");
persistentCacheManager.close();
}
}
结果 :
2022-11-15 15:22:55,875 [main] INFO org.ehcache.sample.BasicProgrammatic - Creating cache manager programmatically
2022-11-15 15:22:56,172 [main] INFO org.terracotta.offheapstore.paging.UpfrontAllocatingPageSource - Allocating 2MB in chunks
2022-11-15 15:22:56,231 [main] INFO org.ehcache.core.EhcacheManager - Cache 'threeTieredCache' created in EhcacheManager.
2022-11-15 15:22:56,254 [main] INFO org.ehcache.sample.BasicProgrammatic - Putting to cache
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - Retrieved 'da 4!'
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize on-heap -1
2022-11-15 15:22:56,272 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize off-heap 0
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - getAllocatedByteSize Disk 16640
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - getOccupiedByteSize Disk 320
2022-11-15 15:22:56,273 [main] INFO org.ehcache.sample.BasicProgrammatic - Closing cache manager
2022-11-15 15:22:56,322 [main] INFO org.ehcache.core.EhcacheManager - Cache 'threeTieredCache' removed from EhcacheManager.
问题 :
-
我为磁盘缓存分配了 1 GB,但日志仅显示16640.不管我给它什么,它只显示 16640
-
我有 4 个元素,所以一些数据应该已经移到堆外,因为我的堆大小是 1 个条目。如果是这种情况,为什么堆外大小为空
3.为什么堆大小是-1
还有其他要求吗?
我试图查看从堆上、堆外和磁盘中的每一层获取元素所花费的时间,例如 [1ms vs 2ms vs 3ms]。这样我就可以计算权衡
谢谢 jk
【问题讨论】: