Parcourir la source

Merge pull request #566 from onevcat/fix/forever-disk-cache

Support unlimited disk cache time
Wei Wang il y a 9 ans
Parent
commit
a3edece2c6
2 fichiers modifiés avec 12 ajouts et 11 suppressions
  1. 2 2
      .travis.yml
  2. 10 9
      Sources/ImageCache.swift

+ 2 - 2
.travis.yml

@@ -1,6 +1,6 @@
 language: objective-c
 os: osx
-osx_image: xcode8
+osx_image: xcode8.2
 
 env:
   matrix:
@@ -15,7 +15,7 @@ script:
 - |
     if [ "$TEST_TYPE" = iOS ]; then
       set -o pipefail
-      xcodebuild clean test -workspace Kingfisher.xcworkspace -scheme Kingfisher -destination "platform=iOS Simulator,name=iPhone 6 Plus" -enableCodeCoverage YES | xcpretty
+      xcodebuild clean test -workspace Kingfisher.xcworkspace -scheme Kingfisher -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.1' -enableCodeCoverage YES | xcpretty
     elif [ "$TEST_TYPE" = macOS ]; then
       set -o pipefail
       xcodebuild clean test -workspace Kingfisher.xcworkspace -scheme Kingfisher-macOS -sdk macosx -enableCodeCoverage YES | xcpretty

+ 10 - 9
Sources/ImageCache.swift

@@ -93,6 +93,7 @@ open class ImageCache {
     
     /// The longest time duration in second of the cache being stored in disk. 
     /// Default is 1 week (60 * 60 * 24 * 7 seconds).
+    /// Setting this to a negative value will make the disk cache never expiring.
     open var maxCachePeriodInSecond: TimeInterval = 60 * 60 * 24 * 7 //Cache exists for 1 week
     
     /// The largest disk size can be taken for the cache. It is the total 
@@ -475,7 +476,7 @@ open class ImageCache {
         
         let diskCacheURL = URL(fileURLWithPath: diskCachePath)
         let resourceKeys: Set<URLResourceKey> = [.isDirectoryKey, .contentAccessDateKey, .totalFileAllocatedSizeKey]
-        let expiredDate = Date(timeIntervalSinceNow: -maxCachePeriodInSecond)
+        let expiredDate = (maxCachePeriodInSecond < 0) ? nil : Date(timeIntervalSinceNow: -maxCachePeriodInSecond)
         
         var cachedFiles = [URL: URLResourceValues]()
         var urlsToDelete = [URL]()
@@ -493,14 +494,14 @@ open class ImageCache {
                         continue
                     }
                     
-                    if !onlyForCacheSize {
-                        // If this file is expired, add it to URLsToDelete
-                        if let lastAccessData = resourceValues.contentAccessDate {
-                            if (lastAccessData as NSDate).laterDate(expiredDate) == expiredDate {
-                                urlsToDelete.append(fileUrl)
-                                continue
-                            }
-                        }
+                    // If this file is expired, add it to URLsToDelete
+                    if !onlyForCacheSize,
+                       let expiredDate = expiredDate,
+                       let lastAccessData = resourceValues.contentAccessDate,
+                       (lastAccessData as NSDate).laterDate(expiredDate) == expiredDate
+                    {
+                        urlsToDelete.append(fileUrl)
+                        continue
                     }
 
                     if let fileSize = resourceValues.totalFileAllocatedSize {