AutoPurgingImageCache

open class AutoPurgingImageCache : ImageRequestCache

The AutoPurgingImageCache in an in-memory image cache used to store images up to a given memory capacity. When the memory capacity is reached, the image cache is sorted by last access date, then the oldest image is continuously purged until the preferred memory usage after purge is met. Each time an image is accessed through the cache, the internal access date of the image is updated.

Properties

  • The current total memory usage in bytes of all images stored within the cache.

    Declaration

    Swift

    open var memoryUsage: UInt64 { get }
  • The total memory capacity of the cache in bytes.

    Declaration

    Swift

    public let memoryCapacity: UInt64
  • The preferred memory usage after purge in bytes. During a purge, images will be purged until the memory capacity drops below this limit.

    Declaration

    Swift

    public let preferredMemoryUsageAfterPurge: UInt64

Initialization

  • Initializes the AutoPurgingImageCache instance with the given memory capacity and preferred memory usage after purge limit.

    Please note, the memory capacity must always be greater than or equal to the preferred memory usage after purge.

    Declaration

    Swift

    public init(memoryCapacity: UInt64 = 100_000_000, preferredMemoryUsageAfterPurge: UInt64 = 60_000_000)

    Parameters

    memoryCapacity

    The total memory capacity of the cache in bytes. 100 MB by default.

    preferredMemoryUsageAfterPurge

    The preferred memory usage after purge in bytes. 60 MB by default.

    Return Value

    The new AutoPurgingImageCache instance.

Add Image to Cache

  • Adds the image to the cache using an identifier created from the request and optional identifier.

    Declaration

    Swift

    open func add(_ image: Image, for request: URLRequest, withIdentifier identifier: String? = nil)

    Parameters

    image

    The image to add to the cache.

    request

    The request used to generate the image’s unique identifier.

    identifier

    The additional identifier to append to the image’s unique identifier.

  • Adds the image to the cache with the given identifier.

    Declaration

    Swift

    open func add(_ image: Image, withIdentifier identifier: String)

    Parameters

    image

    The image to add to the cache.

    identifier

    The identifier to use to uniquely identify the image.

Remove Image from Cache

  • Removes the image from the cache using an identifier created from the request and optional identifier.

    Declaration

    Swift

    @discardableResult
    open func removeImage(for request: URLRequest, withIdentifier identifier: String?) -> Bool

    Parameters

    request

    The request used to generate the image’s unique identifier.

    identifier

    The additional identifier to append to the image’s unique identifier.

    Return Value

    true if the image was removed, false otherwise.

  • Removes all images from the cache created from the request.

    Declaration

    Swift

    @discardableResult
    open func removeImages(matching request: URLRequest) -> Bool

    Parameters

    request

    The request used to generate the image’s unique identifier.

    Return Value

    true if any images were removed, false otherwise.

  • Removes the image from the cache matching the given identifier.

    Declaration

    Swift

    @discardableResult
    open func removeImage(withIdentifier identifier: String) -> Bool

    Parameters

    identifier

    The unique identifier for the image.

    Return Value

    true if the image was removed, false otherwise.

  • Removes all images stored in the cache.

    Declaration

    Swift

    @discardableResult
    @objc
    open func removeAllImages() -> Bool

    Return Value

    true if images were removed from the cache, false otherwise.

Fetch Image from Cache

  • Returns the image from the cache associated with an identifier created from the request and optional identifier.

    Declaration

    Swift

    open func image(for request: URLRequest, withIdentifier identifier: String? = nil) -> Image?

    Parameters

    request

    The request used to generate the image’s unique identifier.

    identifier

    The additional identifier to append to the image’s unique identifier.

    Return Value

    The image if it is stored in the cache, nil otherwise.

  • Returns the image in the cache associated with the given identifier.

    Declaration

    Swift

    open func image(withIdentifier identifier: String) -> Image?

    Parameters

    identifier

    The unique identifier for the image.

    Return Value

    The image if it is stored in the cache, nil otherwise.

Image Cache Keys

  • Returns the unique image cache key for the specified request and additional identifier.

    Declaration

    Swift

    open func imageCacheKey(for request: URLRequest, withIdentifier identifier: String?) -> String

    Parameters

    request

    The request.

    identifier

    The additional identifier.

    Return Value

    The unique image cache key.