1. Redis에 어떤 모양으로 저장을 할까?

Hash 자료구조를 사용한다.

hashkey는 “like”, “hate”, “recLike” 이렇게 세 개가 있다.

예를 들어, “like”에 childId123이고 bookId가 여러 개인 경우 아래와 같이 저장된다.

{
  "like": {
    "123": ["bookId1", "bookId2", "bookId3"],
    "456": ["bookId4", "bookId5"]
  }
}

2. Redis에 정확하게 어떤 키를 넣어서 value를 뽑으면 좋을까?

위 참조

3. Redis 메서드 공부

좋아요를 redis 의 set 으로 관리한다면 다음과 같은 메서드를 통해 CRUD 할 수 있다.

@Service
@RequiredArgsConstructor
public class LikeServiceImpl implements LikeService {

    private static final String LIKES_KEY = "likes:";  // Redis Key prefix

    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    @Transactional
    public void insertLike(Long memberId, Long bookId) {
        String redisKey = LIKES_KEY + memberId;
        // 좋아요 책 목록에 추가 (중복이 제거됨)
        redisTemplate.opsForSet().add(redisKey, bookId);
    }

    @Override
    @Transactional
    public void removeLike(Long memberId, Long bookId) {
        String redisKey = LIKES_KEY + memberId;
        // 좋아요 목록에서 책 삭제
        redisTemplate.opsForSet().remove(redisKey, bookId);
    }

    @Override
    public Set<Object> getLikedBooks(Long memberId) {
        String redisKey = LIKES_KEY + memberId;
        // 좋아요 목록 조회
        return redisTemplate.opsForSet().members(redisKey);
    }

    @Override
    public boolean isBookLiked(Long memberId, Long bookId) {
        String redisKey = LIKES_KEY + memberId;
        // 책이 좋아요 목록에 있는지 확인
        return redisTemplate.opsForSet().isMember(redisKey, bookId);
    }

    @Override
    public long getLikedBooksCount(Long memberId) {
        String redisKey = LIKES_KEY + memberId;
        // 좋아요한 책의 개수 반환
        return redisTemplate.opsForSet().size(redisKey);
    }
}

Redis 테스트 클래스

// test lombok
testCompileOnly 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class test {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private ValueOperations<String, Object> valueOperations;

    @BeforeEach
    void setUp() {
        // ValueOperations 객체를 redisTemplate에서 초기화
        this.valueOperations = redisTemplate.opsForValue();
    }

    @Test
    void testValueOperations() {
        valueOperations.set("승희가 키", "신지가 밸류");
        log.info("승희가 키: {}", valueOperations.get("승희가 키").toString());
    }
}

→ 그냥 String : String으로 이루어진 맵을 저장하는 코드