Fast and efficient image caching library for iOS
- Memory + Disk Caching: Automatic multi-level caching
- Customizable Policies: Extensible cache eviction strategies
- Thumbnail Downsampling (Memory-only): Thumbnails are generated on the fly and stored only in memory. The disk cache stores only the original image to reduce disk usage and avoid redundant file writes.
- Swift Concurrency: Modern async/await API
- Flexible Strategy: Memory-only, disk-only, both, or none
- Disk, Memory Policy: Disk & Memory Policy: Auto-eviction (LRU), TTL, cost limit
dependencies: [
.package(url: "https://github.com/hyeonghwan/HwanCache.git", from: "0.1.0")
]import HwanCache
// 1. Implement HWImageDownloader protocol
struct MyImageDownloader: HWImageDownloader {
func downloadImage(from urlString: String) async throws -> Data {
// Your download implementation
let (data, _) = try await URLSession.shared.data(from: URL(string: urlString)!)
return data
}
}
// 2. Create image service
let downloader = MyImageDownloader()
let imageService = HWDefaultImageService(downloader: downloader)// Load original image
let image = try await imageService.loadImage(
from: url,
displayMode: .original,
cacheStrategy: .both // Memory + Disk
)
// Load thumbnail
let thumbnail = try await imageService.loadImage(
from: url,
displayMode: .thumbnail(size: CGSize(width: 100, height: 100)),
cacheStrategy: .both
)// Memory only (fast, cleared on app restart)
.memoryOnly
// Disk only (persistent, slower access)
.diskOnly
// Both (recommended)
.both
// No caching
.nonelet cacheManager = HWCacheManager.shared
// Clear memory cache
cacheManager.clearMemoryCache()
// Clear disk cache
await cacheManager.clearDiskCache()
// Clear all
await cacheManager.clearAllCache()
// Get disk cache size
let size = await cacheManager.diskCacheSize()
print("Cache size: \(size) bytes")Library/Caches/HwanCache/ |-- 3a7b19fef8cd... // original image for URL
HwanCache stores only original image data in disk cache. Therefore the disk cache key is derived solely from the original URL (SHA256 hashed), and does not include thumbnail size or display options.
Thumbnails are always memory-only and derived from the original disk data.
- iOS 13.0+
- Swift 5.9+
MIT License