Skip to content

Commit

Permalink
Merge pull request #106 from woowa-techcamp-2024/feature/105-cache-de…
Browse files Browse the repository at this point in the history
…livery-location

[feature] cache delivery location
  • Loading branch information
hellomatia authored Aug 22, 2024
2 parents 8f08423 + 7f3628e commit a5ccd70
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
1 change: 1 addition & 0 deletions domain/cache-domain-redis/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies {
implementation project(":domain:review-domain-rdb")
implementation project(":event:review-event")
implementation project(":event:common-event")
implementation project(":event:cache-event")
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package woowa.team4.bff.cache.redis.service;

import com.fasterxml.jackson.core.type.TypeReference;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import woowa.team4.bff.cache.redis.utils.JsonConverter;
import woowa.team4.bff.cache.redis.utils.RedisKeyExtractor;
import woowa.team4.bff.cache.redis.utils.RedisKeyMaker;
import woowa.team4.bff.domain.RestaurantSummary;
import woowa.team4.bff.event.cache.DeliveryLocationAndKeywordCreateEvent;
import woowa.team4.bff.event.reviewstatistics.ReviewStatisticsUpdateEvent;

@Service
Expand All @@ -24,7 +29,7 @@ public class CacheManagerService {
public void handleReviewStatisticsUpdated(ReviewStatisticsUpdateEvent event) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
Long restaurantId = event.restaurantId();
String restaurantKey = RedisKeyExtractor.extractRestaurantKey(restaurantId);
String restaurantKey = RedisKeyMaker.makeRestaurantKey(restaurantId);
String json = valueOperations.get(restaurantKey);
if (json == null) {
return;
Expand All @@ -33,4 +38,17 @@ public void handleReviewStatisticsUpdated(ReviewStatisticsUpdateEvent event) {
restaurantSummary.updateReviewStatistics(event.averageRating(), event.reviews());
valueOperations.set(restaurantKey, jsonConverter.convert(restaurantSummary));
}

@Async
@EventListener
public void handleDeliveryLocationAndKeywordCreate(DeliveryLocationAndKeywordCreateEvent event) {
String key = RedisKeyMaker.makeDeliveryLocation(event.deliveryLocation());
redisTemplate.opsForHash().put(key, event.keyword(),
jsonConverter.convert(event.restaurantIds()));
redisTemplate.expire(
key,
1,
TimeUnit.HOURS
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package woowa.team4.bff.cache.redis.service;

import static com.fasterxml.jackson.databind.type.LogicalType.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import woowa.team4.bff.cache.redis.repository.RestaurantReviewStatisticsRepository;
import woowa.team4.bff.cache.redis.utils.JsonConverter;
import woowa.team4.bff.cache.redis.utils.RedisKeyExtractor;
import woowa.team4.bff.cache.redis.utils.RedisKeyMaker;
import woowa.team4.bff.domain.RestaurantSummary;
import woowa.team4.bff.interfaces.CacheService;

Expand All @@ -21,6 +24,17 @@ public class CacheRedisService implements CacheService {
private final RestaurantReviewStatisticsRepository restaurantReviewStatisticsRepository;
private final JsonConverter jsonConverter;

@Override
public List<Long> findIdsByKeywordAndDeliveryLocation(String keyword, String deliveryLocation) {
String deliveryLocationKey = RedisKeyMaker.makeDeliveryLocation(deliveryLocation);
String json = (String) redisTemplate.opsForHash().get(deliveryLocationKey, keyword);
if(json == null){
return null;
}
return jsonConverter.convert(json, new TypeReference <List<Long>>() {});
}

// ToDo: Async
@Override
public List<RestaurantSummary> findByRestaurantIds(List<Long> ids) {
return ids.stream()
Expand All @@ -31,7 +45,7 @@ public List<RestaurantSummary> findByRestaurantIds(List<Long> ids) {

public RestaurantSummary findById(Long id) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
String restaurantKey = RedisKeyExtractor.extractRestaurantKey(id);
String restaurantKey = RedisKeyMaker.makeRestaurantKey(id);
String json = valueOperations.get(restaurantKey);
if (json == null) {
RestaurantSummary restaurantSummary = restaurantReviewStatisticsRepository.findByRestaurantId(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package woowa.team4.bff.cache.redis.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand All @@ -27,4 +28,12 @@ public <T> T convert(String json, Class<T> clazz) {
throw new JsonConvertException(e);
}
}

public <T> T convert(String json, TypeReference<T> typeReference) {
try {
return objectMapper.readValue(json, typeReference);
} catch (JsonProcessingException e) {
throw new JsonConvertException(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package woowa.team4.bff.cache.redis.utils;

public final class RedisKeyMaker {

public static String makeRestaurantKey(Long id) {
return String.join(":", "restaurant", String.valueOf(id));
}

public static String makeDeliveryLocation(String deliveryLocation){
return String.join(":", "delivery_location", deliveryLocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

public interface CacheService {
List<RestaurantSummary> findByRestaurantIds(List<Long> ids);
List<Long> findIdsByKeywordAndDeliveryLocation(String keyword, String deliveryLocation);
}
3 changes: 3 additions & 0 deletions domain/exposure-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ dependencies {
implementation project(":domain:exposure-common-domain")
implementation project(":domain:search-domain-rdb")
implementation project(":domain:cache-domain-redis")
implementation project(":event:event-publisher")
implementation project(":event:common-event")
implementation project(":event:cache-event")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import woowa.team4.bff.domain.RestaurantSummary;
import woowa.team4.bff.event.cache.DeliveryLocationAndKeywordCreateEvent;
import woowa.team4.bff.exposure.command.SearchCommand;
import woowa.team4.bff.interfaces.CacheService;
import woowa.team4.bff.interfaces.SearchService;

import java.util.List;
import woowa.team4.bff.publisher.EventPublisher;

@Slf4j
@Service
@RequiredArgsConstructor
public class RestaurantExposureListService {

private final SearchService searchService;
private final CacheService cacheService;
private final EventPublisher eventPublisher;

public List<RestaurantSummary> search(SearchCommand command) {
List<Long> restaurantIds = searchService.findIdsByKeywordAndDeliveryLocation(command.keyword(), command.deliveryLocation(), command.pageNumber());
return cacheService.findByRestaurantIds(restaurantIds);
List<Long> restaurantIds = cacheService.findIdsByKeywordAndDeliveryLocation(command.keyword(),
command.deliveryLocation());
if (restaurantIds == null) {
restaurantIds = searchService.findIdsByKeywordAndDeliveryLocation(command.keyword(),
command.deliveryLocation(), command.pageNumber());
eventPublisher.publish(new DeliveryLocationAndKeywordCreateEvent(command.keyword(), command.deliveryLocation(), restaurantIds));
}
List<RestaurantSummary> res = cacheService.findByRestaurantIds(restaurantIds);
// ToDo: 외부 api 호출
return res;
}

// ToDo: 실험 후 제거
Expand Down
2 changes: 1 addition & 1 deletion dummy-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
21 changes: 21 additions & 0 deletions event/cache-event/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'java'
}

group = 'org.example'
version = '0.0.1-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
implementation project(":event:common-event")

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package woowa.team4.bff.event.cache;

import java.util.List;
import woowa.team4.bff.event.Event;

public record DeliveryLocationAndKeywordCreateEvent(String keyword, String deliveryLocation, List<Long> restaurantIds) implements
Event {
}
2 changes: 1 addition & 1 deletion mono/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
languageVersion = JavaLanguageVersion.of(21)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import woowa.team4.bff.common.utils.ApiUtils.ApiResult;
import woowa.team4.bff.domain.RestaurantSummary;
import woowa.team4.bff.exposure.command.SearchCommand;
import woowa.team4.bff.exposure.controller.get.GetRestaurantSummaryRequest;
import woowa.team4.bff.exposure.service.RestaurantExposureListService;

@RequestMapping("/api/v1/restaurant-summary")
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ findProject(':domain:exposure-common-domain')?.name = 'exposure-common-domain'
include 'domain:cache-domain-redis'
findProject(':domain:cache-domain-redis')?.name = 'cache-domain-redis'
include 'dummy-api'
include 'event:cache-event'
findProject(':event:cache-event')?.name = 'cache-event'

0 comments on commit a5ccd70

Please sign in to comment.