Authored by Bogdan Poplauschi
Committed by GitHub

Merge pull request #2057 from dreampiggy/feature_disk_cache_reading_options

Allow user to provide reading options such as mapped file to improve performance in SDImageCache disk cache
@@ -308,14 +308,14 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -308,14 +308,14 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
308 308
309 - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key { 309 - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key {
310 NSString *defaultPath = [self defaultCachePathForKey:key]; 310 NSString *defaultPath = [self defaultCachePathForKey:key];
311 - NSData *data = [NSData dataWithContentsOfFile:defaultPath]; 311 + NSData *data = [NSData dataWithContentsOfFile:defaultPath options:self.config.diskCacheReadingOptions error:nil];
312 if (data) { 312 if (data) {
313 return data; 313 return data;
314 } 314 }
315 315
316 // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name 316 // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
317 // checking the key with and without the extension 317 // checking the key with and without the extension
318 - data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension]; 318 + data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil];
319 if (data) { 319 if (data) {
320 return data; 320 return data;
321 } 321 }
@@ -323,14 +323,14 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { @@ -323,14 +323,14 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
323 NSArray<NSString *> *customPaths = [self.customPaths copy]; 323 NSArray<NSString *> *customPaths = [self.customPaths copy];
324 for (NSString *path in customPaths) { 324 for (NSString *path in customPaths) {
325 NSString *filePath = [self cachePathForKey:key inPath:path]; 325 NSString *filePath = [self cachePathForKey:key inPath:path];
326 - NSData *imageData = [NSData dataWithContentsOfFile:filePath]; 326 + NSData *imageData = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil];
327 if (imageData) { 327 if (imageData) {
328 return imageData; 328 return imageData;
329 } 329 }
330 330
331 // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name 331 // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
332 // checking the key with and without the extension 332 // checking the key with and without the extension
333 - imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension]; 333 + imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil];
334 if (imageData) { 334 if (imageData) {
335 return imageData; 335 return imageData;
336 } 336 }
@@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
18 @property (assign, nonatomic) BOOL shouldDecompressImages; 18 @property (assign, nonatomic) BOOL shouldDecompressImages;
19 19
20 /** 20 /**
21 - * disable iCloud backup [defaults to YES] 21 + * disable iCloud backup [defaults to YES]
22 */ 22 */
23 @property (assign, nonatomic) BOOL shouldDisableiCloud; 23 @property (assign, nonatomic) BOOL shouldDisableiCloud;
24 24
@@ -28,7 +28,13 @@ @@ -28,7 +28,13 @@
28 @property (assign, nonatomic) BOOL shouldCacheImagesInMemory; 28 @property (assign, nonatomic) BOOL shouldCacheImagesInMemory;
29 29
30 /** 30 /**
31 - * The maximum length of time to keep an image in the cache, in seconds 31 + * The reading options while reading cache from disk.
  32 + * Defaults to 0. You can set this to mapped file to improve performance.
  33 + */
  34 +@property (assign, nonatomic) NSDataReadingOptions diskCacheReadingOptions;
  35 +
  36 +/**
  37 + * The maximum length of time to keep an image in the cache, in seconds.
32 */ 38 */
33 @property (assign, nonatomic) NSInteger maxCacheAge; 39 @property (assign, nonatomic) NSInteger maxCacheAge;
34 40
@@ -17,6 +17,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week @@ -17,6 +17,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
17 _shouldDecompressImages = YES; 17 _shouldDecompressImages = YES;
18 _shouldDisableiCloud = YES; 18 _shouldDisableiCloud = YES;
19 _shouldCacheImagesInMemory = YES; 19 _shouldCacheImagesInMemory = YES;
  20 + _diskCacheReadingOptions = 0;
20 _maxCacheAge = kDefaultCacheMaxCacheAge; 21 _maxCacheAge = kDefaultCacheMaxCacheAge;
21 _maxCacheSize = 0; 22 _maxCacheSize = 0;
22 } 23 }