Skip to content

Commit

Permalink
adding ehcache. closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Jun 9, 2019
1 parent b1ac08e commit 0a75094
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package guru.springframework.msscbeerservice.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

/**
* Created by jt on 2019-06-09.
*/
@EnableCaching
@Configuration
public class CacheConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import guru.springframework.msscbeerservice.web.model.BeerPagedList;
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +26,7 @@ public class BeerServiceImpl implements BeerService {
private final BeerRepository beerRepository;
private final BeerMapper beerMapper;

@Cacheable(cacheNames = "beerListCache", condition = "#showInventoryOnHand == false ")
@Override
public BeerPagedList listBeers(String beerName, BeerStyleEnum beerStyle, PageRequest pageRequest, Boolean showInventoryOnHand) {

Expand Down Expand Up @@ -69,6 +71,7 @@ public BeerPagedList listBeers(String beerName, BeerStyleEnum beerStyle, PageReq
return beerPagedList;
}

@Cacheable(cacheNames = "beerCache", key = "#beerId", condition = "#showInventoryOnHand == false ")
@Override
public BeerDto getById(UUID beerId, Boolean showInventoryOnHand) {
if (showInventoryOnHand) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Positive;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.UUID;
Expand All @@ -22,7 +23,9 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BeerDto {
public class BeerDto implements Serializable {

static final long serialVersionUID = -5815566940065181210L;

@Null
private UUID id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.io.Serializable;
import java.util.List;

/**
* Created by jt on 2019-05-12.
*/
public class BeerPagedList extends PageImpl<BeerDto> {
public class BeerPagedList extends PageImpl<BeerDto> implements Serializable {

static final long serialVersionUID = 1114715135625836949L;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public BeerPagedList(@JsonProperty("content") List<BeerDto> content,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
sfg.brewery.beer-inventory-service-host=http://localhost:8082
spring.datasource.initialization-mode=EMBEDDED
spring.cache.jcache.config=classpath:ehcache.xml
20 changes: 20 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<config
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xmlns='http://www.ehcache.org/v3'>
<service>
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>

<cache alias="beerCache" uses-template="config-cache"/>
<cache alias="beerListCache" uses-template="config-cache"/>

<cache-template name="config-cache">
<expiry>
<ttl unit="minutes">5</ttl>
</expiry>
<resources>
<heap>1</heap>
<offheap unit="MB">1</offheap>
</resources>
</cache-template>
</config>

0 comments on commit 0a75094

Please sign in to comment.