BAEL-3459: Introduction to cache2k (#8651)

This commit is contained in:
Kamlesh Kumar 2020-02-06 08:37:19 +05:30 committed by GitHub
parent 8047fc333a
commit a51dd49517
9 changed files with 249 additions and 1 deletions

View File

@ -67,7 +67,12 @@
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>${cactoos.version}</version>
</dependency>
<dependency>
<groupId>org.cache2k</groupId>
<artifactId>cache2k-base-bom</artifactId>
<version>${cache2k.version}</version>
<type>pom</type>
</dependency>
</dependencies>
@ -88,5 +93,6 @@
<cactoos.version>0.43</cactoos.version>
<airline.version>2.7.2</airline.version>
<cache2k.version>1.2.3.Final</cache2k.version>
</properties>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.cache2k;
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;
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);
}
return discount;
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperUsingLoader {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperUsingLoader.class);
private Cache<String, Integer> cachedDiscounts;
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);
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;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.cache2k;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.cache2k.CacheEntry;
import org.cache2k.event.CacheEntryCreatedListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProductHelperWithEventListener {
final Logger LOGGER = LoggerFactory.getLogger(ProductHelperWithEventListener.class);
private Cache<String, Integer> cachedDiscounts;
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);
return "Sports".equalsIgnoreCase(key) ? 20 : 10;
})
.addListener(new CacheEntryCreatedListener<String, Integer>() {
@Override
public void onEntryCreated(Cache<String, Integer> cache, CacheEntry<String, Integer> entry) {
LOGGER.info("Entry created: [{}, {}].", entry.getKey(), entry.getValue());
}
})
.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;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.cache2k;
import java.util.Objects;
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;
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);
}
return discount;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperUnitTest {
ProductHelper productHelper = new ProductHelper();
@Test
public void whenInvokedGetDiscount_thenGetItFromCache() {
assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getDiscount("Electronics") == 0);
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperUsingLoaderUnitTest {
ProductHelperUsingLoader productHelper = new ProductHelperUsingLoader();
@Test
public void whenInvokedGetDiscount_thenPopulateCache() {
assertTrue(productHelper.getDiscount("Sports") == 20);
assertTrue(productHelper.getDiscount("Electronics") == 10);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperWithEventListenerUnitTest {
ProductHelperWithEventListener productHelper = new ProductHelperWithEventListener();
@Test
public void whenEntryAddedInCache_thenEventListenerCalled() {
assertTrue(productHelper.getDiscount("Sports") == 20);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.cache2k;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ProductHelperWithExpiryUnitTest {
ProductHelperWithExpiry productHelper = new ProductHelperWithExpiry();
@Test
public void whenInvokedGetDiscountForExpiredProduct_thenNoDiscount() throws InterruptedException {
assertTrue(productHelper.getDiscount("Sports") == 20);
Thread.sleep(20);
assertTrue(productHelper.getDiscount("Sports") == 0);
}
}