Authored by Bogdan Poplauschi

Another clarification: `imageFromDiskCacheForKey:` used to also check the memory…

… cache which I think is misleading. Now `imageFromDiskCacheForKey` only checks the disk cache and the new method `imageFromCacheForKey` checks both caches
@@ -144,18 +144,25 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot @@ -144,18 +144,25 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot
144 /** 144 /**
145 * Query the memory cache synchronously. 145 * Query the memory cache synchronously.
146 * 146 *
147 - * @param key The unique key used to store the wanted image 147 + * @param key The unique key used to store the image
148 */ 148 */
149 - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key; 149 - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;
150 150
151 /** 151 /**
152 - * Query the disk cache synchronously after checking the memory cache. 152 + * Query the disk cache synchronously.
153 * 153 *
154 - * @param key The unique key used to store the wanted image 154 + * @param key The unique key used to store the image
155 */ 155 */
156 - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key; 156 - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;
157 157
158 /** 158 /**
  159 + * Query the cache (memory and or disk) synchronously after checking the memory cache.
  160 + *
  161 + * @param key The unique key used to store the image
  162 + */
  163 +- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;
  164 +
  165 +/**
159 * Remove the image from memory and disk cache asynchronously 166 * Remove the image from memory and disk cache asynchronously
160 * 167 *
161 * @param key The unique image cache key 168 * @param key The unique image cache key
@@ -231,7 +231,6 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -231,7 +231,6 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
231 } 231 }
232 } 232 }
233 233
234 -  
235 - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock { 234 - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {
236 dispatch_async(_ioQueue, ^{ 235 dispatch_async(_ioQueue, ^{
237 BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]]; 236 BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
@@ -255,7 +254,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -255,7 +254,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
255 } 254 }
256 255
257 - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key { 256 - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key {
  257 + UIImage *diskImage = [self diskImageForKey:key];
  258 + if (diskImage && self.config.shouldCacheImagesInMemory) {
  259 + NSUInteger cost = SDCacheCostForImage(diskImage);
  260 + [self.memCache setObject:diskImage forKey:key cost:cost];
  261 + }
  262 +
  263 + return diskImage;
  264 +}
258 265
  266 +- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key {
259 // First check the in-memory cache... 267 // First check the in-memory cache...
260 UIImage *image = [self imageFromMemoryCacheForKey:key]; 268 UIImage *image = [self imageFromMemoryCacheForKey:key];
261 if (image) { 269 if (image) {
@@ -263,13 +271,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -263,13 +271,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
263 } 271 }
264 272
265 // Second check the disk cache... 273 // Second check the disk cache...
266 - UIImage *diskImage = [self diskImageForKey:key];  
267 - if (diskImage && self.shouldCacheImagesInMemory) {  
268 - NSUInteger cost = SDCacheCostForImage(diskImage);  
269 - [self.memCache setObject:diskImage forKey:key cost:cost];  
270 - }  
271 -  
272 - return diskImage; 274 + image = [self imageFromDiskCacheForKey:key];
  275 + return image;
273 } 276 }
274 277
275 - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key { 278 - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key {
@@ -118,7 +118,7 @@ static char TAG_ACTIVITY_SHOW; @@ -118,7 +118,7 @@ static char TAG_ACTIVITY_SHOW;
118 progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock 118 progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
119 completed:(nullable SDExternalCompletionBlock)completedBlock { 119 completed:(nullable SDExternalCompletionBlock)completedBlock {
120 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; 120 NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
121 - UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; 121 + UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:key];
122 122
123 [self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock]; 123 [self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
124 } 124 }