BAEL-3459 updated cache2k example and test cases (#8732)
This commit is contained in:
@@ -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<String, Integer> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> 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<String, Integer>() {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, Integer> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user