Skip to content

Commit

Permalink
refactor : /v1/customers/latest 성능 개성
Browse files Browse the repository at this point in the history
- repository에 캐시 사용
- N:1 관계인 테이블을 지연로딩으로 변경

issue #189
  • Loading branch information
kung036 committed Oct 16, 2024
1 parent 754e27d commit 303484d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ga/backend/customer/entity/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@Entity
@Getter
@Setter
@Cacheable
public class Customer extends Auditable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -44,7 +45,7 @@ public class Customer extends Auditable {
@JoinColumn(name = "dong2_pk")
private Dong2 dong2;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_pk")
private Employee employee;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import ga.backend.customerType.entity.CustomerType;
import ga.backend.employee.entity.Employee;
import ga.backend.customer.entity.ConsultationStatus;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -20,6 +22,8 @@ public interface CustomerRepository extends JpaRepository<Customer, Long> {
Optional<Customer> findByPkAndDelYnFalse(long customerPk);

// 최신순
@Cacheable(value = "customers", key = "#employee.pk")
@EntityGraph(attributePaths = {"customerType", "li", "dong2", "gu2", "metro2"})
List<Customer> findByEmployeeAndDelYnFalse(Employee employee, Sort sort);
List<Customer> findByEmployeeAndCustomerTypeAndDelYnFalse(Employee employee, CustomerType customerType, Sort sort);
List<Customer> findAllByEmployeeAndRegisterDateBetweenAndCustomerTypeInAndDelYnFalse(Employee employee, Sort sort, LocalDate start, LocalDate finish, List<CustomerType> customerTypes);
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/ga/backend/customer/service/CustomerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import ga.backend.util.FindEmployee;
import ga.backend.util.InitialCustomerTypeNull;
import lombok.AllArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

Expand All @@ -46,10 +48,13 @@ public class CustomerService {

private final FindEmployee findEmployee;
private final FindCoordinateByKakaoMap findCoordinateByKakaoMap;
private final CacheManager cacheManager;

// CREATE
public Customer createCustomer(Customer customer, CustomerRequestDto.MetroGuDong metroGuDong) {
// 캐시 항목 삭제
Employee employee = findEmployee.getLoginEmployeeByToken();
cacheManager.getCache("customers").evict(employee.getPk());
customer.setEmployee(employee);

// liPk를 이용한 dongString 자동 설정
Expand Down Expand Up @@ -97,7 +102,10 @@ public Customer createCustomer(Customer customer, CustomerRequestDto.MetroGuDong
// 여러 명의 custemer 생성
@Transactional
public List<Customer> createCustomers(List<Customer> customers) {
// 캐시 항목 삭제
Employee employee = findEmployee.getLoginEmployeeByToken();
cacheManager.getCache("customers").evict(employee.getPk());

List<CustomerType> customerTypes = customerTypeService.findCustomerTypeByCompanyFromEmployee(employee); // 고객의 customerType

// NULL 유형의 고객유형
Expand Down Expand Up @@ -714,9 +722,12 @@ public List<Customer> findCustomerByName(String name) {

// UPDATE
public Customer patchCustomer(Customer customer, CustomerRequestDto.MetroGuDong metroGuDong, Long customerTypePk) {
Customer findCustomer = verifiedCustomer(customer.getPk());
// 캐시 항목 삭제
Employee employee = findEmployee.getLoginEmployeeByToken();
cacheManager.getCache("customers").evict(employee.getPk());

// 직원 유효성 검사
Customer findCustomer = verifiedCustomer(customer.getPk());
if (findCustomer.getEmployee().getPk() != employee.getPk())
throw new BusinessLogicException(ExceptionCode.EMPLOYEE_NOT_CONTAIN_CUSTOMER);

Expand Down Expand Up @@ -786,7 +797,16 @@ public Customer patchCustomer(Customer customer, CustomerRequestDto.MetroGuDong

// delYn=true 변경 후 customer과 관련된 schedule.delYn=true로 변경
public void deleteCustomer(long customerPk) {
// 캐시 항목 삭제
Employee employee = findEmployee.getLoginEmployeeByToken();
cacheManager.getCache("customers").evict(employee.getPk());

Customer customer = verifiedCustomer(customerPk);

// 직원 유효성 검사
if(customer.getEmployee().getPk() != employee.getPk())
throw new BusinessLogicException(ExceptionCode.EMPLOYEE_NOT_CONTAIN_CUSTOMER);

customer.setDelYn(true);
changeSchduleDelYnTrue(customer);
customerRepository.save(customer);
Expand Down

0 comments on commit 303484d

Please sign in to comment.