From 8d9b2e6ee7d8dacc64d66a6f246ffd390b9fce9e Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Tue, 4 Mar 2025 00:22:56 +0800 Subject: [PATCH] fix: move cache after doFinally to ensure effective concurrency control (#7257) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What this PR does? 将 cache 移动到 doFinally 之后以确保并发控制有效,在这之前可能无法完全有效的控制并发 ```release-note None ``` --- .../halo/app/core/attachment/impl/ThumbnailServiceImpl.java | 3 ++- .../app/core/attachment/impl/ThumbnailServiceImplTest.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/run/halo/app/core/attachment/impl/ThumbnailServiceImpl.java b/application/src/main/java/run/halo/app/core/attachment/impl/ThumbnailServiceImpl.java index b8c1508fb..d77a94ba9 100644 --- a/application/src/main/java/run/halo/app/core/attachment/impl/ThumbnailServiceImpl.java +++ b/application/src/main/java/run/halo/app/core/attachment/impl/ThumbnailServiceImpl.java @@ -50,8 +50,9 @@ public class ThumbnailServiceImpl implements ThumbnailService { // restriction return ongoingTasks.computeIfAbsent(cacheKey, k -> doGenerate(imageUri, size) // In the case of concurrency, doGenerate must return the same instance + .doFinally(signalType -> ongoingTasks.remove(cacheKey)) .cache() - .doFinally(signalType -> ongoingTasks.remove(cacheKey))); + ); } record CacheKey(URI imageUri, ThumbnailSize size) { diff --git a/application/src/test/java/run/halo/app/core/attachment/impl/ThumbnailServiceImplTest.java b/application/src/test/java/run/halo/app/core/attachment/impl/ThumbnailServiceImplTest.java index 6821b6be1..c1c1433cc 100644 --- a/application/src/test/java/run/halo/app/core/attachment/impl/ThumbnailServiceImplTest.java +++ b/application/src/test/java/run/halo/app/core/attachment/impl/ThumbnailServiceImplTest.java @@ -192,7 +192,7 @@ class ThumbnailServiceImplTest { var createdUri = URI.create("/test-thumb.jpg"); doReturn(Mono.just(createdUri)).when(spyThumbnailService).create(any(), any()); - int threadCount = 10; + int threadCount = 100; ExecutorService executor = Executors.newFixedThreadPool(threadCount); var latch = new CountDownLatch(threadCount);