Skip to content

Commit

Permalink
fix: 조회수 카운팅 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
AnTaeho committed Dec 30, 2024
1 parent 2e66899 commit 89dd1db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ArchiveDetailResponse getArchive(@PathVariable("archiveId") long archiveI

@GetMapping("/main")
public ArchiveListResponse getMainArchiveList() {
return archiveService.getMainArchive(getContextUser());
return archiveService.getMainArchive();
}

@GetMapping
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/palettee/archive/event/HitEventListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.palettee.archive.event;

import java.time.Duration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
Expand All @@ -13,24 +14,23 @@ public class HitEventListener {

private static final String SET_KEY_PREFIX = "hit:archiveId:";
private static final String VALUE_KEY_PREFIX = "incr:hit:archiveId:";
private static final String ZERO = "0";

private final RedisTemplate<String, String> redisTemplate;

@EventListener(value = HitEvent.class)
public void onHit(HitEvent event) {

Long add = redisTemplate.opsForSet().add(SET_KEY_PREFIX + event.archiveId() + event.email(), event.email());
if (add == null || add != 1L) {
String setKey = SET_KEY_PREFIX + event.archiveId();
Long addResult = redisTemplate.opsForSet().add(setKey, event.email());

if (addResult == null || addResult != 1L) {
return;
}

String currentHits = redisTemplate.opsForValue().get(VALUE_KEY_PREFIX + event.archiveId());
if (currentHits == null) {
redisTemplate.opsForValue().set(VALUE_KEY_PREFIX + event.archiveId(), ZERO);
}
redisTemplate.expire(setKey, Duration.ofHours(1));

redisTemplate.opsForValue().increment(VALUE_KEY_PREFIX + event.archiveId());
String valueKey = VALUE_KEY_PREFIX + event.archiveId();
redisTemplate.opsForValue().increment(valueKey);
}

}

0 comments on commit 89dd1db

Please sign in to comment.