A lightweight, production-ready image caching library for SwiftUI using Swift's native concurrency.
- Two-layer caching — Fast in-memory cache backed by persistent disk storage
- Swift concurrency — Built entirely with
actorandasync/await - Request coalescing — Concurrent requests for the same URL share one download
- Automatic expiration — 30-day TTL with configurable policy
- Zero dependencies — No Combine, no third-party libraries
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/iamducnhat/CachedAsyncImage.git", from: "1.0.0")
]Or in Xcode: File → Add Package Dependencies → paste the repository URL.
import CachedAsyncImage
struct ContentView: View {
var body: some View {
CachedAsyncImage(url: URL(string: "https://example.com/image.jpg")) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.aspectRatio(contentMode: .fit)
case .failure:
Image(systemName: "photo")
}
}
}
}// Fetch image
let image = try await ImageCache.shared.image(for: url)
// Prefetch
await ImageCache.shared.prefetch(urls: [url1, url2, url3])
// Clear cache
try await ImageCache.shared.clearAll()
// Cleanup expired
await ImageCache.shared.cleanupExpired()Request → Memory Cache → Disk Cache → Network
↓ ↓
(hit) (hit + valid)
↓ ↓
Return ←──────────┘
| Component | Description |
|---|---|
MemoryCache |
NSCache wrapper with auto-eviction |
DiskCache |
Actor-protected FileManager storage |
ImageCacheActor |
Coordinator with request coalescing |
CachePolicy |
30-day expiration logic |
- iOS 15.0+ / macOS 12.0+ / tvOS 15.0+ / watchOS 8.0+
- Swift 6.0+
MIT License. See LICENSE for details.