DiskCache is lightweight caching libary intended to persist arbitrary data to disk.
Swift 5.5x toolchain with Swift Package Manager, iOS 13
📦 Swift Package Manager (recommended)
DiskCache to your
.package(url: "https://github.com/Mobelux/DiskCache.git", from: "2.0.0"),
DiskCache with a
let cache = try DiskCache(storageType: .temporary(nil))
There are three storage type options, which inherently define the root directory where the cache resides:
temporary - Stores data in the user's
Cache directory. This directory is subject to the system's normal cache purging rules. Data stored here should be assumed to be ephemeral and could be purged by the system at any time.
permanent - Stores data in the user's
Documents directory. This will not be intentionally purged by the system and is safe to store long(er) term data.
shared - Stores data in the app's shared container with the given
appGroupID. This type is great for sharing data between app and extension or sibling apps.
let imageData = ... try await cache.cache(imageData, key: "cool-image")
Get cached data:
var data = try await cache.data("cool-image")
Note: if data has not been cached with the given key, an error will be thrown. The code of this error will be
Delete cached data:
try await cache.delete("cool-image")
Delete all data:
try await cache.deleteAll()
DiskCache is release under MIT licensing.