diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java index dc984e5f0b..cc646c9e17 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelper.java @@ -4,38 +4,33 @@ import java.util.Objects; import org.cache2k.Cache; import org.cache2k.Cache2kBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ProductHelper { - final Logger LOGGER = LoggerFactory.getLogger(ProductHelper.class); - private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelper() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount") .eternal(true) .entryCapacity(100) .build(); - - initDiscountCache("Sports", 20); - } - - public void initDiscountCache(String productType, Integer value) { - cachedDiscounts.put(productType, value); } public Integer getDiscount(String productType) { Integer discount = cachedDiscounts.get(productType); if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); + cacheMissCount++; + discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10; + cachedDiscounts.put(productType, discount); } return discount; } + public int getCacheMissCount() { + return cacheMissCount; + } + } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java index 787a78cd36..7b2ac4caa1 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperUsingLoader.java @@ -1,6 +1,5 @@ package com.baeldung.cache2k; -import java.util.Objects; import java.util.concurrent.TimeUnit; import org.cache2k.Cache; @@ -14,28 +13,26 @@ public class ProductHelperUsingLoader { private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperUsingLoader() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-loader") - .eternal(false) .expireAfterWrite(10, TimeUnit.MILLISECONDS) .entryCapacity(100) .loader((key) -> { - LOGGER.info("Calculating discount for {}.", key); + cacheMissCount++; return "Sports".equalsIgnoreCase(key) ? 20 : 10; }) .build(); } public Integer getDiscount(String productType) { - Integer discount = cachedDiscounts.get(productType); - if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); - } - return discount; + return cachedDiscounts.get(productType); + } + + public int getCacheMissCount() { + return cacheMissCount; } } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java index 5b9eb28c68..90c6ee3adf 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithEventListener.java @@ -1,6 +1,5 @@ package com.baeldung.cache2k; -import java.util.Objects; import java.util.concurrent.TimeUnit; import org.cache2k.Cache; @@ -16,14 +15,15 @@ public class ProductHelperWithEventListener { private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperWithEventListener() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-listener") - .eternal(false) .expireAfterWrite(10, TimeUnit.MILLISECONDS) .entryCapacity(100) .loader((key) -> { - LOGGER.info("Calculating discount for {}.", key); + cacheMissCount++; return "Sports".equalsIgnoreCase(key) ? 20 : 10; }) .addListener(new CacheEntryCreatedListener() { @@ -36,14 +36,11 @@ public class ProductHelperWithEventListener { } public Integer getDiscount(String productType) { - Integer discount = cachedDiscounts.get(productType); - if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); - } - return discount; + return cachedDiscounts.get(productType); + } + + public int getCacheMissCount() { + return cacheMissCount; } } diff --git a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java index b0bf8f90de..22b656fead 100644 --- a/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java +++ b/libraries-3/src/main/java/com/baeldung/cache2k/ProductHelperWithExpiry.java @@ -5,39 +5,34 @@ import java.util.concurrent.TimeUnit; import org.cache2k.Cache; import org.cache2k.Cache2kBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class ProductHelperWithExpiry { - final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithExpiry.class); - private Cache cachedDiscounts; + private int cacheMissCount = 0; + public ProductHelperWithExpiry() { cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) .name("discount-expiry") - .eternal(false) .expireAfterWrite(5, TimeUnit.MILLISECONDS) .entryCapacity(100) .build(); - initDiscountCache("Sports", 20); - } - - public void initDiscountCache(String productType, Integer value) { - cachedDiscounts.put(productType, value); } public Integer getDiscount(String productType) { Integer discount = cachedDiscounts.get(productType); if (Objects.isNull(discount)) { - LOGGER.info("Discount for {} not found.", productType); - discount = 0; - } else { - LOGGER.info("Discount for {} found.", productType); + cacheMissCount++; + discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10; + cachedDiscounts.put(productType, discount); } return discount; } + public int getCacheMissCount() { + return cacheMissCount; + } + } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java index 69da2591dd..e9b495279a 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUnitTest.java @@ -6,12 +6,13 @@ import org.junit.Test; public class ProductHelperUnitTest { - ProductHelper productHelper = new ProductHelper(); - @Test - public void whenInvokedGetDiscount_thenGetItFromCache() { + public void whenInvokedGetDiscountTwice_thenGetItFromCache() { + ProductHelper productHelper = new ProductHelper(); + assertTrue(productHelper.getCacheMissCount() == 0); assertTrue(productHelper.getDiscount("Sports") == 20); - assertTrue(productHelper.getDiscount("Electronics") == 0); + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); } } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java index 2656e75cab..3ad77aa2de 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperUsingLoaderUnitTest.java @@ -6,12 +6,16 @@ import org.junit.Test; public class ProductHelperUsingLoaderUnitTest { - ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader(); - @Test - public void whenInvokedGetDiscount_thenPopulateCache() { + public void whenInvokedGetDiscount_thenPopulateCacheUsingLoader() { + ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader(); + assertTrue(productHelper.getCacheMissCount() == 0); + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); + assertTrue(productHelper.getDiscount("Electronics") == 10); + assertTrue(productHelper.getCacheMissCount() == 2); } } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java index 7bf08232f4..9aeb9f0552 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithEventListenerUnitTest.java @@ -6,10 +6,9 @@ import org.junit.Test; public class ProductHelperWithEventListenerUnitTest { - ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener(); - @Test public void whenEntryAddedInCache_thenEventListenerCalled() { + ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener(); assertTrue(productHelper.getDiscount("Sports") == 20); } diff --git a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java index 65feba2c70..a3303ca0aa 100644 --- a/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java +++ b/libraries-3/src/test/java/com/baeldung/cache2k/ProductHelperWithExpiryUnitTest.java @@ -6,13 +6,17 @@ import org.junit.Test; public class ProductHelperWithExpiryUnitTest { - ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry(); - @Test - public void whenInvokedGetDiscountForExpiredProduct_thenNoDiscount() throws InterruptedException { + public void whenInvokedGetDiscountAfterExpiration_thenDiscountCalculatedAgain() throws InterruptedException { + ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry(); + assertTrue(productHelper.getCacheMissCount() == 0); assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 1); + Thread.sleep(20); - assertTrue(productHelper.getDiscount("Sports") == 0); + + assertTrue(productHelper.getDiscount("Sports") == 20); + assertTrue(productHelper.getCacheMissCount() == 2); } }