https://github.com/nashysolutions/cache
A lightweight Swift library for caching Identifiable values with optional expiry, supporting both in-memory and file-backed storage. Designed for testability, composability, and use with swift-dependencies.
https://github.com/nashysolutions/cache
cache caching codable dependency-injection disk-cache expiry file-system in-memory-cache swift swift-concurrency swift-dependencies swift-package testable
Last synced: 4 months ago
JSON representation
A lightweight Swift library for caching Identifiable values with optional expiry, supporting both in-memory and file-backed storage. Designed for testability, composability, and use with swift-dependencies.
- Host: GitHub
- URL: https://github.com/nashysolutions/cache
- Owner: nashysolutions
- License: mit
- Created: 2022-07-23T03:36:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-06-15T03:50:51.000Z (8 months ago)
- Last Synced: 2025-06-22T08:05:56.655Z (8 months ago)
- Topics: cache, caching, codable, dependency-injection, disk-cache, expiry, file-system, in-memory-cache, swift, swift-concurrency, swift-dependencies, swift-package, testable
- Language: Swift
- Homepage:
- Size: 1.01 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache
[](https://swiftpackageindex.com/nashysolutions/cache)
[](https://swiftpackageindex.com/nashysolutions/cache)
**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.
---
## Features
- โ
Type-safe caching for any `Identifiable` type
- ๐ฆ Two interchangeable storage implementations:
- `VolatileCache`: fast in-memory storage
- `FileSystemCache`: 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`
---
## Usage
See the QuickStart guide in the `Documentation.docc` catalogue.
### Example
```swift
struct Cheese: Identifiable {
let id: Int
let name: String
}
let cache = VolatileCache()
try await cache.stash(Cheese(id: 0, name: "Brie"), duration: .short)
```
---
### Dependency Injection
An example using [`swift-dependencies`](https://github.com/pointfreeco/swift-dependencies).
```swift
import Dependencies
import Cache
extension DependencyValues {
/// A cache for storing and retrieving `Cheese` models.
var cheeseCache: any Cache {
get { self[CheeseCacheKey.self] }
set { self[CheeseCacheKey.self] = newValue }
}
}
private enum CheeseCacheKey: DependencyKey {
static let liveValue: any Cache = FileSystemCache(.caches, subfolder: "Cheeses")
}
```
Then use it like this:
```swift
struct MyModel {
@Dependency(\.cheeseCache) var cheeseCache
fucn loadCheese(id: Int) throws -> Cheese? {
try await cheeseCache.resource(for: id)
}
}
#Preview {
withDependencies {
$0.cheeseCache = MockCache()
} operation: {
ContentView()
}
}
private actor MockCache: Cache {
private var store: [Int: Cheese] = [:]
func stash(_ item: Cheese, duration: Expiry) async throws {
// Ignore `duration` for now; just stash in memory
store[item.id] = item
}
func removeResource(for identifier: Int) async throws {
store.removeValue(forKey: identifier)
}
func resource(for identifier: Int) async throws -> Cheese? {
store[identifier]
}
func reset() async throws {
store.removeAll()
}
}