Skip to content

Commit

Permalink
Merge pull request #78 from 28th-meetup/feat/chat-1201
Browse files Browse the repository at this point in the history
feat: 메세지 저장과정 비동기 설정
  • Loading branch information
eckrin authored Dec 1, 2023
2 parents 69fc5cb + 983af64 commit c846a2a
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 17 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# 🍱 집밥도착 🍱

<img width="1920" alt="1" src="https://github.com/kusitms-com/28th_Semi_README/assets/81168401/76e8c2f8-0e82-4d20-b5ff-66985d078208">
Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/main/.DS_Store
Binary file not shown.
Binary file modified src/main/java/.DS_Store
Binary file not shown.
Binary file modified src/main/java/com/.DS_Store
Binary file not shown.
Binary file modified src/main/java/com/kusitms/.DS_Store
Binary file not shown.
Binary file modified src/main/java/com/kusitms/jipbap/.DS_Store
Binary file not shown.
Binary file modified src/main/java/com/kusitms/jipbap/auth/.DS_Store
Binary file not shown.
Binary file modified src/main/java/com/kusitms/jipbap/chat/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kusitms.jipbap.chat.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kusitms.jipbap.chat.model.dto.MessageDto;
import com.kusitms.jipbap.chat.model.entity.Message;
import com.kusitms.jipbap.chat.model.entity.Room;
import com.kusitms.jipbap.chat.repository.MessageRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@RequiredArgsConstructor
public class MessageReleaseService {

private final RedisTemplate<String, MessageDto> redisTemplateMessage;
private final MessageRepository messageRepository;
private final ObjectMapper objectMapper;

@Async
public void saveMessage(MessageDto messageDto, Room room) {
// DB 저장
Message message = messageRepository.save(new Message(messageDto.getSenderName(), room, messageDto.getMessage()));
message.updateSentTime(message.getCreatedAt().toString());

// 1. 직렬화
redisTemplateMessage.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));

// 2. redis 저장
redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto);

// 3. redistemplate의 expire() 을 이용해서, Key 를 만료시킬 수 있음
redisTemplateMessage.expire(messageDto.getRoomId(), 60, TimeUnit.MINUTES);
}
}
20 changes: 3 additions & 17 deletions src/main/java/com/kusitms/jipbap/chat/service/MessageService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.kusitms.jipbap.chat.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kusitms.jipbap.chat.exception.RoomNotExistsException;
import com.kusitms.jipbap.chat.model.dto.MessageDto;
import com.kusitms.jipbap.chat.model.entity.Message;
import com.kusitms.jipbap.chat.model.entity.Room;
import com.kusitms.jipbap.chat.exception.RoomNotExistsException;
import com.kusitms.jipbap.chat.repository.MessageRepository;
import com.kusitms.jipbap.chat.repository.RoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
Expand All @@ -26,7 +23,7 @@ public class MessageService {
private final RedisTemplate<String, MessageDto> redisTemplateMessage;
private final MessageRepository messageRepository;
private final RoomRepository roomRepository;
private final ObjectMapper objectMapper;
private final MessageReleaseService messageReleaseService;

// 메세지 저장
@Transactional
Expand All @@ -35,18 +32,7 @@ public void saveMessage(MessageDto messageDto) {
()->new RoomNotExistsException("채팅방이 더 이상 존재하지 않습니다.")
);

// DB 저장
Message message = messageRepository.save(new Message(messageDto.getSenderName(), room, messageDto.getMessage()));
message.updateSentTime(message.getCreatedAt().toString());

// 1. 직렬화
redisTemplateMessage.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));

// 2. redis 저장
redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto);

// 3. redistemplate의 expire() 을 이용해서, Key 를 만료시킬 수 있음
redisTemplateMessage.expire(messageDto.getRoomId(), 60, TimeUnit.MINUTES);
messageReleaseService.saveMessage(messageDto, room);
}

// 6. 대화 조회 - Redis & DB (TLB 캐시전략 유사)
Expand Down
Binary file modified src/main/java/com/kusitms/jipbap/common/.DS_Store
Binary file not shown.

0 comments on commit c846a2a

Please sign in to comment.