Skip to content

Commit d049e64

Browse files
committed
refactor: 캐시 및 DLQ 타입 변환 로직 개선
- toLongOrNull 메서드로 통일 - 예외 throw 대신 null 반환 + 로깅 - 중복 타입 변환 코드 제거 - DlqMessage 불필요한 null 체크 제거
1 parent c67f50a commit d049e64

3 files changed

Lines changed: 72 additions & 18 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/infrastructure/cache/MemberLikesCache.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ public Set<Long> findLikedProductIds(Long memberId, List<Long> productIds) {
7979
return Set.of();
8080
}
8181

82-
// productIds 중 좋아요한 것만 필터링
82+
// productIds 중 좋아요한 것만 필터링 (변환 실패한 값은 제외)
8383
Set<Long> likedSet = allLiked.stream()
84-
.map(obj -> (Long) obj)
84+
.map(this::toLongOrNull)
85+
.filter(v -> v != null)
8586
.collect(Collectors.toSet());
8687

8788
return productIds.stream()
@@ -107,7 +108,8 @@ public Set<Long> findAll(Long memberId) {
107108
}
108109

109110
return members.stream()
110-
.map(obj -> (Long) obj)
111+
.map(this::toLongOrNull)
112+
.filter(v -> v != null)
111113
.collect(Collectors.toSet());
112114

113115
} catch (Exception e) {
@@ -165,4 +167,29 @@ public void delete(Long memberId) {
165167
log.warn("[MemberLikesCache] delete failed, error={}", e.getMessage());
166168
}
167169
}
170+
171+
/**
172+
* Redis 값을 Long으로 변환 (변환 실패 시 null 반환)
173+
*/
174+
private Long toLongOrNull(Object value) {
175+
if (value == null) {
176+
return null;
177+
}
178+
if (value instanceof Long) {
179+
return (Long) value;
180+
}
181+
if (value instanceof Integer) {
182+
return ((Integer) value).longValue();
183+
}
184+
if (value instanceof String) {
185+
try {
186+
return Long.parseLong((String) value);
187+
} catch (NumberFormatException e) {
188+
log.warn("[MemberLikesCache] Failed to parse String to Long: {}", value);
189+
return null;
190+
}
191+
}
192+
log.warn("[MemberLikesCache] Unsupported value type: {}", value.getClass().getSimpleName());
193+
return null;
194+
}
168195
}

apps/commerce-api/src/main/java/com/loopers/infrastructure/cache/ProductLikeCountCache.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,7 @@ public Long get(Long productId) {
6060
try {
6161
String key = CacheKeyGenerator.productLikeCountKey(productId);
6262
Object value = cacheRedisTemplate.opsForValue().get(key);
63-
64-
if (value == null) {
65-
return null;
66-
}
67-
68-
if (value instanceof Integer) {
69-
return ((Integer) value).longValue();
70-
}
71-
72-
return (Long) value;
63+
return toLongOrNull(value);
7364
} catch (Exception e) {
7465
log.warn("[ProductLikeCountCache] get failed, error={}", e.getMessage());
7566
return null;
@@ -109,11 +100,9 @@ public Map<Long, Long> getAllCounts() {
109100
// key에서 productId 추출
110101
Long productId = extractProductIdFromKey(key);
111102
Object value = cacheRedisTemplate.opsForValue().get(key);
103+
Long count = toLongOrNull(value);
112104

113-
if (value != null && productId != null) {
114-
Long count = value instanceof Integer
115-
? ((Integer) value).longValue()
116-
: (Long) value;
105+
if (count != null && productId != null) {
117106
result.put(productId, count);
118107
}
119108
} catch (Exception e) {
@@ -155,4 +144,29 @@ private Long extractProductIdFromKey(String key) {
155144
return null;
156145
}
157146
}
147+
148+
/**
149+
* Redis 값을 Long으로 변환 (변환 실패 시 null 반환)
150+
*/
151+
private Long toLongOrNull(Object value) {
152+
if (value == null) {
153+
return null;
154+
}
155+
if (value instanceof Long) {
156+
return (Long) value;
157+
}
158+
if (value instanceof Integer) {
159+
return ((Integer) value).longValue();
160+
}
161+
if (value instanceof String) {
162+
try {
163+
return Long.parseLong((String) value);
164+
} catch (NumberFormatException e) {
165+
log.warn("[ProductLikeCountCache] Failed to parse String to Long: {}", value);
166+
return null;
167+
}
168+
}
169+
log.warn("[ProductLikeCountCache] Unsupported value type: {}", value.getClass().getSimpleName());
170+
return null;
171+
}
158172
}

apps/commerce-streamer/src/main/java/com/loopers/domain/dlq/DlqMessage.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static DlqMessage create(
103103
dlqMessage.originalOffset = originalOffset;
104104
dlqMessage.messageKey = messageKey;
105105
dlqMessage.messageValue = messageValue;
106-
dlqMessage.errorType = exception.getClass().getSimpleName();
106+
dlqMessage.errorType = resolveErrorType(exception);
107107
dlqMessage.errorMessage = exception.getMessage();
108108
dlqMessage.stackTrace = getStackTraceAsString(exception);
109109
dlqMessage.failedAt = ZonedDateTime.now();
@@ -138,4 +138,17 @@ private static String getStackTraceAsString(Exception exception) {
138138

139139
return sb.toString();
140140
}
141+
142+
private static String resolveErrorType(Exception exception) {
143+
String simpleName = exception.getClass().getSimpleName();
144+
if (!simpleName.isEmpty()) {
145+
return simpleName;
146+
}
147+
// 익명 클래스의 경우 simpleName이 빈 문자열이므로 부모 클래스 이름 사용
148+
Class<?> superClass = exception.getClass().getSuperclass();
149+
if (superClass != null) {
150+
return superClass.getSimpleName();
151+
}
152+
return exception.getClass().getName();
153+
}
141154
}

0 commit comments

Comments
 (0)