Requirements
- Swift 6.0 or later
- macOS 15, iOS 17, tvOS 17, watchOS 10, or visionOS 1 or later
Install 1.1.0
Add SwiftStash to the package dependencies in Package.swift. Then add "SwiftStash" to the dependencies of your target.
Getting started
Start with memory storage. Move to disk when an entry needs to survive beyond the cache actor.
Add SwiftStash to the package dependencies in Package.swift. Then add "SwiftStash" to the dependencies of your target.
.package(
url: "https://github.com/modern-swift-dev/swift-stash.git",
from: "1.1.0"
)Cache is an actor. The cache accepts values that conform to Sendable. A string-backed enum gets the required CacheKey behavior automatically.
import SwiftStash
enum ProfileKey: String, CacheKey {
case current
}
let storage = MemoryStorageEngine<ProfileKey, String>()
let cache = await Cache(storagePolicy: storage)
await cache.add("Ada", for: .current)
let currentProfile = await cache[.current]Create the named directory before persistence begins. DiskStorageEngine writes each entry below the app cache directory and does not create the named subdirectory for you.
let storage = DiskStorageEngine(
directory: "profiles",
keyType: ProfileKey.self,
serializer: JsonDiskStorageSerializer<Profile>()
)
let cache = await Cache(storagePolicy: storage)Choose .fifo, .lifo, or .lru when creating the cache. Call evictExpired() to remove old entries, or call evictUntil(maxNbItems:) to first remove expired entries and then enforce a maximum count.
SwiftStash does not run a background timer. Place the eviction call in the workflow that knows when reducing the cache makes sense.
Cache state
Before
concurrencyA tour of Swift concurrencyactorsProtecting state with actors2 entries
After evictUntil(maxNbItems: 1)
concurrencyA tour of Swift concurrency1 entry