Skip to content

Instantly share code, notes, and snippets.

@sgl0v
Created November 3, 2019 18:15
Show Gist options
  • Save sgl0v/bcd83a3fc35cbf27dc0b34d5ebfc9ef8 to your computer and use it in GitHub Desktop.
Save sgl0v/bcd83a3fc35cbf27dc0b34d5ebfc9ef8 to your computer and use it in GitHub Desktop.
final class ImageCache {
// 1st level cache, that contains encoded images
private lazy var imageCache: NSCache<AnyObject, AnyObject> = {
let cache = NSCache<AnyObject, AnyObject>()
cache.countLimit = config.countLimit
return cache
}()
// 2nd level cache, that contains decoded images
private lazy var decodedImageCache: NSCache<AnyObject, AnyObject> = {
let cache = NSCache<AnyObject, AnyObject>()
cache.totalCostLimit = config.memoryLimit
return cache
}()
private let lock = NSLock()
private let config: Config
struct Config {
let countLimit: Int
let memoryLimit: Int
static let defaultConfig = Config(countLimit: 100, memoryLimit: 1024 * 1024 * 100) // 100 MB
}
init(config: Config = Config.defaultConfig) {
self.config = config
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment