123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- #if os(macOS)
- import AppKit
- #else
- import UIKit
- #endif
- extension Notification.Name {
-
-
-
-
-
-
-
-
- public static let KingfisherDidCleanDiskCache =
- Notification.Name("com.onevcat.Kingfisher.KingfisherDidCleanDiskCache")
- }
- public let KingfisherDiskCacheCleanedHashKey = "com.onevcat.Kingfisher.cleanedHash"
- public enum CacheType {
-
- case none
-
- case memory
-
- case disk
-
-
- public var cached: Bool {
- switch self {
- case .memory, .disk: return true
- case .none: return false
- }
- }
- }
- public struct CacheStoreResult {
-
-
- public let memoryCacheResult: Result<(), Never>
-
-
-
- public let diskCacheResult: Result<(), KingfisherError>
- }
- extension KFCrossPlatformImage: CacheCostCalculable {
-
- public var cacheCost: Int { return kf.cost }
- }
- extension Data: DataTransformable {
- public func toData() throws -> Data {
- return self
- }
- public static func fromData(_ data: Data) throws -> Data {
- return data
- }
- public static let empty = Data()
- }
- public enum ImageCacheResult {
-
-
- case disk(KFCrossPlatformImage)
-
-
- case memory(KFCrossPlatformImage)
-
-
- case none
-
-
-
- public var image: KFCrossPlatformImage? {
- switch self {
- case .disk(let image): return image
- case .memory(let image): return image
- case .none: return nil
- }
- }
-
-
- public var cacheType: CacheType {
- switch self {
- case .disk: return .disk
- case .memory: return .memory
- case .none: return .none
- }
- }
- }
- open class ImageCache {
-
-
-
-
- public static let `default` = ImageCache(name: "default")
-
-
-
-
- public let memoryStorage: MemoryStorage.Backend<KFCrossPlatformImage>
-
-
-
-
- public let diskStorage: DiskStorage.Backend<Data>
-
- private let ioQueue: DispatchQueue
-
-
- public typealias DiskCachePathClosure = (URL, String) -> URL
-
-
-
-
-
-
- public init(
- memoryStorage: MemoryStorage.Backend<KFCrossPlatformImage>,
- diskStorage: DiskStorage.Backend<Data>)
- {
- self.memoryStorage = memoryStorage
- self.diskStorage = diskStorage
- let ioQueueName = "com.onevcat.Kingfisher.ImageCache.ioQueue.\(UUID().uuidString)"
- ioQueue = DispatchQueue(label: ioQueueName)
- let notifications: [(Notification.Name, Selector)]
- #if !os(macOS) && !os(watchOS)
- notifications = [
- (UIApplication.didReceiveMemoryWarningNotification, #selector(clearMemoryCache)),
- (UIApplication.willTerminateNotification, #selector(cleanExpiredDiskCache)),
- (UIApplication.didEnterBackgroundNotification, #selector(backgroundCleanExpiredDiskCache))
- ]
- #elseif os(macOS)
- notifications = [
- (NSApplication.willResignActiveNotification, #selector(cleanExpiredDiskCache)),
- ]
- #else
- notifications = []
- #endif
- notifications.forEach {
- NotificationCenter.default.addObserver(self, selector: $0.1, name: $0.0, object: nil)
- }
- }
-
-
-
-
-
-
-
- public convenience init(name: String) {
- self.init(noThrowName: name, cacheDirectoryURL: nil, diskCachePathClosure: nil)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public convenience init(
- name: String,
- cacheDirectoryURL: URL?,
- diskCachePathClosure: DiskCachePathClosure? = nil
- ) throws
- {
- if name.isEmpty {
- fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
- }
- let memoryStorage = ImageCache.createMemoryStorage()
- let config = ImageCache.createConfig(
- name: name, cacheDirectoryURL: cacheDirectoryURL, diskCachePathClosure: diskCachePathClosure
- )
- let diskStorage = try DiskStorage.Backend<Data>(config: config)
- self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
- }
- convenience init(
- noThrowName name: String,
- cacheDirectoryURL: URL?,
- diskCachePathClosure: DiskCachePathClosure?
- )
- {
- if name.isEmpty {
- fatalError("[Kingfisher] You should specify a name for the cache. A cache with empty name is not permitted.")
- }
- let memoryStorage = ImageCache.createMemoryStorage()
- let config = ImageCache.createConfig(
- name: name, cacheDirectoryURL: cacheDirectoryURL, diskCachePathClosure: diskCachePathClosure
- )
- let diskStorage = DiskStorage.Backend<Data>(noThrowConfig: config, creatingDirectory: true)
- self.init(memoryStorage: memoryStorage, diskStorage: diskStorage)
- }
- private static func createMemoryStorage() -> MemoryStorage.Backend<KFCrossPlatformImage> {
- let totalMemory = ProcessInfo.processInfo.physicalMemory
- let costLimit = totalMemory / 4
- let memoryStorage = MemoryStorage.Backend<KFCrossPlatformImage>(config:
- .init(totalCostLimit: (costLimit > Int.max) ? Int.max : Int(costLimit)))
- return memoryStorage
- }
- private static func createConfig(
- name: String,
- cacheDirectoryURL: URL?,
- diskCachePathClosure: DiskCachePathClosure? = nil
- ) -> DiskStorage.Config
- {
- var diskConfig = DiskStorage.Config(
- name: name,
- sizeLimit: 0,
- directory: cacheDirectoryURL
- )
- if let closure = diskCachePathClosure {
- diskConfig.cachePathBlock = closure
- }
- return diskConfig
- }
-
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
-
- open func store(_ image: KFCrossPlatformImage,
- original: Data? = nil,
- forKey key: String,
- options: KingfisherParsedOptionsInfo,
- toDisk: Bool = true,
- completionHandler: ((CacheStoreResult) -> Void)? = nil)
- {
- let identifier = options.processor.identifier
- let callbackQueue = options.callbackQueue
-
- let computedKey = key.computedKey(with: identifier)
-
- memoryStorage.storeNoThrow(value: image, forKey: computedKey, expiration: options.memoryCacheExpiration)
-
- guard toDisk else {
- if let completionHandler = completionHandler {
- let result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
- callbackQueue.execute { completionHandler(result) }
- }
- return
- }
-
- ioQueue.async {
- let serializer = options.cacheSerializer
- if let data = serializer.data(with: image, original: original) {
- self.syncStoreToDisk(
- data,
- forKey: key,
- processorIdentifier: identifier,
- callbackQueue: callbackQueue,
- expiration: options.diskCacheExpiration,
- completionHandler: completionHandler)
- } else {
- guard let completionHandler = completionHandler else { return }
-
- let diskError = KingfisherError.cacheError(
- reason: .cannotSerializeImage(image: image, original: original, serializer: serializer))
- let result = CacheStoreResult(
- memoryCacheResult: .success(()),
- diskCacheResult: .failure(diskError))
- callbackQueue.execute { completionHandler(result) }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- open func store(_ image: KFCrossPlatformImage,
- original: Data? = nil,
- forKey key: String,
- processorIdentifier identifier: String = "",
- cacheSerializer serializer: CacheSerializer = DefaultCacheSerializer.default,
- toDisk: Bool = true,
- callbackQueue: CallbackQueue = .untouch,
- completionHandler: ((CacheStoreResult) -> Void)? = nil)
- {
- struct TempProcessor: ImageProcessor {
- let identifier: String
- func process(item: ImageProcessItem, options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage? {
- return nil
- }
- }
-
- let options = KingfisherParsedOptionsInfo([
- .processor(TempProcessor(identifier: identifier)),
- .cacheSerializer(serializer),
- .callbackQueue(callbackQueue)
- ])
- store(image, original: original, forKey: key, options: options,
- toDisk: toDisk, completionHandler: completionHandler)
- }
-
- open func storeToDisk(
- _ data: Data,
- forKey key: String,
- processorIdentifier identifier: String = "",
- expiration: StorageExpiration? = nil,
- callbackQueue: CallbackQueue = .untouch,
- completionHandler: ((CacheStoreResult) -> Void)? = nil)
- {
- ioQueue.async {
- self.syncStoreToDisk(
- data,
- forKey: key,
- processorIdentifier: identifier,
- callbackQueue: callbackQueue,
- expiration: expiration,
- completionHandler: completionHandler)
- }
- }
-
- private func syncStoreToDisk(
- _ data: Data,
- forKey key: String,
- processorIdentifier identifier: String = "",
- callbackQueue: CallbackQueue = .untouch,
- expiration: StorageExpiration? = nil,
- completionHandler: ((CacheStoreResult) -> Void)? = nil)
- {
- let computedKey = key.computedKey(with: identifier)
- let result: CacheStoreResult
- do {
- try self.diskStorage.store(value: data, forKey: computedKey, expiration: expiration)
- result = CacheStoreResult(memoryCacheResult: .success(()), diskCacheResult: .success(()))
- } catch {
- let diskError: KingfisherError
- if let error = error as? KingfisherError {
- diskError = error
- } else {
- diskError = .cacheError(reason: .cannotConvertToData(object: data, error: error))
- }
-
- result = CacheStoreResult(
- memoryCacheResult: .success(()),
- diskCacheResult: .failure(diskError)
- )
- }
- if let completionHandler = completionHandler {
- callbackQueue.execute { completionHandler(result) }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- open func removeImage(forKey key: String,
- processorIdentifier identifier: String = "",
- fromMemory: Bool = true,
- fromDisk: Bool = true,
- callbackQueue: CallbackQueue = .untouch,
- completionHandler: (() -> Void)? = nil)
- {
- let computedKey = key.computedKey(with: identifier)
- if fromMemory {
- memoryStorage.remove(forKey: computedKey)
- }
-
- if fromDisk {
- ioQueue.async{
- try? self.diskStorage.remove(forKey: computedKey)
- if let completionHandler = completionHandler {
- callbackQueue.execute { completionHandler() }
- }
- }
- } else {
- if let completionHandler = completionHandler {
- callbackQueue.execute { completionHandler() }
- }
- }
- }
- func retrieveImage(forKey key: String,
- options: KingfisherParsedOptionsInfo,
- callbackQueue: CallbackQueue = .mainCurrentOrAsync,
- completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
- {
-
- guard let completionHandler = completionHandler else { return }
-
- if let image = retrieveImageInMemoryCache(forKey: key, options: options) {
- callbackQueue.execute { completionHandler(.success(.memory(image))) }
- } else if options.fromMemoryCacheOrRefresh {
- callbackQueue.execute { completionHandler(.success(.none)) }
- } else {
-
- self.retrieveImageInDiskCache(forKey: key, options: options, callbackQueue: callbackQueue) {
- result in
- switch result {
- case .success(let image):
- guard let image = image else {
-
- callbackQueue.execute { completionHandler(.success(.none)) }
- return
- }
-
-
-
- var cacheOptions = options
- cacheOptions.callbackQueue = .untouch
- self.store(
- image,
- forKey: key,
- options: cacheOptions,
- toDisk: false)
- {
- _ in
- callbackQueue.execute { completionHandler(.success(.disk(image))) }
- }
- case .failure(let error):
- callbackQueue.execute { completionHandler(.failure(error)) }
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- open func retrieveImage(forKey key: String,
- options: KingfisherOptionsInfo? = nil,
- callbackQueue: CallbackQueue = .mainCurrentOrAsync,
- completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
- {
- retrieveImage(
- forKey: key,
- options: KingfisherParsedOptionsInfo(options),
- callbackQueue: callbackQueue,
- completionHandler: completionHandler)
- }
- func retrieveImageInMemoryCache(
- forKey key: String,
- options: KingfisherParsedOptionsInfo) -> KFCrossPlatformImage?
- {
- let computedKey = key.computedKey(with: options.processor.identifier)
- return memoryStorage.value(forKey: computedKey, extendingExpiration: options.memoryCacheAccessExtendingExpiration)
- }
-
-
-
-
-
-
-
- open func retrieveImageInMemoryCache(
- forKey key: String,
- options: KingfisherOptionsInfo? = nil) -> KFCrossPlatformImage?
- {
- return retrieveImageInMemoryCache(forKey: key, options: KingfisherParsedOptionsInfo(options))
- }
- func retrieveImageInDiskCache(
- forKey key: String,
- options: KingfisherParsedOptionsInfo,
- callbackQueue: CallbackQueue = .untouch,
- completionHandler: @escaping (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
- {
- let computedKey = key.computedKey(with: options.processor.identifier)
- let loadingQueue: CallbackQueue = options.loadDiskFileSynchronously ? .untouch : .dispatch(ioQueue)
- loadingQueue.execute {
- do {
- var image: KFCrossPlatformImage? = nil
- if let data = try self.diskStorage.value(forKey: computedKey, extendingExpiration: options.diskCacheAccessExtendingExpiration) {
- image = options.cacheSerializer.image(with: data, options: options)
- }
- callbackQueue.execute { completionHandler(.success(image)) }
- } catch {
- if let error = error as? KingfisherError {
- callbackQueue.execute { completionHandler(.failure(error)) }
- } else {
- assertionFailure("The internal thrown error should be a `KingfisherError`.")
- }
- }
- }
- }
-
-
-
-
-
-
-
-
- open func retrieveImageInDiskCache(
- forKey key: String,
- options: KingfisherOptionsInfo? = nil,
- callbackQueue: CallbackQueue = .untouch,
- completionHandler: @escaping (Result<KFCrossPlatformImage?, KingfisherError>) -> Void)
- {
- retrieveImageInDiskCache(
- forKey: key,
- options: KingfisherParsedOptionsInfo(options),
- callbackQueue: callbackQueue,
- completionHandler: completionHandler)
- }
-
-
-
-
-
- public func clearCache(completion handler: (() -> Void)? = nil) {
- clearMemoryCache()
- clearDiskCache(completion: handler)
- }
-
-
- @objc public func clearMemoryCache() {
- memoryStorage.removeAll()
- }
-
-
-
-
-
- open func clearDiskCache(completion handler: (() -> Void)? = nil) {
- ioQueue.async {
- do {
- try self.diskStorage.removeAll()
- } catch _ { }
- if let handler = handler {
- DispatchQueue.main.async { handler() }
- }
- }
- }
-
-
- open func cleanExpiredCache(completion handler: (() -> Void)? = nil) {
- cleanExpiredMemoryCache()
- cleanExpiredDiskCache(completion: handler)
- }
-
- open func cleanExpiredMemoryCache() {
- memoryStorage.removeExpired()
- }
-
-
- @objc func cleanExpiredDiskCache() {
- cleanExpiredDiskCache(completion: nil)
- }
-
-
-
-
- open func cleanExpiredDiskCache(completion handler: (() -> Void)? = nil) {
- ioQueue.async {
- do {
- var removed: [URL] = []
- let removedExpired = try self.diskStorage.removeExpiredValues()
- removed.append(contentsOf: removedExpired)
- let removedSizeExceeded = try self.diskStorage.removeSizeExceededValues()
- removed.append(contentsOf: removedSizeExceeded)
- if !removed.isEmpty {
- DispatchQueue.main.async {
- let cleanedHashes = removed.map { $0.lastPathComponent }
- NotificationCenter.default.post(
- name: .KingfisherDidCleanDiskCache,
- object: self,
- userInfo: [KingfisherDiskCacheCleanedHashKey: cleanedHashes])
- }
- }
- if let handler = handler {
- DispatchQueue.main.async { handler() }
- }
- } catch {}
- }
- }
- #if !os(macOS) && !os(watchOS)
-
-
-
- @objc public func backgroundCleanExpiredDiskCache() {
-
- guard let sharedApplication = KingfisherWrapper<UIApplication>.shared else { return }
- func endBackgroundTask(_ task: inout UIBackgroundTaskIdentifier) {
- sharedApplication.endBackgroundTask(task)
- task = UIBackgroundTaskIdentifier.invalid
- }
-
- var backgroundTask: UIBackgroundTaskIdentifier!
- backgroundTask = sharedApplication.beginBackgroundTask {
- endBackgroundTask(&backgroundTask!)
- }
-
- cleanExpiredDiskCache {
- endBackgroundTask(&backgroundTask!)
- }
- }
- #endif
-
-
-
-
-
-
-
-
-
-
-
- open func imageCachedType(
- forKey key: String,
- processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> CacheType
- {
- let computedKey = key.computedKey(with: identifier)
- if memoryStorage.isCached(forKey: computedKey) { return .memory }
- if diskStorage.isCached(forKey: computedKey) { return .disk }
- return .none
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public func isCached(
- forKey key: String,
- processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> Bool
- {
- return imageCachedType(forKey: key, processorIdentifier: identifier).cached
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- open func hash(
- forKey key: String,
- processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
- {
- let computedKey = key.computedKey(with: identifier)
- return diskStorage.cacheFileName(forKey: computedKey)
- }
-
-
-
-
-
- open func calculateDiskStorageSize(completion handler: @escaping ((Result<UInt, KingfisherError>) -> Void)) {
- ioQueue.async {
- do {
- let size = try self.diskStorage.totalSize()
- DispatchQueue.main.async { handler(.success(size)) }
- } catch {
- if let error = error as? KingfisherError {
- DispatchQueue.main.async { handler(.failure(error)) }
- } else {
- assertionFailure("The internal thrown error should be a `KingfisherError`.")
- }
-
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- open func cachePath(
- forKey key: String,
- processorIdentifier identifier: String = DefaultImageProcessor.default.identifier) -> String
- {
- let computedKey = key.computedKey(with: identifier)
- return diskStorage.cacheFileURL(forKey: computedKey).path
- }
- }
- extension Dictionary {
- func keysSortedByValue(_ isOrderedBefore: (Value, Value) -> Bool) -> [Key] {
- return Array(self).sorted{ isOrderedBefore($0.1, $1.1) }.map{ $0.0 }
- }
- }
- #if !os(macOS) && !os(watchOS)
- extension UIApplication: KingfisherCompatible { }
- extension KingfisherWrapper where Base: UIApplication {
- public static var shared: UIApplication? {
- let selector = NSSelectorFromString("sharedApplication")
- guard Base.responds(to: selector) else { return nil }
- return Base.perform(selector).takeUnretainedValue() as? UIApplication
- }
- }
- #endif
- extension String {
- func computedKey(with identifier: String) -> String {
- if identifier.isEmpty {
- return self
- } else {
- return appending("@\(identifier)")
- }
- }
- }
|