BAEL-6036: Enable caching on the TasksService for Lightrun Article (#13221)

This commit is contained in:
Graham Cox 2022-12-30 11:13:44 +00:00 committed by GitHub
parent 40249f907c
commit 6579553e8d
4 changed files with 24 additions and 0 deletions

View File

@ -24,6 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>

View File

@ -0,0 +1,16 @@
package com.baeldung.tasksservice;
import java.util.List;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.stereotype.Component;
@Component
public class SimpleCacheCustomizer implements CacheManagerCustomizer<ConcurrentMapCacheManager> {
@Override
public void customize(ConcurrentMapCacheManager cacheManager) {
cacheManager.setCacheNames(List.of("tasks"));
}
}

View File

@ -2,8 +2,10 @@ package com.baeldung.tasksservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class TasksServiceApplication {
public static void main(String[] args) {

View File

@ -19,6 +19,7 @@ import java.util.UUID;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baeldung.tasksservice.adapters.repository.TaskRecord;
@ -29,6 +30,7 @@ public class TasksService {
@Autowired
private TasksRepository tasksRepository;
@Cacheable("tasks")
public TaskRecord getTaskById(String id) {
return tasksRepository.findById(id)
.orElseThrow(() -> new UnknownTaskException(id));