|
18 | 18 | import hs.kr.backend.devpals.global.exception.CustomException; |
19 | 19 | import hs.kr.backend.devpals.global.exception.ErrorException; |
20 | 20 | import hs.kr.backend.devpals.global.jwt.JwtTokenValidator; |
| 21 | +import jakarta.annotation.PostConstruct; |
21 | 22 | import lombok.RequiredArgsConstructor; |
22 | 23 | import org.springframework.beans.factory.annotation.Qualifier; |
23 | 24 | import org.springframework.http.ResponseEntity; |
@@ -46,43 +47,42 @@ public class ProjectService { |
46 | 47 | @Qualifier("emailExecutor") |
47 | 48 | private final Executor emailExecutor; |
48 | 49 |
|
49 | | - private final Map<Long, ProjectAllDto> projectAllCache = new LinkedHashMap<>(); |
| 50 | + private final Map<Long, ProjectAllDto> projectAllCache = Collections.synchronizedMap(new LinkedHashMap<>()); |
50 | 51 |
|
51 | | - // 프로젝트 목록 조회 |
52 | | - @Transactional |
| 52 | + @PostConstruct |
| 53 | + public void initializeProjectCache() { |
| 54 | + List<ProjectEntity> projects = projectRepository.findAllByOrderByCreatedAtDesc(); |
| 55 | + for (ProjectEntity project : projects) { |
| 56 | + projectAllCache.put(project.getId(), convertToDto(project)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // 캐시에서 필터링만 수행 (isEmpty() 조건 제거) |
| 61 | + @Transactional(readOnly = true) |
53 | 62 | public ResponseEntity<ApiResponse<ProjectListResponse>> getProjectAll( |
54 | 63 | List<Long> skillTagId, Long positionTagId, |
55 | 64 | Long methodTypeId, Boolean isBeginner, |
56 | 65 | String keyword, int page) { |
57 | 66 |
|
58 | | - if (projectAllCache.isEmpty()) { |
59 | | - List<ProjectEntity> projects = projectRepository.findAllByOrderByCreatedAtDesc(); |
60 | | - projects.forEach(project -> { |
61 | | - if (!projectAllCache.containsKey(project.getId())) { |
62 | | - projectAllCache.put(project.getId(), convertToDto(project)); |
63 | | - } |
64 | | - }); |
| 67 | + List<ProjectAllDto> filteredProjectsAll; |
| 68 | + synchronized (projectAllCache) { |
| 69 | + filteredProjectsAll = projectAllCache.values().stream() |
| 70 | + .filter(project -> !isBeginner || project.getIsBeginner()) |
| 71 | + .filter(project -> keyword == null || keyword.isEmpty() || |
| 72 | + project.getTitle().toLowerCase().contains(keyword.toLowerCase()) || |
| 73 | + project.getDescription().toLowerCase().contains(keyword.toLowerCase())) |
| 74 | + .filter(project -> methodTypeId == null || methodTypeId <= 0L || |
| 75 | + Objects.equals(project.getMethodTypeId(), methodTypeId)) |
| 76 | + .filter(project -> positionTagId == null || positionTagId <= 0 || |
| 77 | + project.getPositionTagIds().contains(positionTagId)) |
| 78 | + .filter(project -> skillTagId == null || skillTagId.isEmpty() || |
| 79 | + project.getSkillTagIds().stream().anyMatch(skillTagId::contains)) |
| 80 | + .collect(Collectors.toList()); |
65 | 81 | } |
66 | 82 |
|
67 | | - // 전체 캐시에서 필터링 먼저 수행 |
68 | | - List<ProjectAllDto> filteredProjectsAll = projectAllCache.values().stream() |
69 | | - .filter(project -> !isBeginner || project.getIsBeginner()) |
70 | | - .filter(project -> keyword == null || keyword.isEmpty() || |
71 | | - project.getTitle().toLowerCase().contains(keyword.toLowerCase()) || |
72 | | - project.getDescription().toLowerCase().contains(keyword.toLowerCase())) |
73 | | - .filter(project -> methodTypeId == null || methodTypeId <= 0L || |
74 | | - Objects.equals(project.getMethodTypeId(), methodTypeId)) |
75 | | - .filter(project -> positionTagId == null || positionTagId <= 0 || |
76 | | - project.getPositionTagIds().contains(positionTagId)) |
77 | | - .filter(project -> skillTagId == null || skillTagId.isEmpty() || |
78 | | - project.getSkillTagIds().stream().anyMatch(skillTagId::contains)) |
79 | | - .collect(Collectors.toList()); |
80 | | - |
81 | | - // 필터링된 결과 기준으로 total, lastPage 계산 |
82 | 83 | int totalProjects = filteredProjectsAll.size(); |
83 | 84 | int lastPage = (int) Math.ceil((double) totalProjects / 12); |
84 | 85 |
|
85 | | - // 페이지네이션 적용 |
86 | 86 | List<ProjectAllDto> filteredProjects = filteredProjectsAll.stream() |
87 | 87 | .skip((page - 1) * 12) |
88 | 88 | .limit(12) |
|
0 commit comments