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();
}