This cache could download and hold any type of data. Cache size could be explicitly defined. Since it uses NSCache, out of memory situations are also gracefully handled.
For first-hand experience, just open the sample project and run it.
Copy cache folder to your project. That's it.
FileCache.sharedInstance.fetchFileForURL(urlString: "the-url-you-request")
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveFileCacheDidDownloadFile(notification:)), name: Notification.Name.init(rawValue: notificationFileCacheDidDownloadFile), object: nil)
If you have multiple requests initiated check the urlString property in the returned object to verify the notification.
@objc func didReceiveFileCacheDidDownloadFile(notification: Notification) {
if let cachedFile: FCFile = notification.object as? FCFile {
if cachedFile.urlString == "the-url-you-requested" {
if let data: NSData = cachedFile.fileData {
//Do something with the data received
}
}
}
}
FileCache.sharedInstance.clearCacheForURL(urlString: "your-url")
FileCache.sharedInstance.maxCacheSize = 10.0 //size in mega bytes
The MIT License (MIT)