Skip to content

iamducnhat/CachedAsyncImage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CachedAsyncImage

A lightweight, production-ready image caching library for SwiftUI using Swift's native concurrency.

Swift 6.0 Platforms License

Features

  • Two-layer caching — Fast in-memory cache backed by persistent disk storage
  • Swift concurrency — Built entirely with actor and async/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

Installation

Swift Package Manager

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.

Usage

SwiftUI View

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")
            }
        }
    }
}

Direct API

// 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()

Architecture

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

Requirements

  • iOS 15.0+ / macOS 12.0+ / tvOS 15.0+ / watchOS 8.0+
  • Swift 6.0+

License

MIT License. See LICENSE for details.

About

Load and save URL with 30 days cache.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages