Reputation: 1156
I'm using Ehcache 3.4 to persist some data to the disk when my app is going down and set some max size for the disk persistence. Example from Ehcache documentation:
PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(new File(getStoragePath(), "myData")))
.withCache("threeTieredCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true)
)
).build(true);
I couldn't find an API in the Ehcache library for returning current size of disk persisted data. Is there anything for this? What will happen when the size exceeds the required size?
Upvotes: 0
Views: 771
Reputation: 5721
If you ask for 20MB, the disk space will be up to 20MB and then will start evicting.
Then, to know the size, there is always looking at the disk.
Also, there is an unofficial statistics API. By unofficial, I mean it is internal stuff that might change or disappear. But right now, it's there. You can retrieve the statistics for your cache and then for the underlying tiers.
Here is an example. Note that the real occupied disk space if a bit higher than the allocated space. That's the storage overhead above the actual key/value storage.
@Test
public void test() throws IOException {
StatisticsService statisticsService = new DefaultStatisticsService();
try(PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence("myData"))
.using(statisticsService)
.withCache("threeTieredCache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(10, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true)
)
).build(true)) {
Cache<Long, String> cache = persistentCacheManager.getCache("threeTieredCache", Long.class, String.class);
for(long i = 0; i < 1000; i++) {
cache.put(i, "test");
}
System.out.println("Length: " + getFolderSize("mydata"));
TierStatistics tierStatistics = statisticsService
.getCacheStatistics("threeTieredCache")
.getTierStatistics()
.get("Disk");
System.out.println("Occupied: " + tierStatistics.getOccupiedByteSize());
System.out.println("Allocated: " + tierStatistics.getAllocatedByteSize());
}
}
private long getFolderSize(String folder) throws IOException {
return Files.walk(Paths.get(folder))
.filter(p -> p.toFile().isFile())
.mapToLong(p -> p.toFile().length())
.sum();
}
Upvotes: 2