A Swift Package for easy in-memory caching of properties using Property Wrappers.
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
.
dependencies: [
.package(url: "https://github.com/cybozu/CachePropertyKit.git", from: "1.0.0")
]
A property wrapper that caches the wrapped value at runtime.
import CachePropertyKit
struct Repository {
func fetch() async throws -> Data {
@Cache(key: "repository_data", lifetime: .duration(3600))
var data: Data?
if let data { // Cached data exists and has not expired.
return data
} else { // Cached data does not exist or has expired.
let newData = try? await fetchNewData()
data = newData
return newData
}
}
}
A class for managing cached data from outside the property wrapper.
CacheContainer.clearAll() // Remove all cached data.
CacheContainer.clearAll(where: { key in key == "repository_data" }) // Remove the specified cached data.