diff --git a/road/src/main/java/com/example/road/direction/controller/DirectionController.java b/road/src/main/java/com/example/road/direction/controller/DirectionController.java index 10eb3994..6be39a53 100644 --- a/road/src/main/java/com/example/road/direction/controller/DirectionController.java +++ b/road/src/main/java/com/example/road/direction/controller/DirectionController.java @@ -1,13 +1,11 @@ package com.example.road.direction.controller; -import com.example.road.direction.entity.Direction; import com.example.road.direction.service.DirectionService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.util.UriComponentsBuilder; @Slf4j @Controller diff --git a/road/src/main/java/com/example/road/pharmacy/cache/PharmacyRedisTemplateService.java b/road/src/main/java/com/example/road/pharmacy/cache/PharmacyRedisTemplateService.java new file mode 100644 index 00000000..c58ee08d --- /dev/null +++ b/road/src/main/java/com/example/road/pharmacy/cache/PharmacyRedisTemplateService.java @@ -0,0 +1,81 @@ +package com.example.road.pharmacy.cache; + +import com.example.road.pharmacy.dto.PharmacyDto; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +@Slf4j +@Service +@RequiredArgsConstructor +public class PharmacyRedisTemplateService { + + private static final String CACHE_KEY = "PHARMACY"; + + private final RedisTemplate redisTemplate; + private final ObjectMapper objectMapper; + + private HashOperations hashOperations; + + @PostConstruct + public void init() { + this.hashOperations = redisTemplate.opsForHash(); + } + + public void save(PharmacyDto pharmacyDto) { + if (Objects.isNull(pharmacyDto) || Objects.isNull(pharmacyDto.getId())) { + log.error("Required Values must not be null"); + return; + } + + try { + hashOperations.put( + CACHE_KEY, + pharmacyDto.getId().toString(), + serializePharmacyDto(pharmacyDto) + ); + log.info("[PharmacyRedisTemplateService save success] id: {}", pharmacyDto.getId()); + } catch (Exception e) { + log.error("[PharmacyRedisTemplateService save error] {}", e.getMessage()); + } + } + + public List findAll() { + + try { + List list = new ArrayList<>(); + for (String value : hashOperations.entries(CACHE_KEY).values()) { + PharmacyDto pharmacyDto = deserializePharmacyDto(value); + list.add(pharmacyDto); + } + return list; + + } catch (Exception e) { + log.error("[PharmacyRedisTemplateService findAll error] {}", e.getMessage()); + return Collections.emptyList(); + } + } + + public void delete(Long id) { + hashOperations.delete(CACHE_KEY, String.valueOf(id)); + log.info("[PharmacyRedisTemplateService delete] id: {}", id); + } + + private String serializePharmacyDto(PharmacyDto pharmacyDto) throws JsonProcessingException { + return objectMapper.writeValueAsString(pharmacyDto); + } + + private PharmacyDto deserializePharmacyDto(String value) throws JsonProcessingException { + return objectMapper.readValue(value, PharmacyDto.class); + } +} diff --git a/road/src/main/java/com/example/road/pharmacy/controller/PharmacyController.java b/road/src/main/java/com/example/road/pharmacy/controller/PharmacyController.java new file mode 100644 index 00000000..780bd1fc --- /dev/null +++ b/road/src/main/java/com/example/road/pharmacy/controller/PharmacyController.java @@ -0,0 +1,41 @@ +package com.example.road.pharmacy.controller; + +import com.example.road.pharmacy.cache.PharmacyRedisTemplateService; +import com.example.road.pharmacy.dto.PharmacyDto; +import com.example.road.pharmacy.service.PharmacyRepositoryService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +@RestController +@RequiredArgsConstructor +public class PharmacyController { + + private final PharmacyRepositoryService pharmacyRepositoryService; + private final PharmacyRedisTemplateService pharmacyRedisTemplateService; + + @GetMapping("/redis/save") + public String save() { + + List pharmacyDtoList = pharmacyRepositoryService.findAll() + .stream() + .map(pharmacy -> PharmacyDto.builder() + .id(pharmacy.getId()) + .pharmacyName(pharmacy.getPharmacyName()) + .pharmacyAddress(pharmacy.getPharmacyAddress()) + .latitude(pharmacy.getLatitude()) + .longitude(pharmacy.getLongitude()) + .build() + ) + .collect(Collectors.toList()); + + pharmacyDtoList.forEach(pharmacyRedisTemplateService::save); + + return "success"; + } +} diff --git a/road/src/main/java/com/example/road/pharmacy/service/PharmacyRecommendationService.java b/road/src/main/java/com/example/road/pharmacy/service/PharmacyRecommendationService.java index 624f2e54..2cb5119e 100644 --- a/road/src/main/java/com/example/road/pharmacy/service/PharmacyRecommendationService.java +++ b/road/src/main/java/com/example/road/pharmacy/service/PharmacyRecommendationService.java @@ -44,8 +44,8 @@ public class PharmacyRecommendationService { DocumentDto documentDto = kakaoApiResponseDto.getDocumentList().get(0); -// List directionList = directionService.buildDirectionList(documentDto); - List directionList = directionService.buildDirectionListByCategoryApi(documentDto); + List directionList = directionService.buildDirectionList(documentDto); +// List directionList = directionService.buildDirectionListByCategoryApi(documentDto); directionService.saveAll(directionList); diff --git a/road/src/main/java/com/example/road/pharmacy/service/PharmacySearchService.java b/road/src/main/java/com/example/road/pharmacy/service/PharmacySearchService.java index a3e02fe9..11dc323c 100644 --- a/road/src/main/java/com/example/road/pharmacy/service/PharmacySearchService.java +++ b/road/src/main/java/com/example/road/pharmacy/service/PharmacySearchService.java @@ -1,5 +1,6 @@ package com.example.road.pharmacy.service; +import com.example.road.pharmacy.cache.PharmacyRedisTemplateService; import com.example.road.pharmacy.dto.PharmacyDto; import com.example.road.pharmacy.entity.Pharmacy; import lombok.RequiredArgsConstructor; @@ -15,9 +16,18 @@ import java.util.stream.Collectors; public class PharmacySearchService { private final PharmacyRepositoryService pharmacyRepositoryService; + private final PharmacyRedisTemplateService pharmacyRedisTemplateService; public List searchPharmacyDtoList() { + // redis + List pharmacyDtoList = pharmacyRedisTemplateService.findAll(); + if (!pharmacyDtoList.isEmpty()) { + log.info("redis findAll success!"); + return pharmacyDtoList; + } + + // db return pharmacyRepositoryService.findAll() .stream() .map(this::convertToPharmacyDto)