Cache is a Swift library for caching Identifiable
values with optional expiry logic. It supports both in-memory and file-backed storage, making it suitable for short-lived data, offline persistence, or resource caching.
- ✅ Type-safe caching for any
Identifiable
type - 📦 Two interchangeable storage implementations:
VolatileCache
: fast in-memory storageFileSystemCache
: persistent, file-backed storage
- ⏱ Expiry support:
.short
or.custom(Date)
- 🧪 Testable without delays (no need for
sleep
) - 🕹 Native async/await support. Fully thread safe and sendable.
- 🧩 Easily injectable via
swift-dependencies
let cache = VolatileCache<MyModel>()
try await cache.stash(MyModel(id: "a", name: "Temp"), duration: .short)
let item = try await cache.resource(for: "a")
let cache = try FileSystemCache<MyModel>(directory: .caches, "games")
try await cache.stash(MyModel(id: "b", name: "something"), duration: .custom(.distantFuture))
let item = try await cache.resource(for: "b")
If you're using swift-dependencies
, you can expose your preferred cache type as a dependency:
import Dependencies
import Cache
extension DependencyValues {
/// A cache for storing and retrieving `Sport` models.
var sportCache: any Cache<Sport> {
get { self[SportCacheKey.self] }
set { self[SportCacheKey.self] = newValue }
}
}
private enum SportCacheKey: DependencyKey {
static let liveValue: any Cache<Sport> = FileSystemCache(
directory: .caches,
subfolder: "sports"
)
}
Then use it like this:
@Dependency(\.sportCache) var sportCache
let sport = try await sportCache.resource(for: id)