Created
November 3, 2019 18:15
-
-
Save sgl0v/bcd83a3fc35cbf27dc0b34d5ebfc9ef8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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