Remove favicon and feed icon image caches every 3 days

This commit is contained in:
Maurice Parker 2019-09-16 17:09:49 -05:00
parent 18d6a278c7
commit f977a5e0cb
2 changed files with 32 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import UIKit
struct AppDefaults {
struct Key {
static let lastImageCacheFlushDate = "lastImageCacheFlushDate"
static let firstRunDate = "firstRunDate"
static let timelineGroupByFeed = "timelineGroupByFeed"
static let timelineNumberOfLines = "timelineNumberOfLines"
@ -27,6 +28,15 @@ struct AppDefaults {
return true
}()
static var lastImageCacheFlushDate: Date? {
get {
return date(for: Key.lastImageCacheFlushDate)
}
set {
setDate(for: Key.lastImageCacheFlushDate, newValue)
}
}
static var refreshInterval: RefreshInterval {
get {
let rawValue = UserDefaults.standard.integer(forKey: Key.refreshInterval)
@ -74,7 +84,8 @@ struct AppDefaults {
}
static func registerDefaults() {
let defaults: [String : Any] = [Key.refreshInterval: RefreshInterval.everyHour.rawValue,
let defaults: [String : Any] = [Key.lastImageCacheFlushDate: Date(),
Key.refreshInterval: RefreshInterval.everyHour.rawValue,
Key.timelineGroupByFeed: false,
Key.timelineNumberOfLines: 3,
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue]

View File

@ -178,12 +178,31 @@ private extension AppDelegate {
private func initializeDownloaders() {
let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
let faviconsFolderURL = tempDir.appendingPathComponent("Favicons")
let imagesFolderURL = tempDir.appendingPathComponent("Images")
let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist")
// If the image disk cache hasn't been flushed for 3 days and the network is available, delete it
if let flushDate = AppDefaults.lastImageCacheFlushDate, flushDate.addingTimeInterval(3600*24*3) < Date() {
if let reachability = try? Reachability(hostname: "apple.com") {
if reachability.connection != .unavailable {
for tempItem in [faviconsFolderURL, imagesFolderURL, homePageToIconURL] {
do {
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
try FileManager.default.removeItem(at: tempItem)
} catch {
os_log(.error, log: self.log, "Could not delete cache file: %@", error.localizedDescription)
}
}
AppDefaults.lastImageCacheFlushDate = Date()
}
}
}
try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil)
let faviconsFolder = faviconsFolderURL.absoluteString
let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7))
faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath))
let imagesFolderURL = tempDir.appendingPathComponent("Images")
let imagesFolder = imagesFolderURL.absoluteString
let imagesFolderPath = imagesFolder.suffix(from: imagesFolder.index(imagesFolder.startIndex, offsetBy: 7))
try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)