* iOS switch from file cache to memory cache and use last_picture_update to update the avatar if needed * Android switch from file cache to memory cache and use last_picture_update to update the avatar if needed, split function to multiple files and catch potential exceptions
31 lines
864 B
Swift
31 lines
864 B
Swift
import Foundation
|
|
|
|
public final class ImageCache: ImageCacheType {
|
|
public static let `default` = ImageCache()
|
|
struct Config {
|
|
let countLimit: Int
|
|
let memoryLimit: Int
|
|
|
|
static let defaultConfig = Config(countLimit: 50, memoryLimit: 1024 * 1024 * 50)
|
|
}
|
|
|
|
lazy var imageCache: NSCache<NSString, NSData> = {
|
|
let cache = NSCache<NSString, NSData>()
|
|
cache.countLimit = config.countLimit
|
|
cache.totalCostLimit = config.memoryLimit
|
|
return cache
|
|
}()
|
|
|
|
lazy var keysCache: NSCache<NSString, NSString> = {
|
|
let cache = NSCache<NSString, NSString>()
|
|
cache.countLimit = config.countLimit
|
|
return cache
|
|
}()
|
|
|
|
let lock = NSLock()
|
|
let config: Config
|
|
|
|
private init(config: Config = Config.defaultConfig) {
|
|
self.config = config
|
|
}
|
|
}
|