Skip to content

Commit

Permalink
feat: 채팅 목록 조회 데이터 프리워밍(Pre-warming) 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
Curry4182 committed Mar 30, 2024
1 parent a12d779 commit ee0da7b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.programmers.lime.global.event.chat;

import java.util.List;

import com.programmers.lime.domains.chatroom.model.ChatRoomInfo;

public record ChatInitCacheEvent(
List<ChatRoomInfo> chatRoomInfos
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.programmers.lime.global.initializer;

import java.util.List;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import com.programmers.lime.domains.chatroom.implementation.ChatRoomReader;
import com.programmers.lime.domains.chatroom.model.ChatRoomInfo;
import com.programmers.lime.global.event.chat.ChatInitCacheEvent;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ChatCursorCacheInitializer implements ApplicationRunner {

private final ChatRoomReader chatRoomReader;

private final ApplicationEventPublisher eventPublisher;

@Override
public void run(final ApplicationArguments args) {
List<ChatRoomInfo> chatRoomInfos = chatRoomReader.readOpenChatRoomsByMemberId(null);

ChatInitCacheEvent chatInitCacheEvent = new ChatInitCacheEvent(chatRoomInfos);
eventPublisher.publishEvent(chatInitCacheEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ private int getPageSize(final CursorPageParameters parameters) {

return parameterSize;
}

public ChatSummary readFirstChat(final Long chatRoomId) {
return chatRepository.findFirstByCursor(chatRoomId);
}

public ChatSummary readLastChat(final Long chatRoomId) {
return chatRepository.findLastByCursor(chatRoomId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import com.programmers.lime.domains.chat.model.ChatInfoWithMember;
import com.programmers.lime.domains.chat.model.ChatSummary;

public interface ChatRepositoryForCursor {
Expand All @@ -13,4 +12,7 @@ List<ChatSummary> findAllByCursor(
final int pageSize
);

ChatSummary findFirstByCursor(final Long chatRoomId);

ChatSummary findLastByCursor(final Long chatRoomId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ public List<ChatSummary> findAllByCursor(
.fetch();
}

@Override
public ChatSummary findFirstByCursor(final Long chatRoomId) {
return queryFactory
.select(Projections.constructor(ChatSummary.class,
generateChatCursorId(),
chat.id.as("chatId"),
chat.chatRoomId,
chat.memberId,
member.nickname.nickname,
member.socialInfo.profileImage,
chat.message,
chat.sendAt,
chat.chatType)
)
.from(chat)
.join(member).on(chat.memberId.eq(member.id))
.where(chat.chatRoomId.eq(chatRoomId))
.orderBy(chat.sendAt.desc())
.fetchFirst();
}

@Override
public ChatSummary findLastByCursor(final Long chatRoomId) {
return queryFactory
.select(Projections.constructor(ChatSummary.class,
generateChatCursorId(),
chat.id.as("chatId"),
chat.chatRoomId,
chat.memberId,
member.nickname.nickname,
member.socialInfo.profileImage,
chat.message,
chat.sendAt,
chat.chatType)
)
.from(chat)
.join(member).on(chat.memberId.eq(member.id))
.where(chat.chatRoomId.eq(chatRoomId))
.orderBy(chat.sendAt.asc())
.fetchFirst();
}

private BooleanExpression cursorIdCondition(final String cursorId) {
if (cursorId == null) {
return null;
Expand Down

0 comments on commit ee0da7b

Please sign in to comment.