Skip to content

Commit

Permalink
Prune cache more often
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGorman committed Feb 10, 2020
1 parent 041eeb6 commit 18f2054
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion MapleBacon.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MapleBacon'
s.version = '5.6.10'
s.version = '5.6.11'
s.swift_version = '5.1'
s.summary = 'A lightweight and fast image downloading library iOS.'

Expand Down
16 changes: 9 additions & 7 deletions MapleBacon/Core/DiskCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ final class DiskCache {
let path = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
cachePath = (path as NSString).appendingPathComponent(name)

NotificationCenter.default.addObserver(self,
selector: #selector(cleanDiskOnTermination),
name: UIApplication.willTerminateNotification,
object: nil)
let notifications = [UIApplication.willTerminateNotification, UIApplication.didEnterBackgroundNotification]
notifications.forEach { notification in
NotificationCenter.default.addObserver(self, selector: #selector(cleanDiskOnNotification), name: notification, object: nil)
}
}

func insert(_ value: Data?, forKey key: String, completion: (() -> Void)?) {
Expand Down Expand Up @@ -73,7 +73,7 @@ final class DiskCache {
}

@objc
private func cleanDiskOnTermination() {
private func cleanDiskOnNotification() {
cleanDisk(completion: nil)
}

Expand All @@ -90,7 +90,7 @@ final class DiskCache {

func expiredFileUrls() -> [URL] {
let cacheDirectory = URL(fileURLWithPath: cachePath)
let keys: Set<URLResourceKey> = [.isDirectoryKey, .contentAccessDateKey]
let keys: Set<URLResourceKey> = [.isDirectoryKey, .contentModificationDateKey]
let contents = try? backingStore.contentsOfDirectory(at: cacheDirectory, includingPropertiesForKeys: Array(keys),
options: .skipsHiddenFiles)
guard let files = contents else {
Expand All @@ -101,7 +101,9 @@ final class DiskCache {
let expiredFileUrls = files.filter { url in
let resource = try? url.resourceValues(forKeys: keys)
let isDirectory = resource?.isDirectory
guard let lastAccessDate = resource?.contentAccessDate else { return true }
guard let lastAccessDate = resource?.contentAccessDate else {
return true
}
return isDirectory == false && lastAccessDate < expirationDate
}
return expiredFileUrls
Expand Down

0 comments on commit 18f2054

Please sign in to comment.