Skip to content

Commit

Permalink
fix: redis 코드는 냅두고 기존 코드 다시 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
wjddn2165 committed Aug 22, 2024
1 parent b6637de commit 6b5faef
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import JGS.CasperEvent.domain.event.repository.eventRepository.RushEventRepository;
import JGS.CasperEvent.domain.event.repository.eventRepository.RushOptionRepository;
import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository;
import JGS.CasperEvent.domain.event.service.redisService.RushEventRedisService;
import JGS.CasperEvent.global.entity.BaseUser;
import JGS.CasperEvent.global.enums.CustomErrorCode;
import JGS.CasperEvent.global.enums.Position;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.util.RepositoryErrorHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -31,6 +31,7 @@ public class RushEventService {
private final RushParticipantsRepository rushParticipantsRepository;
private final RushOptionRepository rushOptionRepository;
private final EventCacheService eventCacheService;
private final RushEventRedisService rushEventRedisService;

@Transactional
public RushEventListResponseDto getAllRushEvents() {
Expand Down Expand Up @@ -83,6 +84,9 @@ public void apply(BaseUser user, int optionId) {
// eventId 를 이용하여 rushEvent 를 꺼냄
RushEvent rushEvent = RepositoryErrorHandler.findByIdOrElseThrow(rushEventRepository, todayEventId, CustomErrorCode.NO_RUSH_EVENT);

// redis incr 호출
// rushEventRedisService.incrementOptionCount(todayEventId, optionId);

// 새로운 RushParticipants 를 생성하여 DB 에 저장
RushParticipants rushParticipants = new RushParticipants(user, rushEvent, optionId);
rushParticipantsRepository.save(rushParticipants);
Expand All @@ -93,9 +97,14 @@ public RushEventRateResponseDto getRushEventRate(BaseUser user) {
LocalDate today = LocalDate.now();
Long todayEventId = eventCacheService.getTodayEvent(today).rushEventId();
Optional<Integer> optionId = rushParticipantsRepository.getOptionIdByUserId(user.getPhoneNumber());

long leftOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 1);
long rightOptionCount = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(todayEventId, 2);

// redis 에 캐싱 값 가져옴
// long leftOptionCount = rushEventRedisService.getOptionCount(todayEventId, 1);
// long rightOptionCount = rushEventRedisService.getOptionCount(todayEventId, 2);

return new RushEventRateResponseDto(
optionId.orElseThrow(() -> new CustomException("유저가 응모한 선택지가 존재하지 않습니다.", CustomErrorCode.USER_NOT_FOUND)),
leftOptionCount, rightOptionCount);
Expand Down Expand Up @@ -227,6 +236,7 @@ public void setRushEvents() {

eventCacheService.setCacheValue(LocalDate.now());
eventCacheService.setAllRushEvent();
rushEventRedisService.clearAllrushEventRate();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package JGS.CasperEvent.domain.event.service.redisService;

import JGS.CasperEvent.domain.event.repository.participantsRepository.RushParticipantsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@RequiredArgsConstructor
public class RushEventRedisService {

private final RedisTemplate<String, String> redisTemplate;
private final RushParticipantsRepository rushParticipantsRepository;

public Long getOptionCount(Long eventId, int optionId) {
String redisKey = getRedisKey(eventId, optionId);

// Redis에서 값 조회
String cachedCount = redisTemplate.opsForValue().get(redisKey);

if (cachedCount != null) {
// Redis에 값이 있으면 캐시된 값 반환
return Long.valueOf(cachedCount);
} else {
// Redis에 값이 없으면 DB에서 값 조회
long countFromDb = rushParticipantsRepository.countByRushEvent_RushEventIdAndOptionId(eventId, optionId);

// 조회한 값을 Redis에 저장
redisTemplate.opsForValue().set(redisKey, String.valueOf(countFromDb));

return countFromDb;
}
}

public void incrementOptionCount(Long eventId, int optionId) {
String redisKey = getRedisKey(eventId, optionId);

// Redis에서 해당 키의 값을 증가시킴
redisTemplate.opsForValue().increment(redisKey);
}

private String getRedisKey(Long eventId, int optionId) {
return "rushEvent:" + eventId + ":option:" + optionId;
}

public void clearAllrushEventRate() {
// 특정 rushEventId에 대한 모든 옵션 키들을 가져와 삭제
String pattern = "rushEvent:*" + ":option:*";
Set<String> keys = redisTemplate.keys(pattern);
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
}

0 comments on commit 6b5faef

Please sign in to comment.