Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NAYB-154] feat : Redis에 있는 평균 별점과 리뷰 수를 주기적으로 DB 데이터와 맞춰준다. #118

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,37 @@ public void updateAverageRatingByItemId(
listOperations.rightPushAll(cacheKey, String.valueOf(dbAverageRating),
String.valueOf(dbNumberOfReviews));
}

public void synchronizeNumberOfReview(
final Long itemId,
final String cacheKey
) {
long dbCount = reviewRepository.countByItem_ItemId(itemId);

numberOfReviewsRedisTemplate.opsForValue().set(cacheKey, dbCount);
}

public void synchronizeAverageRating(
final Long itemId,
final String cacheKey
) {
String averageRating = listOperations.index(cacheKey, 0);
String totalNumberOfReviews = listOperations.index(cacheKey, 1);

if (averageRating != null && totalNumberOfReviews != null) {
double dbAverageRating = reviewRepository.findAverageRatingByItemId(itemId);
long dbNumberOfReviews = reviewRepository.countByItem_ItemId(itemId);

listOperations.set(cacheKey, 0, String.valueOf(dbAverageRating));
listOperations.set(cacheKey, 1, String.valueOf(dbNumberOfReviews));

return;
}

double dbAverageRating = reviewRepository.findAverageRatingByItemId(itemId);
long dbNumberOfReviews = reviewRepository.countByItem_ItemId(itemId);

listOperations.rightPushAll(cacheKey, String.valueOf(dbAverageRating),
String.valueOf(dbNumberOfReviews));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.prgrms.nabmart.domain.review.service;

import com.prgrms.nabmart.domain.review.repository.ReviewRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ReviewSchedulerService {

private final RedisCacheService redisCacheService;
private final ReviewRepository reviewRepository;

private static final String AVERAGE_RATE_CACHE_KEY = "averageRating:Item:";
private static final String REVIEW_COUNT_CACHE_KEY = "reviewCount:Item:";

@Scheduled(cron = "0 0 * * 1 *")
public void synchronizeAverageRating() {
reviewRepository.findAll()
.forEach(review -> redisCacheService.synchronizeAverageRating(
review.getItem().getItemId(),
AVERAGE_RATE_CACHE_KEY + review.getItem().getItemId()
));
}

@Scheduled(cron = "0 0 * * 1 *")
public void synchronizeNumberOfReview() {
reviewRepository.findAll()
.forEach(
review -> redisCacheService.synchronizeNumberOfReview(
review.getItem().getItemId(),
REVIEW_COUNT_CACHE_KEY + review.getItem().getItemId()
)
);
}
}