Skip to content

Commit

Permalink
fix: Redis 캐스팅 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dlrkdus committed Aug 9, 2024
1 parent e849dfd commit e4fa85b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ public void dumpToDB() {
}
}

@Transactional
@Transactional(rollbackFor = Exception.class)
public void migrateDB() {
log.info("데이터 이전을 시작합니다.");
try {
List<Follow> follows = followListRedisRepository.getAllFollows();
followRepository.deleteAll();
followRepository.saveAll(follows);
log.info("Redis 데이터를 RDBMS로 옮기고 Redis를 초기화했습니다.");
}
catch (Exception e) {
log.error("데이터 이전에 실패했습니다.",e);
}
log.info("Redis 데이터를 RDBMS로 옮기고 Redis를 초기화했습니다.");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import com.goormy.hackathon.entity.Follow;
import com.goormy.hackathon.entity.Hashtag;
import com.goormy.hackathon.entity.User;
import com.goormy.hackathon.repository.JPA.FollowRepository;
import com.goormy.hackathon.repository.JPA.HashtagRepository;
import com.goormy.hackathon.repository.JPA.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Repository
@Slf4j
Expand All @@ -26,18 +21,13 @@ public class FollowListRedisRepository {

private final RedisTemplate<String, Object> redisTemplate;

private ListOperations <String, Long> listOperations;

private final FollowRepository followRepository;

private final HashtagRepository hashtagRepository;

private final UserRepository userRepository;

public void set(Long hashtagId, Long userId) {
String key = "hashtagId:" + hashtagId.toString();
String userid = userId.toString();
redisTemplate.opsForList().rightPush(key, userid);
redisTemplate.opsForList().rightPush(key, userId);
}

public void delete(Long hashtagId, Long userId) {
Expand All @@ -55,7 +45,9 @@ public List<Follow> getAllFollows() {
if (userIds != null) {
for (Object userId : userIds) {
Long hashtagId = Long.parseLong(key.split(":")[1]);
User user = userRepository.findById((Long)userId)
Long userIdLong = Long.parseLong(String.valueOf(userId));

User user = userRepository.findById(userIdLong)
.orElseThrow(() -> new IllegalArgumentException("해당 사용자를 찾을 수 없습니다."));
Hashtag hashtag = hashtagRepository.findById(hashtagId)
.orElseThrow(() -> new IllegalArgumentException("해당 해시태그 찾을 수 없습니다. "));
Expand Down

0 comments on commit e4fa85b

Please sign in to comment.