Skip to content

Commit

Permalink
feat : kuddy top5를 추출 정보 캐싱처리
Browse files Browse the repository at this point in the history
1일에 1번 순위 업데이트를 하도록 설정
하루 안에 프로필(닉네임, 프로필사진)이 변경될 경우 캐시도 변경되도록 변경
Related to: #134
  • Loading branch information
june0216 committed Oct 6, 2023
1 parent 76b71c7 commit 110f43e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public ResponseEntity<StatusResponse> readKuddyProfile(@RequestParam(value = "pa

@GetMapping("kuddy/top5")
public ResponseEntity<StatusResponse> readTop5KuddyProfile() {
List<Profile> profileList = top5KuddyService.findTopKuddies();
Top5KuddyListResDto response = top5KuddyService.changePageToResponse(profileList);
Top5KuddyListResDto response = top5KuddyService.getTop5Kuddy();
return ResponseEntity.ok(StatusResponse.builder()
.status(StatusEnum.OK.getStatusCode())
.message(StatusEnum.OK.getCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Top5KuddyListResDto {
private List<Top5KuddyResDto> top5KuddyList;
private long size;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public static class Top5KuddyResDto{
private Long memberId;
Expand All @@ -37,6 +37,13 @@ public static Top5KuddyResDto from(Member member, String recentReview){
.kuddyLevel(member.getProfile().getKuddyLevel())
.build();
}

public void setNickname(String newNickname){
this.nickname = newNickname;
}
public void setProfileImageUrl(String profileImageUrl){
this.profileImageUrl = profileImageUrl;
}
}

public static Top5KuddyListResDto of(List<Top5KuddyResDto> top5KuddyList){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import java.util.stream.Collectors;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

Expand Down Expand Up @@ -46,7 +45,8 @@ public class ProfileService {
private final MemberService memberService;
private final ProfileAreaService profileAreaService;
private final ProfileQueryService profileQueryService;
private final JPAQueryFactory queryFactory;
private final Top5KuddyService top5KuddyService;


public Long create(Member member, ProfileReqDto.Create reqDto){
if(existsProfileByMember(member)){
Expand Down Expand Up @@ -81,9 +81,11 @@ public Profile update(Member member, ProfileReqDto.Update reqDto) {
setInterests(profile, reqDto.getInterests());
profileLanguageService.updateProfileLanguage(profile, reqDto.getAvailableLanguages());
profileAreaService.updateProfileDistricts(profile, reqDto.getDistricts());
top5KuddyService.updateTop5KuddiesCache(member);
return profile;
}


public void setInterests(Profile profile, InterestsDto reqDto){

profile.setActivitiesInvestmentTechs(reqDto.getActivitiesInvestmentTech());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -36,8 +38,8 @@
public class Top5KuddyService {
private final ReviewRepository reviewRepository;
private final JPAQueryFactory queryFactory;
private final CacheManager contentCacheManager;

@Cacheable(value="topKuddies",cacheManager = "contentCacheManager")
public List<Profile> findTopKuddies() {
// 점수 계산
NumberExpression<Integer> reviewScore = new CaseBuilder()
Expand Down Expand Up @@ -74,7 +76,9 @@ public List<Profile> findTopKuddies() {
.collect(Collectors.toList());
}

public Top5KuddyListResDto changePageToResponse(List<Profile> profiles){
@Cacheable(value="topKuddies",cacheManager = "contentCacheManager")
public Top5KuddyListResDto getTop5Kuddy(){
List<Profile> profiles = findTopKuddies(); // 캐시에 데이터가 없으면 실행
List<Top5KuddyListResDto.Top5KuddyResDto> resDtos = profiles.stream()
.map(profile -> {
Member member = profile.getMember();
Expand All @@ -88,7 +92,26 @@ public Top5KuddyListResDto changePageToResponse(List<Profile> profiles){
@CacheEvict(value = "topKuddies", allEntries = true,cacheManager = "contentCacheManager" )
@Scheduled(cron = "0 0 12 * * ?") // 매일 오후 12시에 실행
public void refreshTopKuddies() {
findTopKuddies();
getTop5Kuddy();
}

public void updateTop5KuddiesCache(Member member){
Top5KuddyListResDto cachedData = getTop5Kuddy();
for(Top5KuddyListResDto.Top5KuddyResDto kuddy : cachedData.getTop5KuddyList()) {
if(kuddy.getMemberId().equals(member.getId())) {
kuddy.setNickname(member.getNickname());
kuddy.setProfileImageUrl(member.getProfileImageUrl());
updateCache(cachedData); // 변경된 정보로 캐시 업데이트
break;
}
}
}

private void updateCache(Top5KuddyListResDto updatedData) {
Cache cache = contentCacheManager.getCache("topKuddies");
if (cache != null) {
cache.put("topKuddies", updatedData);
}
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CacheManager contentCacheManager(RedisConnectionFactory cf) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) // Value Serializer 변경
.entryTtl(Duration.ofHours(10L));
.entryTtl(Duration.ofDays(1L));

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(cf).cacheDefaults(redisCacheConfiguration).build();
}
Expand Down

0 comments on commit 110f43e

Please sign in to comment.