Skip to content

Commit

Permalink
feat: sha1으로 기존 스크립트 실행하도록 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Curry4182 committed Apr 4, 2024
1 parent cacbf8d commit fabdb9f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;
import java.util.Objects;

import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

Expand All @@ -22,6 +21,8 @@ public class ChatCursorCacheReader {

private final RedisTemplate<String, Object> redisTemplate;

private final ChatLLScriptManager chatLLScriptManager;

public ChatCursorCacheResult readByCursor(
final Long roomId,
final String cursorId,
Expand Down Expand Up @@ -112,12 +113,8 @@ private List<ChatCursorCache> readCursorCachesByLua(
final String currCursorId,
final int size
) {
String linkedListScript = ChatLuaScriptManager.getReadByLinkedListScript();

// 스크립트 실행
List<Object> result = redisTemplate.execute(
connection -> connection.eval(linkedListScript.getBytes(), ReturnType.MULTI, 0,
roomId.toString().getBytes(), currCursorId.getBytes(), String.valueOf(size).getBytes()), true);
List<Object> result = chatLLScriptManager.executeLLScript(roomId, currCursorId, size);

if (result == null || result.isEmpty()) {
return List.of();
Expand All @@ -131,4 +128,5 @@ private List<ChatCursorCache> readCursorCachesByLua(

return chatCursorCaches;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.programmers.lime.redis.chat;

import java.util.List;

import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.programmers.lime.redis.chat.model.LLReadScript;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class ChatLLScriptManager {

private final RedisTemplate<String, Object> redisTemplate;

private final LLReadScript llReadScript;
private String llScriptSha1;

private static boolean isNoscript(final RedisSystemException e) {
return e.getCause().getMessage().contains("NOSCRIPT");
}

@PostConstruct
public void initLLSha1() {
byte[] llBytes = llReadScript.getReadByLinkedListScriptBytes();
llScriptSha1 = redisTemplate.execute((RedisConnection connection) ->
connection.scriptLoad(llBytes)
);
}

public List<Object> executeLLScript(final Long roomId, final String currCursorId, final int size) {
try {
return executeEvalSha(roomId, currCursorId, size);
} catch (RedisSystemException e) {
if (isNoscript(e)) {
initLLSha1();
return executeEvalSha(roomId, currCursorId, size);
}
throw e;
}
}

private List<Object> executeEvalSha(final Long roomId, final String currCursorId, final int size) {
return redisTemplate.execute(
connection -> connection.evalSha(llScriptSha1.getBytes(), ReturnType.MULTI, 0,
roomId.toString().getBytes(), currCursorId.getBytes(), String.valueOf(size).getBytes()), true
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.programmers.lime.redis.chat;
package com.programmers.lime.redis.chat.model;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -10,22 +10,22 @@
import jakarta.annotation.PostConstruct;

@Component
public class ChatLuaScriptManager {
public class LLReadScript {

private static String readBylinkedListScript;
private String readByLinkedListScript;

@PostConstruct
public void init() {
try {
ClassPathResource cpr = new ClassPathResource("ReadByLinkedList.lua");
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
readBylinkedListScript = new String(bdata, StandardCharsets.UTF_8);
readByLinkedListScript = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Failed to load redis script", e);
}
}

public static String getReadByLinkedListScript() {
return readBylinkedListScript;
public byte[] getReadByLinkedListScriptBytes() {
return readByLinkedListScript.getBytes(StandardCharsets.UTF_8);
}
}

0 comments on commit fabdb9f

Please sign in to comment.