fix: move cache after doFinally to ensure effective concurrency control (#7257)

### What this PR does?
将 cache 移动到 doFinally 之后以确保并发控制有效,在这之前可能无法完全有效的控制并发


```release-note
None
```
pull/7258/head^2
guqing 2025-03-04 00:22:56 +08:00 committed by GitHub
parent 0c7a8de0c9
commit 8d9b2e6ee7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 2 deletions

View File

@ -50,8 +50,9 @@ public class ThumbnailServiceImpl implements ThumbnailService {
// restriction // restriction
return ongoingTasks.computeIfAbsent(cacheKey, k -> doGenerate(imageUri, size) return ongoingTasks.computeIfAbsent(cacheKey, k -> doGenerate(imageUri, size)
// In the case of concurrency, doGenerate must return the same instance // In the case of concurrency, doGenerate must return the same instance
.doFinally(signalType -> ongoingTasks.remove(cacheKey))
.cache() .cache()
.doFinally(signalType -> ongoingTasks.remove(cacheKey))); );
} }
record CacheKey(URI imageUri, ThumbnailSize size) { record CacheKey(URI imageUri, ThumbnailSize size) {

View File

@ -192,7 +192,7 @@ class ThumbnailServiceImplTest {
var createdUri = URI.create("/test-thumb.jpg"); var createdUri = URI.create("/test-thumb.jpg");
doReturn(Mono.just(createdUri)).when(spyThumbnailService).create(any(), any()); doReturn(Mono.just(createdUri)).when(spyThumbnailService).create(any(), any());
int threadCount = 10; int threadCount = 100;
ExecutorService executor = Executors.newFixedThreadPool(threadCount); ExecutorService executor = Executors.newFixedThreadPool(threadCount);
var latch = new CountDownLatch(threadCount); var latch = new CountDownLatch(threadCount);