ResourceCacher

public class ResourceCacher<Resource> where Resource : Decodable, Resource : Encodable

A storage to persist a resource and it’s etag, when available.

Resource is stored in the DataStore at the provided fileName, while the etag will be stored at \(fileName)_etag

  • Creates a resource cacher with the specified data store and file name.

    Declaration

    Swift

    public init(dataStore: any DataStore, fileName: String)

    Parameters

    dataStore

    The data store to use for persistence.

    fileName

    The file name for storing the resource.

  • Returns the stored resource, if present and successfully decodable.

    Declaration

    Swift

    public func readResource() -> Resource?
  • Returns the stored etag, if present.

    Declaration

    Swift

    public func readEtag() -> String?
  • Saves the provided resource with the etag, if provided, in a transaction.

    Both the resource and the etag will override previous resources stored at the same location. In case of a nil etag any previous etag present will be deleted.

    Declaration

    Swift

    public func saveResource(_ resource: Resource, etag: String?) throws

    Parameters

    resource

    the resource to be stored.

    etag

    the etag to save along with this resource.