Redis cache function upgrade
This commit is contained in:
@@ -7,7 +7,10 @@ import com.kamco.cd.kamcoback.code.dto.CommonCodeDto.OrderReq;
|
|||||||
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
|
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
|
||||||
import com.kamco.cd.kamcoback.postgres.core.CommonCodeCoreService;
|
import com.kamco.cd.kamcoback.postgres.core.CommonCodeCoreService;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -23,6 +26,7 @@ public class CommonCodeService {
|
|||||||
*
|
*
|
||||||
* @return 모튼 코드 정보
|
* @return 모튼 코드 정보
|
||||||
*/
|
*/
|
||||||
|
@Cacheable(value = "commonCodes")
|
||||||
public List<Basic> getFindAll() {
|
public List<Basic> getFindAll() {
|
||||||
return commonCodeCoreService.findAll();
|
return commonCodeCoreService.findAll();
|
||||||
}
|
}
|
||||||
@@ -44,6 +48,7 @@ public class CommonCodeService {
|
|||||||
* @return 생성된 코드 id
|
* @return 생성된 코드 id
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ApiResponseDto.ResponseObj save(AddReq req) {
|
public ApiResponseDto.ResponseObj save(AddReq req) {
|
||||||
return commonCodeCoreService.save(req);
|
return commonCodeCoreService.save(req);
|
||||||
}
|
}
|
||||||
@@ -55,6 +60,7 @@ public class CommonCodeService {
|
|||||||
* @param req 수정요청 정보
|
* @param req 수정요청 정보
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ApiResponseDto.ResponseObj update(Long id, ModifyReq req) {
|
public ApiResponseDto.ResponseObj update(Long id, ModifyReq req) {
|
||||||
return commonCodeCoreService.update(id, req);
|
return commonCodeCoreService.update(id, req);
|
||||||
}
|
}
|
||||||
@@ -65,6 +71,7 @@ public class CommonCodeService {
|
|||||||
* @param id 코드 아이디
|
* @param id 코드 아이디
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ApiResponseDto.ResponseObj removeCode(Long id) {
|
public ApiResponseDto.ResponseObj removeCode(Long id) {
|
||||||
return commonCodeCoreService.removeCode(id);
|
return commonCodeCoreService.removeCode(id);
|
||||||
}
|
}
|
||||||
@@ -75,6 +82,7 @@ public class CommonCodeService {
|
|||||||
* @param req id, order 정보를 가진 List
|
* @param req id, order 정보를 가진 List
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ApiResponseDto.ResponseObj updateOrder(OrderReq req) {
|
public ApiResponseDto.ResponseObj updateOrder(OrderReq req) {
|
||||||
return commonCodeCoreService.updateOrder(req);
|
return commonCodeCoreService.updateOrder(req);
|
||||||
}
|
}
|
||||||
@@ -99,4 +107,15 @@ public class CommonCodeService {
|
|||||||
public ApiResponseDto.ResponseObj getCodeCheckDuplicate(Long parentId, String code) {
|
public ApiResponseDto.ResponseObj getCodeCheckDuplicate(Long parentId, String code) {
|
||||||
return commonCodeCoreService.getCodeCheckDuplicate(parentId, code);
|
return commonCodeCoreService.getCodeCheckDuplicate(parentId, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 공통코드 이름 조회
|
||||||
|
*
|
||||||
|
* @param parentCodeCd 상위 코드
|
||||||
|
* @param childCodeCd 하위 코드
|
||||||
|
* @return 공통코드명
|
||||||
|
*/
|
||||||
|
public Optional<String> getCode(String parentCodeCd, String childCodeCd) {
|
||||||
|
return commonCodeCoreService.getCode(parentCodeCd, childCodeCd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
package com.kamco.cd.kamcoback.code.util;
|
||||||
|
|
||||||
|
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto.Basic;
|
||||||
|
import com.kamco.cd.kamcoback.code.service.CommonCodeService;
|
||||||
|
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 공통코드 조회 유틸리티 클래스
|
||||||
|
*
|
||||||
|
* <p>애플리케이션 전역에서 공통코드를 조회하기 위한 유틸리티입니다. Redis 캐시를 통해 성능을 최적화합니다.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CommonCodeUtil {
|
||||||
|
|
||||||
|
private final CommonCodeService commonCodeService;
|
||||||
|
|
||||||
|
public CommonCodeUtil(CommonCodeService commonCodeService) {
|
||||||
|
this.commonCodeService = commonCodeService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 모든 공통코드 조회
|
||||||
|
*
|
||||||
|
* @return 캐시된 모든 공통코드 목록
|
||||||
|
*/
|
||||||
|
public List<Basic> getAllCommonCodes() {
|
||||||
|
try {
|
||||||
|
return commonCodeService.getFindAll();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("공통코드 전체 조회 중 오류 발생", e);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 특정 코드로 공통코드 조회
|
||||||
|
*
|
||||||
|
* @param code 코드값
|
||||||
|
* @return 해당 코드의 공통코드 목록
|
||||||
|
*/
|
||||||
|
public List<Basic> getCommonCodesByCode(String code) {
|
||||||
|
if (code == null || code.isEmpty()) {
|
||||||
|
log.warn("유효하지 않은 코드: {}", code);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return commonCodeService.findByCode(code);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("코드 기반 공통코드 조회 중 오류 발생: {}", code, e);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 특정 ID로 공통코드 단건 조회
|
||||||
|
*
|
||||||
|
* @param id 공통코드 ID
|
||||||
|
* @return 조회된 공통코드
|
||||||
|
*/
|
||||||
|
public Optional<Basic> getCommonCodeById(Long id) {
|
||||||
|
if (id == null || id <= 0) {
|
||||||
|
log.warn("유효하지 않은 ID: {}", id);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return Optional.of(commonCodeService.getOneById(id));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("ID 기반 공통코드 조회 중 오류 발생: {}", id, e);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 상위 코드와 하위 코드로 공통코드명 조회
|
||||||
|
*
|
||||||
|
* @param parentCode 상위 코드
|
||||||
|
* @param childCode 하위 코드
|
||||||
|
* @return 공통코드명
|
||||||
|
*/
|
||||||
|
public Optional<String> getCodeName(String parentCode, String childCode) {
|
||||||
|
if (parentCode == null || parentCode.isEmpty() || childCode == null || childCode.isEmpty()) {
|
||||||
|
log.warn("유효하지 않은 코드: parentCode={}, childCode={}", parentCode, childCode);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return commonCodeService.getCode(parentCode, childCode);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("코드명 조회 중 오류 발생: parentCode={}, childCode={}", parentCode, childCode, e);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 상위 코드를 기반으로 하위 코드 조회
|
||||||
|
*
|
||||||
|
* @param parentCode 상위 코드
|
||||||
|
* @return 해당 상위 코드의 하위 공통코드 목록
|
||||||
|
*/
|
||||||
|
public List<Basic> getChildCodesByParentCode(String parentCode) {
|
||||||
|
if (parentCode == null || parentCode.isEmpty()) {
|
||||||
|
log.warn("유효하지 않은 상위 코드: {}", parentCode);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<Basic> allCodes = commonCodeService.getFindAll();
|
||||||
|
return allCodes.stream()
|
||||||
|
.filter(code -> parentCode.equals(code.getCode()))
|
||||||
|
.findFirst()
|
||||||
|
.map(Basic::getChildren)
|
||||||
|
.orElse(List.of());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("상위 코드 기반 하위 코드 조회 중 오류 발생: {}", parentCode, e);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 코드 사용 가능 여부 확인
|
||||||
|
*
|
||||||
|
* @param parentId 상위 코드 ID
|
||||||
|
* @param code 확인할 코드값
|
||||||
|
* @return 사용 가능 여부 (true: 사용 가능, false: 중복 또는 오류)
|
||||||
|
*/
|
||||||
|
public boolean isCodeAvailable(Long parentId, String code) {
|
||||||
|
if (parentId == null || parentId <= 0 || code == null || code.isEmpty()) {
|
||||||
|
log.warn("유효하지 않은 입력: parentId={}, code={}", parentId, code);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ApiResponseDto.ResponseObj response = commonCodeService.getCodeCheckDuplicate(parentId, code);
|
||||||
|
// ResponseObj의 flag 필드를 통해 SUCCESS/FAIL 확인
|
||||||
|
return response.getFlag() != null
|
||||||
|
&& response.getFlag().equals(ApiResponseDto.SuccFailCode.SUCCESS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("코드 중복 확인 중 오류 발생: parentId={}, code={}", parentId, code, e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,8 @@ import jakarta.persistence.EntityNotFoundException;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -28,6 +30,7 @@ public class CommonCodeCoreService
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@Cacheable(value = "commonCodes")
|
||||||
public List<CommonCodeDto.Basic> findAll() {
|
public List<CommonCodeDto.Basic> findAll() {
|
||||||
return commonCodeRepository.findByAll().stream().map(CommonCodeEntity::toDto).toList();
|
return commonCodeRepository.findByAll().stream().map(CommonCodeEntity::toDto).toList();
|
||||||
}
|
}
|
||||||
@@ -38,6 +41,7 @@ public class CommonCodeCoreService
|
|||||||
* @param req
|
* @param req
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ResponseObj save(CommonCodeDto.AddReq req) {
|
public ResponseObj save(CommonCodeDto.AddReq req) {
|
||||||
|
|
||||||
String regex = "^[A-Z0-9_]+$";
|
String regex = "^[A-Z0-9_]+$";
|
||||||
@@ -88,6 +92,7 @@ public class CommonCodeCoreService
|
|||||||
* @param req
|
* @param req
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ResponseObj update(Long id, CommonCodeDto.ModifyReq req) {
|
public ResponseObj update(Long id, CommonCodeDto.ModifyReq req) {
|
||||||
CommonCodeEntity found =
|
CommonCodeEntity found =
|
||||||
commonCodeRepository
|
commonCodeRepository
|
||||||
@@ -111,6 +116,7 @@ public class CommonCodeCoreService
|
|||||||
* @param req
|
* @param req
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ResponseObj updateOrder(CommonCodeDto.OrderReq req) {
|
public ResponseObj updateOrder(CommonCodeDto.OrderReq req) {
|
||||||
|
|
||||||
CommonCodeEntity found =
|
CommonCodeEntity found =
|
||||||
@@ -145,6 +151,7 @@ public class CommonCodeCoreService
|
|||||||
* @param id
|
* @param id
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
@CacheEvict(value = "commonCodes", allEntries = true)
|
||||||
public ResponseObj removeCode(Long id) {
|
public ResponseObj removeCode(Long id) {
|
||||||
CommonCodeEntity entity =
|
CommonCodeEntity entity =
|
||||||
commonCodeRepository
|
commonCodeRepository
|
||||||
|
|||||||
Reference in New Issue
Block a user