BAEL-3459 updated cache2k example and test cases (#8732)

This commit is contained in:
Kamlesh Kumar 2020-02-21 09:16:22 +05:30 committed by GitHub
parent 409ebc1a90
commit a73581fbaf
8 changed files with 55 additions and 63 deletions

View File

@ -4,38 +4,33 @@ import java.util.Objects;
import org.cache2k.Cache; import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder; import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelper { public class ProductHelper {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelper.class);
private Cache<String, Integer> cachedDiscounts; private Cache<String, Integer> cachedDiscounts;
private int cacheMissCount = 0;
public ProductHelper() { public ProductHelper() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount") .name("discount")
.eternal(true) .eternal(true)
.entryCapacity(100) .entryCapacity(100)
.build(); .build();
initDiscountCache("Sports", 20);
}
public void initDiscountCache(String productType, Integer value) {
cachedDiscounts.put(productType, value);
} }
public Integer getDiscount(String productType) { public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType); Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) { if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType); cacheMissCount++;
discount = 0; discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10;
} else { cachedDiscounts.put(productType, discount);
LOGGER.info("Discount for {} found.", productType);
} }
return discount; return discount;
} }
public int getCacheMissCount() {
return cacheMissCount;
}
} }

View File

@ -1,6 +1,5 @@
package com.baeldung.cache2k; package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.cache2k.Cache; import org.cache2k.Cache;
@ -14,28 +13,26 @@ public class ProductHelperUsingLoader {
private Cache<String, Integer> cachedDiscounts; private Cache<String, Integer> cachedDiscounts;
private int cacheMissCount = 0;
public ProductHelperUsingLoader() { public ProductHelperUsingLoader() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-loader") .name("discount-loader")
.eternal(false)
.expireAfterWrite(10, TimeUnit.MILLISECONDS) .expireAfterWrite(10, TimeUnit.MILLISECONDS)
.entryCapacity(100) .entryCapacity(100)
.loader((key) -> { .loader((key) -> {
LOGGER.info("Calculating discount for {}.", key); cacheMissCount++;
return "Sports".equalsIgnoreCase(key) ? 20 : 10; return "Sports".equalsIgnoreCase(key) ? 20 : 10;
}) })
.build(); .build();
} }
public Integer getDiscount(String productType) { public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType); return cachedDiscounts.get(productType);
if (Objects.isNull(discount)) { }
LOGGER.info("Discount for {} not found.", productType);
discount = 0; public int getCacheMissCount() {
} else { return cacheMissCount;
LOGGER.info("Discount for {} found.", productType);
}
return discount;
} }
} }

View File

@ -1,6 +1,5 @@
package com.baeldung.cache2k; package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.cache2k.Cache; import org.cache2k.Cache;
@ -16,14 +15,15 @@ public class ProductHelperWithEventListener {
private Cache<String, Integer> cachedDiscounts; private Cache<String, Integer> cachedDiscounts;
private int cacheMissCount = 0;
public ProductHelperWithEventListener() { public ProductHelperWithEventListener() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-listener") .name("discount-listener")
.eternal(false)
.expireAfterWrite(10, TimeUnit.MILLISECONDS) .expireAfterWrite(10, TimeUnit.MILLISECONDS)
.entryCapacity(100) .entryCapacity(100)
.loader((key) -> { .loader((key) -> {
LOGGER.info("Calculating discount for {}.", key); cacheMissCount++;
return "Sports".equalsIgnoreCase(key) ? 20 : 10; return "Sports".equalsIgnoreCase(key) ? 20 : 10;
}) })
.addListener(new CacheEntryCreatedListener<String, Integer>() { .addListener(new CacheEntryCreatedListener<String, Integer>() {
@ -36,14 +36,11 @@ public class ProductHelperWithEventListener {
} }
public Integer getDiscount(String productType) { public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType); return cachedDiscounts.get(productType);
if (Objects.isNull(discount)) { }
LOGGER.info("Discount for {} not found.", productType);
discount = 0; public int getCacheMissCount() {
} else { return cacheMissCount;
LOGGER.info("Discount for {} found.", productType);
}
return discount;
} }
} }

View File

@ -5,39 +5,34 @@ import java.util.concurrent.TimeUnit;
import org.cache2k.Cache; import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder; import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperWithExpiry { public class ProductHelperWithExpiry {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithExpiry.class);
private Cache<String, Integer> cachedDiscounts; private Cache<String, Integer> cachedDiscounts;
private int cacheMissCount = 0;
public ProductHelperWithExpiry() { public ProductHelperWithExpiry() {
cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class) cachedDiscounts = Cache2kBuilder.of(String.class, Integer.class)
.name("discount-expiry") .name("discount-expiry")
.eternal(false)
.expireAfterWrite(5, TimeUnit.MILLISECONDS) .expireAfterWrite(5, TimeUnit.MILLISECONDS)
.entryCapacity(100) .entryCapacity(100)
.build(); .build();
initDiscountCache("Sports", 20);
}
public void initDiscountCache(String productType, Integer value) {
cachedDiscounts.put(productType, value);
} }
public Integer getDiscount(String productType) { public Integer getDiscount(String productType) {
Integer discount = cachedDiscounts.get(productType); Integer discount = cachedDiscounts.get(productType);
if (Objects.isNull(discount)) { if (Objects.isNull(discount)) {
LOGGER.info("Discount for {} not found.", productType); cacheMissCount++;
discount = 0; discount = "Sports".equalsIgnoreCase(productType) ? 20 : 10;
} else { cachedDiscounts.put(productType, discount);
LOGGER.info("Discount for {} found.", productType);
} }
return discount; return discount;
} }
public int getCacheMissCount() {
return cacheMissCount;
}
} }

View File

@ -6,12 +6,13 @@ import org.junit.Test;
public class ProductHelperUnitTest { public class ProductHelperUnitTest {
ProductHelper productHelper = new ProductHelper();
@Test @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("Sports") == 20);
assertTrue(productHelper.getDiscount("Electronics") == 0); assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getCacheMissCount() == 1);
} }
} }

View File

@ -6,12 +6,16 @@ import org.junit.Test;
public class ProductHelperUsingLoaderUnitTest { public class ProductHelperUsingLoaderUnitTest {
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
@Test @Test
public void whenInvokedGetDiscount_thenPopulateCache() { public void whenInvokedGetDiscount_thenPopulateCacheUsingLoader() {
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
assertTrue(productHelper.getCacheMissCount() == 0);
assertTrue(productHelper.getDiscount("Sports") == 20); assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getCacheMissCount() == 1);
assertTrue(productHelper.getDiscount("Electronics") == 10); assertTrue(productHelper.getDiscount("Electronics") == 10);
assertTrue(productHelper.getCacheMissCount() == 2);
} }
} }

View File

@ -6,10 +6,9 @@ import org.junit.Test;
public class ProductHelperWithEventListenerUnitTest { public class ProductHelperWithEventListenerUnitTest {
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
@Test @Test
public void whenEntryAddedInCache_thenEventListenerCalled() { public void whenEntryAddedInCache_thenEventListenerCalled() {
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
assertTrue(productHelper.getDiscount("Sports") == 20); assertTrue(productHelper.getDiscount("Sports") == 20);
} }

View File

@ -6,13 +6,17 @@ import org.junit.Test;
public class ProductHelperWithExpiryUnitTest { public class ProductHelperWithExpiryUnitTest {
ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry();
@Test @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.getDiscount("Sports") == 20);
assertTrue(productHelper.getCacheMissCount() == 1);
Thread.sleep(20); Thread.sleep(20);
assertTrue(productHelper.getDiscount("Sports") == 0);
assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getCacheMissCount() == 2);
} }
} }