Redis 연동 완료

This commit is contained in:
mskim 2025-12-12 14:08:37 +09:00
parent 9d4906a141
commit 66e02868a6
2 changed files with 43 additions and 9 deletions

View File

@ -934,7 +934,7 @@ public class MemoryResponse {
- ✅ `capabilities``supported` 필드 추가 - ✅ `capabilities``supported` 필드 추가
- ✅ JSON-RPC 2.0 표준 준수 (정상 응답에 error 필드 제외) - ✅ JSON-RPC 2.0 표준 준수 (정상 응답에 error 필드 제외)
- ✅ 요청/응답 전체 로깅 기능 추가 - ✅ 요청/응답 전체 로깅 기능 추가
- ⏳ Redis 연동 (TODO) - ✅ Redis 연동 (임시 메모리 저장/조회 완료, TTL 3일)
- ⏳ 벡터 검색 구현 (TODO) - ⏳ 벡터 검색 구현 (TODO)
- ⏳ 배치 처리 구현 (TODO) - ⏳ 배치 처리 구현 (TODO)

View File

@ -7,11 +7,15 @@ import com.pandol365.dewey.domain.memory.service.MemoryService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
/** /**
@ -24,24 +28,36 @@ import java.util.UUID;
public class MemoryServiceImpl implements MemoryService { public class MemoryServiceImpl implements MemoryService {
private final MemoryRepository memoryRepository; private final MemoryRepository memoryRepository;
// TODO: Redis 연동 추가 필요 private final RedisTemplate<String, Object> redisTemplate;
// private final ReactiveRedisTemplate<String, TemporaryMemory> redisTemplate;
private static final String TEMP_MEMORY_KEY_PREFIX = "tempMemory:";
private static final String USER_TEMP_MEMORY_KEY_PREFIX = "user:tempMemories:";
private static final int TEMP_TTL_DAYS = 3;
@SuppressWarnings("null")
@Override @Override
public TemporaryMemory storeTemporaryMemory(String userId, String memoryText, Integer importance) { public TemporaryMemory storeTemporaryMemory(String userId, String memoryText, Integer importance) {
log.info("임시 메모리 저장: userId={}, importance={}", userId, importance); log.info("임시 메모리 저장: userId={}, importance={}", userId, importance);
// TODO: Redis에 저장하는 로직 구현
TemporaryMemory temporaryMemory = TemporaryMemory.builder() TemporaryMemory temporaryMemory = TemporaryMemory.builder()
.id(UUID.randomUUID().toString()) .id(UUID.randomUUID().toString())
.userId(userId) .userId(userId)
.memoryText(memoryText) .memoryText(memoryText)
.importance(importance != null ? importance : 1) .importance(importance != null ? importance : 1)
.createdAt(LocalDateTime.now()) .createdAt(LocalDateTime.now())
.expiresAt(LocalDateTime.now().plusDays(3)) // TTL 3일 .expiresAt(LocalDateTime.now().plusDays(TEMP_TTL_DAYS)) // TTL 3일
.build(); .build();
// TODO: redisTemplate.opsForValue().set(key, temporaryMemory, Duration.ofDays(3)); final Duration ttl = Duration.ofDays(TEMP_TTL_DAYS);
// Redis에 저장
String memoryKey = TEMP_MEMORY_KEY_PREFIX + temporaryMemory.getId();
redisTemplate.opsForValue().set(memoryKey, temporaryMemory, ttl);
// 사용자별 목록에도 추가 (시간순 정렬)
String userMemoriesKey = USER_TEMP_MEMORY_KEY_PREFIX + userId;
redisTemplate.opsForZSet().add(userMemoriesKey, memoryKey, System.currentTimeMillis());
redisTemplate.expire(userMemoriesKey, ttl);
return temporaryMemory; return temporaryMemory;
} }
@ -51,10 +67,28 @@ public class MemoryServiceImpl implements MemoryService {
public List<TemporaryMemory> getTemporaryMemories(String userId) { public List<TemporaryMemory> getTemporaryMemories(String userId) {
log.info("임시 메모리 조회: userId={}", userId); log.info("임시 메모리 조회: userId={}", userId);
// TODO: Redis에서 조회하는 로직 구현 String userMemoriesKey = USER_TEMP_MEMORY_KEY_PREFIX + userId;
Set<Object> ids = redisTemplate.opsForZSet()
.reverseRange(userMemoriesKey, 0, -1);
if (ids == null || ids.isEmpty()) {
return List.of(); return List.of();
} }
List<TemporaryMemory> memories = new ArrayList<>();
for (Object idObj : ids) {
if (idObj instanceof String memoryKey) {
Object obj = redisTemplate.opsForValue().get(memoryKey);
if (obj instanceof TemporaryMemory memory) {
memories.add(memory);
}
}
}
return memories;
}
@Override @Override
public Memory savePermanentMemory(Memory memory) { public Memory savePermanentMemory(Memory memory) {
log.info("영구 메모리 저장: userId={}, importance={}", memory.getUserId(), memory.getImportance()); log.info("영구 메모리 저장: userId={}, importance={}", memory.getUserId(), memory.getImportance());