Skip to content

Commit

Permalink
Merge pull request #149 from WePlanPlans/BE-78-Feat-Redis-Caching
Browse files Browse the repository at this point in the history
Be 78 feat redis caching
  • Loading branch information
yuhyun1 authored Feb 4, 2024
2 parents 37de95f + 91cc8b4 commit ce0b2f7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/main/java/org/tenten/tentenbe/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.tenten.tentenbe.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
Expand All @@ -9,7 +8,6 @@
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
@Configuration
public class RedisConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.tenten.tentenbe.domain.tour.dto.response;

import java.util.List;

public record TourSimpleResponsePage(
List<TourSimpleResponse> tourSimpleResponseList,
long totalElements
) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tenten.tentenbe.domain.tour.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -14,16 +15,19 @@
import org.tenten.tentenbe.domain.review.model.Review;
import org.tenten.tentenbe.domain.tour.dto.response.TourDetailResponse;
import org.tenten.tentenbe.domain.tour.dto.response.TourSimpleResponse;
import org.tenten.tentenbe.domain.tour.dto.response.TourSimpleResponsePage;
import org.tenten.tentenbe.domain.tour.exception.TourException;
import org.tenten.tentenbe.domain.tour.model.TourItem;
import org.tenten.tentenbe.domain.tour.repository.TourItemRepository;
import org.tenten.tentenbe.global.cache.RedisCache;
import org.tenten.tentenbe.global.common.enums.Category;
import org.tenten.tentenbe.global.common.enums.Region;

import java.util.List;
import java.util.Optional;

import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.tenten.tentenbe.global.common.constant.TopicConstant.TOUR_ITEM;

@Slf4j
@Service
Expand All @@ -32,17 +36,32 @@ public class TourService {
private final TourItemRepository tourItemRepository;
private final MemberRepository memberRepository;
private final LikedItemRepository likedItemRepository;
private final RedisCache redisCache;
private final ObjectMapper objectMapper;

@Transactional(readOnly = true)
public Page<TourSimpleResponse> getTours(Long memberId, String regionName, Pageable pageable) {
return getTourSimpleResponsePage(memberId, regionName, pageable);
}

private Page<TourSimpleResponse> getTourSimpleResponsePage(Long memberId, String regionName, Pageable pageable) {
if (regionName == null) {
return tourItemRepository.findPopularTourItems(memberId, pageable);
String topic = buildTopic(regionName, pageable);

// Redis에서 데이터 가져오기
Object cachedData = redisCache.get(topic, String.valueOf(memberId));
if (cachedData != null) {
TourSimpleResponsePage tourSimpleResponsePage =
objectMapper.convertValue(cachedData, TourSimpleResponsePage.class);
return new PageImpl<>(
tourSimpleResponsePage.tourSimpleResponseList(), pageable, tourSimpleResponsePage.totalElements());
}
return tourItemRepository.findPopularTourItems(Region.fromName(regionName).getAreaCode(), memberId, pageable);

// 캐시에 데이터가 없으면 DB에서 조회
Page<TourSimpleResponse> tourSimpleResponses = (regionName == null) ?
tourItemRepository.findPopularTourItems(memberId, pageable) :
tourItemRepository.findPopularTourItems(Region.fromName(regionName).getAreaCode(), memberId, pageable);

// 조회한 데이터를 Redis에 저장
TourSimpleResponsePage tourSimpleResponsePage =
new TourSimpleResponsePage(tourSimpleResponses.toList(), tourSimpleResponses.getTotalElements());
redisCache.save(topic, String.valueOf(memberId), tourSimpleResponsePage);
return tourSimpleResponses;
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -120,4 +139,13 @@ private String getFullAddress(String address, String detailedAddress) {
return address + " " + detailedAddress;
}

private String buildTopic(String regionName, Pageable pageable) {
String baseTopic = TOUR_ITEM + " - ";
if (regionName == null) {
return baseTopic + pageable.toString();
} else {
return baseTopic + regionName + " - " + pageable.toString();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

public class TopicConstant {
public static final String REFRESH_TOKEN = "refreshToken";
public static final String TOUR_ITEM = "tourItem";
}

0 comments on commit ce0b2f7

Please sign in to comment.