Skip to content

Commit

Permalink
Merge pull request #235 from woowacourse-teams/feature/234-method-cache
Browse files Browse the repository at this point in the history
#234 조회 메서드의 동일한 결과값 캐시하기
  • Loading branch information
icyMojito authored Oct 22, 2020
2 parents 2360be0 + c3f2d9f commit 0e8149d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.springframework.boot:spring-boot-starter-cache'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/saebyeok/saebyeok/SaebyeokApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@SpringBootApplication
public class SaebyeokApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.saebyeok.saebyeok.dto.EmotionResponse;
import com.saebyeok.saebyeok.exception.EmotionNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -17,20 +18,23 @@ public class EmotionService {

private final EmotionRepository emotionRepository;

@Cacheable(value = "Emotions")
public List<EmotionResponse> getEmotions() {
return emotionRepository.findAll().
stream().
stream().
map(EmotionResponse::new).
collect(Collectors.toList());
}

@Cacheable(value = "EmotionDetailResponse")
public EmotionDetailResponse readEmotion(Long emotionId) {
Emotion emotion = emotionRepository.findById(emotionId).
orElseThrow(() -> new EmotionNotFoundException(emotionId));

return new EmotionDetailResponse(emotion);
}

@Cacheable(value = "AllEmotionsIds")
public List<Long> getAllEmotionsIds() {
return emotionRepository.findAll().
stream().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.saebyeok.saebyeok.dto.report.ReportCreateRequest;
import com.saebyeok.saebyeok.exception.ReportCategoryNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,6 +22,7 @@ public class ReportService {
private final ReportCategoryRepository reportCategoryRepository;
private final ReportRepository reportRepository;

@Cacheable(value = "ReportCategories")
public List<ReportCategoryResponse> getReportCategories() {
return reportCategoryRepository.findAll().
stream().
Expand Down

0 comments on commit 0e8149d

Please sign in to comment.