فهرست منبع

Merge pull request #1483 from onevcat/anghami-master

Anghami master
Wei Wang 5 سال پیش
والد
کامیت
d7fb524c89
2فایلهای تغییر یافته به همراه29 افزوده شده و 2 حذف شده
  1. 28 1
      Sources/Cache/DiskStorage.swift
  2. 1 1
      Tests/KingfisherTests/ImageDownloaderTests.swift

+ 28 - 1
Sources/Cache/DiskStorage.swift

@@ -47,6 +47,9 @@ public enum DiskStorage {
 
         let metaChangingQueue: DispatchQueue
 
+        var maybeCached : Set<String>?
+        let maybeCachedCheckingQueue = DispatchQueue(label: "com.onevcat.Kingfisher.maybeCachedCheckingQueue")
+
         /// Creates a disk storage with the given `DiskStorage.Config`.
         ///
         /// - Parameter config: The config used for this disk storage.
@@ -72,6 +75,19 @@ public enum DiskStorage {
             metaChangingQueue = DispatchQueue(label: cacheName)
 
             try prepareDirectory()
+
+            maybeCachedCheckingQueue.async {
+                do {
+                    self.maybeCached = Set()
+                    try config.fileManager.contentsOfDirectory(atPath: self.directoryURL.path).forEach { fileName in
+                        self.maybeCached?.insert(fileName)
+                    }
+                } catch {
+                    // Just disable the functionality if we fail to initialize it properly. This will just revert to
+                    // the behavior which is to check file existence on disk directly.
+                    self.maybeCached = nil
+                }
+            }
         }
 
         // Creates the storage folder.
@@ -135,6 +151,10 @@ public enum DiskStorage {
                     )
                 )
             }
+
+            maybeCachedCheckingQueue.async {
+                self.maybeCached?.insert(fileURL.lastPathComponent)
+            }
         }
 
         func value(forKey key: String, extendingExpiration: ExpirationExtending = .cacheTime) throws -> T? {
@@ -150,6 +170,13 @@ public enum DiskStorage {
             let fileManager = config.fileManager
             let fileURL = cacheFileURL(forKey: key)
             let filePath = fileURL.path
+
+            let fileMaybeCached = maybeCachedCheckingQueue.sync {
+                return maybeCached?.contains(fileURL.lastPathComponent) ?? true
+            }
+            guard fileMaybeCached else {
+                return nil
+            }
             guard fileManager.fileExists(atPath: filePath) else {
                 return nil
             }
@@ -228,7 +255,7 @@ public enum DiskStorage {
         /// the `cacheKey` of an image `Source`. It is the computed key with processor identifier considered.
         public func cacheFileURL(forKey key: String) -> URL {
             let fileName = cacheFileName(forKey: key)
-            return directoryURL.appendingPathComponent(fileName)
+            return directoryURL.appendingPathComponent(fileName, isDirectory: false)
         }
 
         func cacheFileName(forKey key: String) -> String {

+ 1 - 1
Tests/KingfisherTests/ImageDownloaderTests.swift

@@ -100,7 +100,7 @@ class ImageDownloaderTests: XCTestCase {
         }
 
         group.notify(queue: .main, execute: exp.fulfill)
-        waitForExpectations(timeout: 3, handler: nil)
+        waitForExpectations(timeout: 5, handler: nil)
     }
     
     func testDownloadWithModifyingRequest() {