123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- /*
- * This file is part of the SDWebImage package.
- * (c) Olivier Poitrey <rs@dailymotion.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- #import "SDImageCache.h"
- #import "SDWebImageDecoder.h"
- #import "UIImage+MultiFormat.h"
- #import <CommonCrypto/CommonDigest.h>
- #import "UIImage+GIF.h"
- #import "NSData+ImageContentType.h"
- #import "NSImage+WebCache.h"
- // See https://github.com/rs/SDWebImage/pull/1141 for discussion
- @interface AutoPurgeCache : NSCache
- @end
- @implementation AutoPurgeCache
- - (nonnull instancetype)init {
- self = [super init];
- if (self) {
- #if SD_UIKIT
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
- #endif
- }
- return self;
- }
- - (void)dealloc {
- #if SD_UIKIT
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
- #endif
- }
- @end
- FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
- #if SD_MAC
- return image.size.height * image.size.width;
- #elif SD_UIKIT || SD_WATCH
- return image.size.height * image.size.width * image.scale * image.scale;
- #endif
- }
- @interface SDImageCache ()
- #pragma mark - Properties
- @property (strong, nonatomic, nonnull) NSCache *memCache;
- @property (strong, nonatomic, nonnull) NSString *diskCachePath;
- @property (strong, nonatomic, nullable) NSMutableArray<NSString *> *customPaths;
- @property (SDDispatchQueueSetterSementics, nonatomic, nullable) dispatch_queue_t ioQueue;
- @end
- @implementation SDImageCache {
- NSFileManager *_fileManager;
- }
- #pragma mark - Singleton, init, dealloc
- + (nonnull instancetype)sharedImageCache {
- static dispatch_once_t once;
- static id instance;
- dispatch_once(&once, ^{
- instance = [self new];
- });
- return instance;
- }
- - (instancetype)init {
- return [self initWithNamespace:@"default"];
- }
- - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns {
- NSString *path = [self makeDiskCachePath:ns];
- return [self initWithNamespace:ns diskCacheDirectory:path];
- }
- - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
- diskCacheDirectory:(nonnull NSString *)directory {
- if ((self = [super init])) {
- NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];
-
- // Create IO serial queue
- _ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);
-
- _config = [[SDImageCacheConfig alloc] init];
-
- // Init the memory cache
- _memCache = [[AutoPurgeCache alloc] init];
- _memCache.name = fullNamespace;
- // Init the disk cache
- if (directory != nil) {
- _diskCachePath = [directory stringByAppendingPathComponent:fullNamespace];
- } else {
- NSString *path = [self makeDiskCachePath:ns];
- _diskCachePath = path;
- }
- dispatch_sync(_ioQueue, ^{
- _fileManager = [NSFileManager new];
- });
- #if SD_UIKIT
- // Subscribe to app events
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(clearMemory)
- name:UIApplicationDidReceiveMemoryWarningNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(deleteOldFiles)
- name:UIApplicationWillTerminateNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(backgroundDeleteOldFiles)
- name:UIApplicationDidEnterBackgroundNotification
- object:nil];
- #endif
- }
- return self;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- SDDispatchQueueRelease(_ioQueue);
- }
- - (void)checkIfQueueIsIOQueue {
- const char *currentQueueLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
- const char *ioQueueLabel = dispatch_queue_get_label(self.ioQueue);
- if (strcmp(currentQueueLabel, ioQueueLabel) != 0) {
- NSLog(@"This method should be called from the ioQueue");
- }
- }
- #pragma mark - Cache paths
- - (void)addReadOnlyCachePath:(nonnull NSString *)path {
- if (!self.customPaths) {
- self.customPaths = [NSMutableArray new];
- }
- if (![self.customPaths containsObject:path]) {
- [self.customPaths addObject:path];
- }
- }
- - (nullable NSString *)cachePathForKey:(nullable NSString *)key inPath:(nonnull NSString *)path {
- NSString *filename = [self cachedFileNameForKey:key];
- return [path stringByAppendingPathComponent:filename];
- }
- - (nullable NSString *)defaultCachePathForKey:(nullable NSString *)key {
- return [self cachePathForKey:key inPath:self.diskCachePath];
- }
- - (nullable NSString *)cachedFileNameForKey:(nullable NSString *)key {
- const char *str = key.UTF8String;
- if (str == NULL) {
- str = "";
- }
- unsigned char r[CC_MD5_DIGEST_LENGTH];
- CC_MD5(str, (CC_LONG)strlen(str), r);
- NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",
- r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],
- r[11], r[12], r[13], r[14], r[15], [key.pathExtension isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", key.pathExtension]];
- return filename;
- }
- - (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
- NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- return [paths[0] stringByAppendingPathComponent:fullNamespace];
- }
- #pragma mark - Store Ops
- - (void)storeImage:(nullable UIImage *)image
- forKey:(nullable NSString *)key
- completion:(nullable SDWebImageNoParamsBlock)completionBlock {
- [self storeImage:image imageData:nil forKey:key toDisk:YES completion:completionBlock];
- }
- - (void)storeImage:(nullable UIImage *)image
- forKey:(nullable NSString *)key
- toDisk:(BOOL)toDisk
- completion:(nullable SDWebImageNoParamsBlock)completionBlock {
- [self storeImage:image imageData:nil forKey:key toDisk:toDisk completion:completionBlock];
- }
- - (void)storeImage:(nullable UIImage *)image
- imageData:(nullable NSData *)imageData
- forKey:(nullable NSString *)key
- toDisk:(BOOL)toDisk
- completion:(nullable SDWebImageNoParamsBlock)completionBlock {
- if (!image || !key) {
- if (completionBlock) {
- completionBlock();
- }
- return;
- }
- // if memory cache is enabled
- if (self.config.shouldCacheImagesInMemory) {
- NSUInteger cost = SDCacheCostForImage(image);
- [self.memCache setObject:image forKey:key cost:cost];
- }
-
- if (toDisk) {
- dispatch_async(self.ioQueue, ^{
- @autoreleasepool {
- NSData *data = imageData;
- if (!data && image) {
- SDImageFormat imageFormatFromData = [NSData sd_imageFormatForImageData:data];
- data = [image sd_imageDataAsFormat:imageFormatFromData];
- }
- [self storeImageDataToDisk:data forKey:key];
- }
-
- if (completionBlock) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completionBlock();
- });
- }
- });
- } else {
- if (completionBlock) {
- completionBlock();
- }
- }
- }
- - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key {
- if (!imageData || !key) {
- return;
- }
-
- [self checkIfQueueIsIOQueue];
-
- if (![_fileManager fileExistsAtPath:_diskCachePath]) {
- [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
- }
-
- // get cache Path for image key
- NSString *cachePathForKey = [self defaultCachePathForKey:key];
- // transform to NSUrl
- NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey];
-
- [_fileManager createFileAtPath:cachePathForKey contents:imageData attributes:nil];
-
- // disable iCloud backup
- if (self.config.shouldDisableiCloud) {
- [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
- }
- }
- #pragma mark - Query and Retrieve Ops
- - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {
- dispatch_async(_ioQueue, ^{
- BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
- // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
- // checking the key with and without the extension
- if (!exists) {
- exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key].stringByDeletingPathExtension];
- }
- if (completionBlock) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completionBlock(exists);
- });
- }
- });
- }
- - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key {
- return [self.memCache objectForKey:key];
- }
- - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key {
- UIImage *diskImage = [self diskImageForKey:key];
- if (diskImage && self.config.shouldCacheImagesInMemory) {
- NSUInteger cost = SDCacheCostForImage(diskImage);
- [self.memCache setObject:diskImage forKey:key cost:cost];
- }
- return diskImage;
- }
- - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key {
- // First check the in-memory cache...
- UIImage *image = [self imageFromMemoryCacheForKey:key];
- if (image) {
- return image;
- }
-
- // Second check the disk cache...
- image = [self imageFromDiskCacheForKey:key];
- return image;
- }
- - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key {
- NSString *defaultPath = [self defaultCachePathForKey:key];
- NSData *data = [NSData dataWithContentsOfFile:defaultPath];
- if (data) {
- return data;
- }
- // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
- // checking the key with and without the extension
- data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension];
- if (data) {
- return data;
- }
- NSArray<NSString *> *customPaths = [self.customPaths copy];
- for (NSString *path in customPaths) {
- NSString *filePath = [self cachePathForKey:key inPath:path];
- NSData *imageData = [NSData dataWithContentsOfFile:filePath];
- if (imageData) {
- return imageData;
- }
- // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
- // checking the key with and without the extension
- imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension];
- if (imageData) {
- return imageData;
- }
- }
- return nil;
- }
- - (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
- NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
- if (data) {
- UIImage *image = [UIImage sd_imageWithData:data];
- image = [self scaledImageForKey:key image:image];
- if (self.config.shouldDecompressImages) {
- image = [UIImage decodedImageWithImage:image];
- }
- return image;
- } else {
- return nil;
- }
- }
- - (nullable UIImage *)scaledImageForKey:(nullable NSString *)key image:(nullable UIImage *)image {
- return SDScaledImageForKey(key, image);
- }
- - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock {
- if (!key) {
- if (doneBlock) {
- doneBlock(nil, nil, SDImageCacheTypeNone);
- }
- return nil;
- }
- // First check the in-memory cache...
- UIImage *image = [self imageFromMemoryCacheForKey:key];
- if (image) {
- NSData *diskData = nil;
- if ([image isGIF]) {
- diskData = [self diskImageDataBySearchingAllPathsForKey:key];
- }
- if (doneBlock) {
- doneBlock(image, diskData, SDImageCacheTypeMemory);
- }
- return nil;
- }
- NSOperation *operation = [NSOperation new];
- dispatch_async(self.ioQueue, ^{
- if (operation.isCancelled) {
- // do not call the completion if cancelled
- return;
- }
- @autoreleasepool {
- NSData *diskData = [self diskImageDataBySearchingAllPathsForKey:key];
- UIImage *diskImage = [self diskImageForKey:key];
- if (diskImage && self.config.shouldCacheImagesInMemory) {
- NSUInteger cost = SDCacheCostForImage(diskImage);
- [self.memCache setObject:diskImage forKey:key cost:cost];
- }
- if (doneBlock) {
- dispatch_async(dispatch_get_main_queue(), ^{
- doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
- });
- }
- }
- });
- return operation;
- }
- #pragma mark - Remove Ops
- - (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion {
- [self removeImageForKey:key fromDisk:YES withCompletion:completion];
- }
- - (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion {
- if (key == nil) {
- return;
- }
- if (self.config.shouldCacheImagesInMemory) {
- [self.memCache removeObjectForKey:key];
- }
- if (fromDisk) {
- dispatch_async(self.ioQueue, ^{
- [_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
-
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion();
- });
- }
- });
- } else if (completion){
- completion();
- }
-
- }
- # pragma mark - Mem Cache settings
- - (void)setMaxMemoryCost:(NSUInteger)maxMemoryCost {
- self.memCache.totalCostLimit = maxMemoryCost;
- }
- - (NSUInteger)maxMemoryCost {
- return self.memCache.totalCostLimit;
- }
- - (NSUInteger)maxMemoryCountLimit {
- return self.memCache.countLimit;
- }
- - (void)setMaxMemoryCountLimit:(NSUInteger)maxCountLimit {
- self.memCache.countLimit = maxCountLimit;
- }
- #pragma mark - Cache clean Ops
- - (void)clearMemory {
- [self.memCache removeAllObjects];
- }
- - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion {
- dispatch_async(self.ioQueue, ^{
- [_fileManager removeItemAtPath:self.diskCachePath error:nil];
- [_fileManager createDirectoryAtPath:self.diskCachePath
- withIntermediateDirectories:YES
- attributes:nil
- error:NULL];
- if (completion) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completion();
- });
- }
- });
- }
- - (void)deleteOldFiles {
- [self deleteOldFilesWithCompletionBlock:nil];
- }
- - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock {
- dispatch_async(self.ioQueue, ^{
- NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
- NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
- // This enumerator prefetches useful properties for our cache files.
- NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
- includingPropertiesForKeys:resourceKeys
- options:NSDirectoryEnumerationSkipsHiddenFiles
- errorHandler:NULL];
- NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
- NSMutableDictionary<NSURL *, NSDictionary<NSString *, id> *> *cacheFiles = [NSMutableDictionary dictionary];
- NSUInteger currentCacheSize = 0;
- // Enumerate all of the files in the cache directory. This loop has two purposes:
- //
- // 1. Removing files that are older than the expiration date.
- // 2. Storing file attributes for the size-based cleanup pass.
- NSMutableArray<NSURL *> *urlsToDelete = [[NSMutableArray alloc] init];
- for (NSURL *fileURL in fileEnumerator) {
- NSError *error;
- NSDictionary<NSString *, id> *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];
- // Skip directories and errors.
- if (error || !resourceValues || [resourceValues[NSURLIsDirectoryKey] boolValue]) {
- continue;
- }
- // Remove files that are older than the expiration date;
- NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
- if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
- [urlsToDelete addObject:fileURL];
- continue;
- }
- // Store a reference to this file and account for its total size.
- NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
- currentCacheSize += totalAllocatedSize.unsignedIntegerValue;
- cacheFiles[fileURL] = resourceValues;
- }
-
- for (NSURL *fileURL in urlsToDelete) {
- [_fileManager removeItemAtURL:fileURL error:nil];
- }
- // If our remaining disk cache exceeds a configured maximum size, perform a second
- // size-based cleanup pass. We delete the oldest files first.
- if (self.config.maxCacheSize > 0 && currentCacheSize > self.config.maxCacheSize) {
- // Target half of our maximum cache size for this cleanup pass.
- const NSUInteger desiredCacheSize = self.config.maxCacheSize / 2;
- // Sort the remaining cache files by their last modification time (oldest first).
- NSArray<NSURL *> *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
- usingComparator:^NSComparisonResult(id obj1, id obj2) {
- return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
- }];
- // Delete files until we fall below our desired cache size.
- for (NSURL *fileURL in sortedFiles) {
- if ([_fileManager removeItemAtURL:fileURL error:nil]) {
- NSDictionary<NSString *, id> *resourceValues = cacheFiles[fileURL];
- NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
- currentCacheSize -= totalAllocatedSize.unsignedIntegerValue;
- if (currentCacheSize < desiredCacheSize) {
- break;
- }
- }
- }
- }
- if (completionBlock) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completionBlock();
- });
- }
- });
- }
- #if SD_UIKIT
- - (void)backgroundDeleteOldFiles {
- Class UIApplicationClass = NSClassFromString(@"UIApplication");
- if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) {
- return;
- }
- UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
- __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
- // Clean up any unfinished task business by marking where you
- // stopped or ending the task outright.
- [application endBackgroundTask:bgTask];
- bgTask = UIBackgroundTaskInvalid;
- }];
- // Start the long-running task and return immediately.
- [self deleteOldFilesWithCompletionBlock:^{
- [application endBackgroundTask:bgTask];
- bgTask = UIBackgroundTaskInvalid;
- }];
- }
- #endif
- #pragma mark - Cache Info
- - (NSUInteger)getSize {
- __block NSUInteger size = 0;
- dispatch_sync(self.ioQueue, ^{
- NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
- for (NSString *fileName in fileEnumerator) {
- NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
- NSDictionary<NSString *, id> *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
- size += [attrs fileSize];
- }
- });
- return size;
- }
- - (NSUInteger)getDiskCount {
- __block NSUInteger count = 0;
- dispatch_sync(self.ioQueue, ^{
- NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
- count = fileEnumerator.allObjects.count;
- });
- return count;
- }
- - (void)calculateSizeWithCompletionBlock:(nullable SDWebImageCalculateSizeBlock)completionBlock {
- NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
- dispatch_async(self.ioQueue, ^{
- NSUInteger fileCount = 0;
- NSUInteger totalSize = 0;
- NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
- includingPropertiesForKeys:@[NSFileSize]
- options:NSDirectoryEnumerationSkipsHiddenFiles
- errorHandler:NULL];
- for (NSURL *fileURL in fileEnumerator) {
- NSNumber *fileSize;
- [fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:NULL];
- totalSize += fileSize.unsignedIntegerValue;
- fileCount += 1;
- }
- if (completionBlock) {
- dispatch_async(dispatch_get_main_queue(), ^{
- completionBlock(fileCount, totalSize);
- });
- }
- });
- }
- @end
|