fix(language-detection): 최초 감지 캐시 경합을 직렬화한다

This commit is contained in:
2026-09-10 15:11:48 +09:00
parent 6efecf5495
commit 0284070667
4 changed files with 464 additions and 8 deletions
@@ -25,18 +25,22 @@ class LanguageDetectionCacheService(
)
if (cached != null) return cached.detectedLanguage
val detectedLanguage = detector()?.takeIf { it.isNotBlank() } ?: return null
languageDetectionResultRepository.save(
LanguageDetectionResult(
val detectedLanguage = detector()?.takeIf { it.isNotBlank() }?.lowercase() ?: return null
languageDetectionResultRepository.insertIfAbsent(
sourceHash = sourceHash,
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
detectedLanguage = detectedLanguage,
provider = provider,
confidence = null,
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
)
return requireNotNull(
languageDetectionResultRepository.findDetectedLanguageForUpdate(
sourceHash = sourceHash,
sourceTextSample = normalizedQuery.take(MAX_SAMPLE_LENGTH),
detectedLanguage = detectedLanguage.lowercase(),
provider = provider,
confidence = null,
normalizationVersion = SourceTextNormalizer.NORMALIZATION_VERSION
)
)
return detectedLanguage.lowercase()
}
companion object {
@@ -1,6 +1,9 @@
package kr.co.vividnext.sodalive.content
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
interface LanguageDetectionResultRepository : JpaRepository<LanguageDetectionResult, Long> {
fun findBySourceHashAndProviderAndNormalizationVersion(
@@ -8,4 +11,55 @@ interface LanguageDetectionResultRepository : JpaRepository<LanguageDetectionRes
provider: String,
normalizationVersion: String
): LanguageDetectionResult?
@Modifying
@Query(
value = """
INSERT INTO language_detection_result (
source_hash,
source_text_sample,
detected_language,
provider,
confidence,
normalization_version,
created_at,
updated_at
) VALUES (
:sourceHash,
:sourceTextSample,
:detectedLanguage,
:provider,
:confidence,
:normalizationVersion,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
) ON DUPLICATE KEY UPDATE id = id
""",
nativeQuery = true
)
fun insertIfAbsent(
@Param("sourceHash") sourceHash: String,
@Param("sourceTextSample") sourceTextSample: String,
@Param("detectedLanguage") detectedLanguage: String,
@Param("provider") provider: String,
@Param("confidence") confidence: Double?,
@Param("normalizationVersion") normalizationVersion: String
): Int
@Query(
value = """
SELECT detected_language
FROM language_detection_result
WHERE source_hash = :sourceHash
AND provider = :provider
AND normalization_version = :normalizationVersion
FOR UPDATE
""",
nativeQuery = true
)
fun findDetectedLanguageForUpdate(
@Param("sourceHash") sourceHash: String,
@Param("provider") provider: String,
@Param("normalizationVersion") normalizationVersion: String
): String?
}