diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerQueryRepository.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerQueryRepository.kt index 9f8c586d..aac21c9b 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerQueryRepository.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerQueryRepository.kt @@ -113,30 +113,6 @@ class ExplorerQueryRepository( .fetchOne() } - fun getMemberDonationRankingTotal(creatorId: Long): Int { - val userMember = QMember("user") - - val donation = useCan.rewardCan.add(useCan.can).sum() - return queryFactory - .select(userMember.id) - .from(useCanCalculate) - .innerJoin(useCanCalculate.useCan, useCan) - .innerJoin(useCan.member, userMember) - .where( - useCan.isRefund.isFalse - .and(useCanCalculate.recipientCreatorId.eq(creatorId)) - .and( - useCan.canUsage.eq(CanUsage.DONATION) - .or(useCan.canUsage.eq(CanUsage.SPIN_ROULETTE)) - .or(useCan.canUsage.eq(CanUsage.LIVE)) - ) - ) - .groupBy(useCan.member.id) - .orderBy(donation.desc()) - .fetch() - .size - } - fun getMemberDonationRanking( creatorId: Long, limit: Long, diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerService.kt index 3cad7f4e..53551728 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/ExplorerService.kt @@ -403,6 +403,14 @@ class ExplorerService( val firstDayOfLastWeek = currentDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusDays(7) val firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth()) + val donationMemberTotal = donationRankingService.getMemberDonationRankingTotal(creatorId) + val donationRanking = donationRankingService.getMemberDonationRanking( + creatorId = creatorId, + offset = pageable.offset, + limit = pageable.pageSize.toLong(), + withDonationCan = creatorId == member.id!! + ) + return GetDonationAllResponse( accumulatedCansToday = if (creatorId == member.id!!) { queryRepository.getDonationCoinsDateRange( @@ -436,13 +444,8 @@ class ExplorerService( } else { false }, - totalCount = queryRepository.getMemberDonationRankingTotal(creatorId), - userDonationRanking = queryRepository.getMemberDonationRanking( - creatorId, - offset = pageable.offset, - limit = pageable.pageSize.toLong(), - withDonationCan = creatorId == member.id!! - ) + totalCount = donationMemberTotal, + userDonationRanking = donationRanking ) } diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingQueryRepository.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingQueryRepository.kt index c17abe2d..7ab5c2fa 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingQueryRepository.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingQueryRepository.kt @@ -13,6 +13,7 @@ import org.springframework.stereotype.Repository class CreatorDonationRankingQueryRepository(private val queryFactory: JPAQueryFactory) { fun getMemberDonationRanking( creatorId: Long, + offset: Long, limit: Long ): List { val donationCan = useCan.rewardCan.add(useCan.can).sum() @@ -38,12 +39,33 @@ class CreatorDonationRankingQueryRepository(private val queryFactory: JPAQueryFa .or(useCan.canUsage.eq(CanUsage.LIVE)) ) ) - .offset(0) + .offset(offset) .limit(limit) .groupBy(member.id) .orderBy(donationCan.desc(), member.id.desc()) .fetch() } + + fun getMemberDonationRankingTotal(creatorId: Long): Int { + return queryFactory + .select(member.id) + .from(useCanCalculate) + .innerJoin(useCanCalculate.useCan, useCan) + .innerJoin(useCan.member, member) + .where( + useCan.member.isActive.isTrue + .and(useCan.isRefund.isFalse) + .and(useCanCalculate.recipientCreatorId.eq(creatorId)) + .and( + useCan.canUsage.eq(CanUsage.DONATION) + .or(useCan.canUsage.eq(CanUsage.SPIN_ROULETTE)) + .or(useCan.canUsage.eq(CanUsage.LIVE)) + ) + ) + .groupBy(member.id) + .fetch() + .size + } } data class DonationRankingProjection @QueryProjection constructor( diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingService.kt index b7f5bee5..bb8bf903 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/CreatorDonationRankingService.kt @@ -20,18 +20,41 @@ class CreatorDonationRankingService( @Value("\${cloud.aws.cloud-front.host}") private val imageHost: String ) { + fun getMemberDonationRankingTotal(creatorId: Long): Int { + val cacheKey = "creator_donation_ranking_member_total_v2:$creatorId" + val cachedTotal = redisTemplate.opsForValue().get(cacheKey) as? Int + if (cachedTotal != null) { + return cachedTotal + } + + val total = repository.getMemberDonationRankingTotal(creatorId) + + val now = LocalDateTime.now() + val nextMonday = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).with(LocalTime.MIN) + val secondsUntilNextMonday = ChronoUnit.SECONDS.between(now, nextMonday) + + redisTemplate.opsForValue().set( + cacheKey, + total, + Duration.ofSeconds(secondsUntilNextMonday) + ) + + return total + } + fun getMemberDonationRanking( creatorId: Long, - limit: Long, + offset: Long = 0, + limit: Long = 10, withDonationCan: Boolean ): List { - val cacheKey = "creator_donation_ranking_v2:$creatorId:$limit:$withDonationCan" + val cacheKey = "creator_donation_ranking_v2:$creatorId:$offset:$limit:$withDonationCan" val cachedData = redisTemplate.opsForValue().get(cacheKey) as? MemberDonationRankingListResponse if (cachedData != null) { return cachedData.rankings } - val memberDonationRanking = repository.getMemberDonationRanking(creatorId, limit) + val memberDonationRanking = repository.getMemberDonationRanking(creatorId, offset, limit) val result = memberDonationRanking.map { MemberDonationRankingResponse( diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/live/room/LiveRoomService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/live/room/LiveRoomService.kt index 922e7e35..00fdbe1f 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/live/room/LiveRoomService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/live/room/LiveRoomService.kt @@ -888,9 +888,7 @@ class LiveRoomService( 3, withDonationCan = false ) - .asSequence() .map { it.userId } - .toList() } else { listOf() }