spring-boot ehcache example added (#6136)

This commit is contained in:
pcoates33 2019-01-21 08:14:06 +00:00 committed by Grzegorz Piwowarek
parent 0fb2adfe99
commit 369655aa02
6 changed files with 54 additions and 69 deletions

View File

@ -1,57 +1,10 @@
package com.baeldung.cachetest.config; package com.baeldung.cachetest.config;
import java.math.BigDecimal;
import java.time.Duration;
import javax.cache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.event.EventType;
import org.ehcache.jsr107.Eh107Configuration;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableCaching @EnableCaching
public class CacheConfig { public class CacheConfig {
private static final int ON_HEAP_CACHE_SIZE_ENTRIES = 2;
private static final int OFF_HEAP_CACHE_SIZE_MB = 10;
private static final int CACHE_EXPIRY_SECONDS = 30;
@Bean
public JCacheManagerCustomizer jcacheManagerCustomizer() {
return new JCacheManagerCustomizer() {
@Override
public void customize(CacheManager cacheManager) {
ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(ON_HEAP_CACHE_SIZE_ENTRIES, EntryUnit.ENTRIES)
.offheap(OFF_HEAP_CACHE_SIZE_MB, MemoryUnit.MB).build();
CacheEventListenerConfigurationBuilder eventLoggerConfig = CacheEventListenerConfigurationBuilder
.newEventListenerConfiguration(new CacheEventLogger(), EventType.CREATED, EventType.EXPIRED)
.unordered().asynchronous();
CacheConfiguration<?, ?> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Long.class, BigDecimal.class, resourcePools)
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(CACHE_EXPIRY_SECONDS)))
.add(eventLoggerConfig).build();
cacheManager.createCache("squareCache",
Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration));
}
};
}
} }

View File

@ -11,8 +11,7 @@ public class CacheEventLogger implements CacheEventListener<Object, Object> {
@Override @Override
public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) { public void onEvent(CacheEvent<? extends Object, ? extends Object> cacheEvent) {
log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(), log.info("Cache event {} for item with key {}. Old value = {}, New value = {}", cacheEvent.getType(), cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
cacheEvent.getKey(), cacheEvent.getOldValue(), cacheEvent.getNewValue());
} }
} }

View File

@ -14,7 +14,8 @@ public class NumberService {
@Cacheable(value = "squareCache", key = "#number", condition = "#number>10") @Cacheable(value = "squareCache", key = "#number", condition = "#number>10")
public BigDecimal square(Long number) { public BigDecimal square(Long number) {
BigDecimal square = BigDecimal.valueOf(number).multiply(BigDecimal.valueOf(number)); BigDecimal square = BigDecimal.valueOf(number)
.multiply(BigDecimal.valueOf(number));
log.info("square of {} is {}", number, square); log.info("square of {} is {}", number, square);
return square; return square;
} }

View File

@ -0,0 +1 @@
spring.cache.jcache.config=classpath:ehcache.xml

View File

@ -0,0 +1,31 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.ehcache.org/v3"
xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<cache alias="squareCache">
<key-type>java.lang.Long</key-type>
<value-type>java.math.BigDecimal</value-type>
<expiry>
<ttl unit="seconds">30</ttl>
</expiry>
<listeners>
<listener>
<class>com.baeldung.cachetest.config.CacheEventLogger</class>
<event-firing-mode>ASYNCHRONOUS</event-firing-mode>
<event-ordering-mode>UNORDERED</event-ordering-mode>
<events-to-fire-on>CREATED</events-to-fire-on>
<events-to-fire-on>EXPIRED</events-to-fire-on>
</listener>
</listeners>
<resources>
<heap unit="entries">2</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache>
</config>