Skip to content

Commit 066163f

Browse files
committed
Fix the issue that previous optimization for special case (multiple same URL in prefetcher list) breaks the queryCacheType option sematic
We should only do optimization on `async` disk cache query case
1 parent 10d06f6 commit 066163f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

SDWebImage/Core/SDImageCache.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,8 @@ - (nullable SDImageCacheToken *)queryCacheOperationForKey:(nullable NSString *)k
619619

620620
// First check the in-memory cache...
621621
UIImage *image;
622-
if (queryCacheType != SDImageCacheTypeDisk) {
622+
BOOL shouldQueryDiskOnly = (queryCacheType == SDImageCacheTypeDisk);
623+
if (!shouldQueryDiskOnly) {
623624
image = [self imageFromMemoryCacheForKey:key];
624625
}
625626

@@ -683,14 +684,19 @@ - (nullable SDImageCacheToken *)queryCacheOperationForKey:(nullable NSString *)k
683684
// the image is from in-memory cache, but need image data
684685
diskImage = image;
685686
} else if (diskData) {
687+
// the image memory cache miss, need image data and image
686688
BOOL shouldCacheToMemory = YES;
687689
if (context[SDWebImageContextStoreCacheType]) {
688690
SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue];
689691
shouldCacheToMemory = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory);
690692
}
691-
// Special case: If user query image in list for the same URL, to avoid decode and write **same** image object into disk cache multiple times, we query and check memory cache here again.
692-
if (shouldCacheToMemory && self.config.shouldCacheImagesInMemory) {
693-
diskImage = [self.memoryCache objectForKey:key];
693+
// Special case: If user query image in list for the same URL, to avoid decode and write **same** image object into disk cache multiple times, we query and check memory cache here again. See: #3523
694+
// This because disk operation can be async, previous sync check of `memory cache miss`, does not gurantee current check of `memory cache miss`
695+
if (!shouldQueryDiskSync) {
696+
// First check the in-memory cache...
697+
if (!shouldQueryDiskOnly) {
698+
diskImage = [self imageFromMemoryCacheForKey:key];
699+
}
694700
}
695701
// decode image data only if in-memory cache missed
696702
if (!diskImage) {

0 commit comments

Comments
 (0)