0

I am doing poc for my project . I am planning to to use EH-cache. I created a below code to check the allocated/used size of different tier

java : 11

ehcache version : <ehcache3.version>3.7.0</ehcache3.version>

code :

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

    }

}

result :

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.

Question :

  1. I allocated 1 GB for disk cache how ever log shows only 16640 . Irrespective of what ever i give it is showing 16640 only

  2. I have 4 elements so some data should have moved to off-heap because my heap size is 1 entry . If that is case why off-heap size is empty

3.Why on heap size is -1

Any other requirement ?

I am try to see what is time taken for getting element from each tier on-heap vs off-heap vs disk something like [1ms vs 2ms vs 3ms] . So that i can calculate the tradeoff

Thanks Jk

Jeya Kumar
  • 1,002
  • 1
  • 13
  • 36

0 Answers0