Skip to content

Commit 8c09454

Browse files
author
Mikalai Alimenkou
committed
Switched to Spring Cache
1 parent 06c2559 commit 8c09454

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

src/main/java/com/xpinjection/library/LibraryApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import org.springframework.boot.builder.SpringApplicationBuilder;
66
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
77
import org.springframework.boot.context.properties.EnableConfigurationProperties;
8+
import org.springframework.cache.annotation.EnableCaching;
89

910
@SpringBootApplication
11+
@EnableCaching
1012
@EnableConfigurationProperties(LibrarySettings.class)
1113
public class LibraryApplication {
1214
public static void main(String[] args) {

src/main/java/com/xpinjection/library/service/impl/BookServiceImpl.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import io.micrometer.observation.annotation.Observed;
99
import lombok.extern.slf4j.Slf4j;
1010
import org.apache.commons.lang3.StringUtils;
11+
import org.springframework.cache.Cache;
12+
import org.springframework.cache.CacheManager;
1113
import org.springframework.stereotype.Service;
1214
import org.springframework.transaction.annotation.Transactional;
1315
import org.springframework.util.Assert;
1416

1517
import java.util.List;
16-
import java.util.concurrent.ConcurrentHashMap;
17-
import java.util.concurrent.ConcurrentMap;
18-
import java.util.stream.Stream;
1918

2019
import static net.logstash.logback.argument.StructuredArguments.value;
2120
import static net.logstash.logback.marker.Markers.append;
@@ -28,18 +27,21 @@
2827
@Transactional
2928
public class BookServiceImpl implements BookService {
3029
private final BookDao bookDao;
31-
private final ConcurrentMap<String, List<Book>> cache = new ConcurrentHashMap<>();
30+
private final Cache cache;
3231

33-
public BookServiceImpl(BookDao bookDao) {
32+
public BookServiceImpl(BookDao bookDao, CacheManager cacheManager) {
3433
this.bookDao = bookDao;
34+
this.cache = cacheManager.getCache("booksByAuthor");
3535
}
3636

3737
@Override
3838
public List<BookDto> addBooks(Books books) {
3939
LOG.info("Adding books: {}", books);
40-
return toDto(books.stream()
40+
var savedBooks = books.stream()
4141
.map(entry -> new Book(entry.getKey(), entry.getValue()))
42-
.map(bookDao::save));
42+
.map(bookDao::save)
43+
.toList();
44+
return toDto(savedBooks);
4345
}
4446

4547
@Override
@@ -52,14 +54,14 @@ public List<BookDto> findBooksByAuthor(String author) {
5254
"Try to find books by author: {}", value("author", author));
5355
Assert.hasText(author, "Author is empty!");
5456
var normalizedAuthor = normalizeAuthorName(author);
55-
var books = cache.computeIfAbsent(normalizedAuthor, bookDao::findByAuthor).stream();
57+
var books = cache.get(normalizedAuthor, () -> bookDao.findByAuthor(author));
5658
return toDto(books);
5759
}
5860

5961
@Override
6062
public List<BookDto> findAllBooks() {
6163
LOG.info("Finding all books");
62-
return toDto(bookDao.findAll().stream());
64+
return toDto(bookDao.findAll());
6365
}
6466

6567
private String normalizeAuthorName(String author) {
@@ -81,8 +83,9 @@ private String splitOnFirstAndLastNames(String author) {
8183
return String.join(" ", firstName, lastName);
8284
}
8385

84-
private List<BookDto> toDto(Stream<Book> books) {
85-
return books.map(BookServiceImpl::toDto)
86+
private List<BookDto> toDto(List<Book> books) {
87+
return books.stream()
88+
.map(BookServiceImpl::toDto)
8689
.toList();
8790
}
8891

src/main/resources/application-perftest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ spring:
33
url: jdbc:postgresql://db:5432/library
44
username: test
55
password: test
6+
cache:
7+
type: none
68

79
management:
810
otlp:

src/main/resources/application.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ spring:
7070
static-locations: classpath:/static/, classpath:/api/spec/
7171
jmx:
7272
enabled: false
73+
cache:
74+
type: simple
75+
cache-names: booksByAuthor
7376

7477
library:
7578
size: 0

src/test/java/com/xpinjection/library/service/BookServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.junit.jupiter.api.extension.ExtendWith;
1111
import org.mockito.Mock;
1212
import org.mockito.junit.jupiter.MockitoExtension;
13+
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
1314

1415
import java.util.HashMap;
1516
import java.util.Map;
@@ -33,7 +34,7 @@ public class BookServiceTest {
3334

3435
@BeforeEach
3536
void init() {
36-
bookService = new BookServiceImpl(dao);
37+
bookService = new BookServiceImpl(dao, new ConcurrentMapCacheManager("booksByAuthor"));
3738
}
3839

3940
@Test

0 commit comments

Comments
 (0)