Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created December 19, 2023 08:06
Show Gist options
  • Save laevandus/680b8c9ffb5f8acfa44a1863af0fe543 to your computer and use it in GitHub Desktop.
Save laevandus/680b8c9ffb5f8acfa44a1863af0fe543 to your computer and use it in GitHub Desktop.
/// An interface for caching images by identifier and size.
protocol AsyncPhotoCaching {
/// Store the specified image by size and identifier.
/// - Parameters:
/// - image: The image to be cached.
/// - id: The unique identifier of the image.
func store(_ image: UIImage, forID id: any Hashable)
/// Returns the image associated with a given id and size.
/// - Parameters:
/// - id: The unique identifier of the image.
/// - size: The size of the image stored in the cache.
/// - Returns: The image associated with id and size, or nil if no image is associated with id and size.
func image(for id: any Hashable, size: CGSize) -> UIImage?
/// Returns the caching key by combining a given image id and a size.
/// - Parameters:
/// - id: The unique identifier of the image.
/// - size: The size of the image stored in the cache.
/// - Returns: The caching key by combining a given id and size.
func cacheKey(for id: any Hashable, size: CGSize) -> String
}
extension AsyncPhotoCaching {
func cacheKey(for id: any Hashable, size: CGSize) -> String {
"\(id.hashValue):w\(Int(size.width))h\(Int(size.height))"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment