Skip to content

Commit

Permalink
refactor : /v1/employee 성능 개성
Browse files Browse the repository at this point in the history
- repository에 캐시 사용
- N+1문제 해결 -> @entitygraph를 사용해서 즉시로딩
- N:1 관계인 Team을 지연로딩으로 변경
- 쓸모없는 컬럼제거
- email 인덱스 생성

issue #189
  • Loading branch information
kung036 committed Oct 16, 2024
1 parent ada083b commit 4e21a37
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/ga/backend/BackendApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.joda.time.DateTime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

Expand All @@ -12,6 +13,7 @@
@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
@EnableCaching
public class BackendApplication {

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import ga.backend.team.repository.TeamRepository;
import ga.backend.util.Version;
import lombok.AllArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -73,11 +76,17 @@ public ResponseEntity getEmployee(@Positive @PathVariable("employee-pk") long em

// 토큰으로 employee 정보 조회하기
@GetMapping
// @Cacheable(value = "employeeCache")
public ResponseEntity getEmployee() {
Employee employee = employeeService.findEmployeeByToken();
EmployeeResponseDto.Response response = employeeMapper.employeeToEmployeeResponseDto(employee);
TeamResponseDto.Response teamResponse = teamMapper.teamToTeamResponseDto(employee.getTeam(), employee.getCompany().getPk());
response.setTeamResponseDto(teamResponse);
EmployeeResponseDto.SimpleResponse response = employeeMapper.employeeToEmployeeSimpleResponseDto(employee);

// ResponseDto.Response 반환
// EmployeeResponseDto.Response response = employeeMapper.employeeToEmployeeSimpleResponseDto(employee);
// if(employee.getTeam() != null) {
// TeamResponseDto.Response teamResponse = teamMapper.teamToTeamResponseDto(employee.getTeam(), employee.getCompany().getPk());
// response.setTeamResponseDto(teamResponse);
// }

return new ResponseEntity<>(response, HttpStatus.OK);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ga/backend/employee/dto/EmployeeResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public static class Response {
private LocalDateTime modifiedAt;
}

@AllArgsConstructor
@Setter
@Getter
public static class SimpleResponse {
private Long pk;
private String id; // 사번
private Long KakaoId; // 카카오톡 ID
private String email;
private String name; // 이름
private Long year; // 연차
private boolean regiYn;
}

@AllArgsConstructor
@Setter
@Getter
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ga/backend/employee/entity/Employee.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ga.backend.employee.entity;

//import ga.backend.analysis.entity.Analysis;
import ga.backend.analysis.entity.Analysis;
import ga.backend.auditable.Auditable;
import ga.backend.company.entity.Company;
Expand All @@ -24,6 +23,7 @@
@Getter
@Setter
@NoArgsConstructor
@Cacheable
public class Employee extends Auditable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -71,7 +71,7 @@ public class Employee extends Auditable {
@JoinColumn(name = "company_pk")
private Company company;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_pk")
private Team team;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface EmployeeMapper {
Employee employeePatchDtoToEmployee(EmployeeRequestDto.Patch patch);
Employee employeeChangePasswordToEmployee(EmployeeRequestDto.ChangePassword changePassword);
EmployeeResponseDto.Response employeeToEmployeeResponseDto(Employee employee);
EmployeeResponseDto.SimpleResponse employeeToEmployeeSimpleResponseDto(Employee employee);

List<EmployeeResponseDto.Response> employeeToEmployeeListResponseDto(List<Employee> employees);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import ga.backend.employee.entity.Employee;
import ga.backend.team.entity.Team;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -10,6 +12,8 @@

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Cacheable(value = "employees", key = "#email")
@EntityGraph(attributePaths = {"company", "roles"})
Optional<Employee> findByEmailAndDelYnFalse(String email);
List<Employee> findFistByIdAndDelYnFalse(String id);
Optional<Employee> findByPkAndDelYnFalse(long pk);
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/ga/backend/employee/service/EmployeeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@
import ga.backend.team.service.TeamService;
import ga.backend.util.FindEmployee;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;

@Service
@AllArgsConstructor
Expand Down Expand Up @@ -112,9 +118,7 @@ public Employee findEmployeeByPk(long employeePk) {

// 토큰으로 직원 조회
public Employee findEmployeeByToken() {
Employee employee = findEmployee.getLoginEmployeeByToken();

return employee;
return findEmployee.getLoginEmployeeByToken();
}

// UPDATE
Expand Down

0 comments on commit 4e21a37

Please sign in to comment.