BAEL-6036: Enable caching on the TasksService for Lightrun Article (#13221)
This commit is contained in:
parent
40249f907c
commit
6579553e8d
@ -24,6 +24,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-artemis</artifactId>
|
<artifactId>spring-boot-starter-artemis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-cache</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
@ -2,8 +2,10 @@ package com.baeldung.tasksservice;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@EnableCaching
|
||||||
public class TasksServiceApplication {
|
public class TasksServiceApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -19,6 +19,7 @@ import java.util.UUID;
|
|||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.baeldung.tasksservice.adapters.repository.TaskRecord;
|
import com.baeldung.tasksservice.adapters.repository.TaskRecord;
|
||||||
@ -29,6 +30,7 @@ public class TasksService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TasksRepository tasksRepository;
|
private TasksRepository tasksRepository;
|
||||||
|
|
||||||
|
@Cacheable("tasks")
|
||||||
public TaskRecord getTaskById(String id) {
|
public TaskRecord getTaskById(String id) {
|
||||||
return tasksRepository.findById(id)
|
return tasksRepository.findById(id)
|
||||||
.orElseThrow(() -> new UnknownTaskException(id));
|
.orElseThrow(() -> new UnknownTaskException(id));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user