Skip to content

Commit

Permalink
[feat] 메시지 로드, 저장시 24시간마다 redis에서 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
khee2 committed Jun 13, 2024
1 parent 7ace487 commit 918d8f8
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public MessageService(
this.messageRepository = messageRepository;
this.messageRoomRepository = messageRoomRepository;
}

public void saveMessage(MessageDto messageDto) {
MessageRoom messageRoom = messageRoomRepository.findByRoomId(messageDto.getRoomId())
.orElseThrow(() -> new IllegalArgumentException("해당 쪽지방이 존재하지 않습니다."));
Expand All @@ -49,7 +50,7 @@ public void saveMessage(MessageDto messageDto) {

redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(MessageDto.class));
redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto);
redisTemplateMessage.expire(messageDto.getRoomId(), 1, TimeUnit.HOURS); // redis에서 1시간마다 삭제
redisTemplateMessage.expire(messageDto.getRoomId(), 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제
}

/**
Expand Down Expand Up @@ -78,7 +79,8 @@ public List<MessageDto> loadMessage(String roomId) {
.build();
messageList.add(messageDto);
redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(Message.class)); // 직렬화
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장
redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장
redisTemplateMessage.expire(roomId, 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제
}
} else { // 4. 뒤쪽에 데이터 붙이기
messageList.addAll(redisMessageList);
Expand Down

0 comments on commit 918d8f8

Please sign in to comment.