Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created December 19, 2023 08:14
Show Gist options
  • Save laevandus/eeff3cc5331c65163c06cee5caec8698 to your computer and use it in GitHub Desktop.
Save laevandus/eeff3cc5331c65163c06cee5caec8698 to your computer and use it in GitHub Desktop.
struct AsyncPhotoCache: AsyncPhotoCaching {
private var storage: NSCache<NSString, UIImage>
static let shared = AsyncPhotoCache(countLimit: 10)
init(countLimit: Int) {
self.storage = NSCache()
self.storage.countLimit = countLimit
}
func store(_ image: UIImage, forID id: any Hashable) {
let key = cacheKey(for: id, size: image.size)
storage.setObject(image, forKey: key as NSString)
}
func image(for id: any Hashable, size: CGSize) -> UIImage? {
let key = cacheKey(for: id, size: size)
return storage.object(forKey: key as NSString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment