Skip to content

Commit

Permalink
feat: 인기 아카이브에 실시간성 부여
Browse files Browse the repository at this point in the history
  • Loading branch information
AnTaeho committed Jan 2, 2025
1 parent b423b20 commit 7a02891
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.palettee.archive.repository;

import com.palettee.archive.domain.Archive;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -32,7 +36,7 @@ public void settleHits() {
}

for (String incrKey : incrKeys) {
String archiveId = incrKey.split(DELIMITER)[3];
String archiveId = extractId(incrKey);

String incrHits = redisTemplate.opsForValue().get(incrKey);
long incrCount = incrHits == null ? 0 : Long.parseLong(incrHits);
Expand All @@ -50,11 +54,44 @@ public void settleHits() {
}
}

private String extractId(String incrKey) {
return incrKey.split(DELIMITER)[3];
}

@Transactional(readOnly = true)
public void updateArchiveList() {
List<Archive> result = archiveRepository.findTopArchives();
List<Long> top4IncrKeys = getTop4IncrKeys();
List<Archive> result = archiveRepository.findArchivesInIds(top4IncrKeys);

int redisSize = result.size();
if (redisSize < 4) {
int remaining = 4 - redisSize;
List<Archive> additionalFromDB = archiveRepository.findTopArchives(remaining);
result.addAll(additionalFromDB);
}

redisTemplateForArchive.opsForSet().remove(TOP_ARCHIVE);
redisTemplateForArchive.opsForValue().set(TOP_ARCHIVE, result, 1, TimeUnit.MINUTES);
redisTemplateForArchive.opsForValue().set(TOP_ARCHIVE, result, 1, TimeUnit.HOURS);
}

private List<Long> getTop4IncrKeys() {
Set<String> incrKeys = redisTemplate.keys(INCR_PATTERN);
if (incrKeys == null || incrKeys.isEmpty()) {
return Collections.emptyList();
}

return incrKeys.stream()
.map(key -> new AbstractMap.SimpleEntry<>(key, getValueAsLong(key)))
.sorted((e1, e2) -> Long.compare(e2.getValue(), e1.getValue()))
.limit(4)
.map(Map.Entry::getKey)
.map(it -> getValueAsLong(extractId(it)))
.collect(Collectors.toList());
}

private Long getValueAsLong(String key) {
String value = redisTemplate.opsForValue().get(key);
return value == null ? 0L : Long.parseLong(value);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface ArchiveRepository extends JpaRepository<Archive, Long>, Archive
@Query("UPDATE Archive a SET a.hits = :hitCount WHERE a.id = :archiveId")
void updateHitCount(@Param("archiveId") Long archiveId, @Param("hitCount") Long hitCount);

@Query("select a from Archive a order by (a.hits + a.likeCount * 5) desc, a.id desc limit 4")
List<Archive> findTopArchives();
@Query("select a from Archive a order by (a.hits + a.likeCount * 5) desc, a.id desc limit :limit")
List<Archive> findTopArchives(@Param("limit") int limit);

@Query("select a from Archive a where a.id in :ids")
List<Archive> findArchivesInIds(@Param("ids") List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class ArchiveScheduler {

@Scheduled(cron = "0 0 * * * *")
public void updateMainArchive() {
archiveRedisRepository.settleHits();
archiveRedisRepository.updateArchiveList();
archiveRedisRepository.settleHits();
}

}

0 comments on commit 7a02891

Please sign in to comment.