#26 pharmacy: redis - cache pharmacyDto
This commit is contained in:
@@ -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
|
||||
|
||||
81
road/src/main/java/com/example/road/pharmacy/cache/PharmacyRedisTemplateService.java
vendored
Normal file
81
road/src/main/java/com/example/road/pharmacy/cache/PharmacyRedisTemplateService.java
vendored
Normal file
@@ -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<String, Object> redisTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private HashOperations<String, String, String> 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<PharmacyDto> findAll() {
|
||||
|
||||
try {
|
||||
List<PharmacyDto> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<PharmacyDto> 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";
|
||||
}
|
||||
}
|
||||
@@ -44,8 +44,8 @@ public class PharmacyRecommendationService {
|
||||
|
||||
DocumentDto documentDto = kakaoApiResponseDto.getDocumentList().get(0);
|
||||
|
||||
// List<Direction> directionList = directionService.buildDirectionList(documentDto);
|
||||
List<Direction> directionList = directionService.buildDirectionListByCategoryApi(documentDto);
|
||||
List<Direction> directionList = directionService.buildDirectionList(documentDto);
|
||||
// List<Direction> directionList = directionService.buildDirectionListByCategoryApi(documentDto);
|
||||
|
||||
directionService.saveAll(directionList);
|
||||
|
||||
|
||||
@@ -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<PharmacyDto> searchPharmacyDtoList() {
|
||||
|
||||
// redis
|
||||
List<PharmacyDto> pharmacyDtoList = pharmacyRedisTemplateService.findAll();
|
||||
if (!pharmacyDtoList.isEmpty()) {
|
||||
log.info("redis findAll success!");
|
||||
return pharmacyDtoList;
|
||||
}
|
||||
|
||||
// db
|
||||
return pharmacyRepositoryService.findAll()
|
||||
.stream()
|
||||
.map(this::convertToPharmacyDto)
|
||||
|
||||
Reference in New Issue
Block a user