Actor isolation
Cache is an actor. Reads, writes, and eviction happen inside its isolation domain.
Swift 6 cache library
SwiftStash is an actor-isolated cache with explicit FIFO, LIFO, and LRU eviction. Use a no-op memory engine for process-local values or persist entries to disk with a serializer you control.
No background timer. You decide when eviction runs.
Add the package to your Package.swift, then add "SwiftStash" to the target dependencies that use it.
dependencies: [
.package(
url: "https://github.com/modern-swift-dev/swift-stash.git",
from: "1.1.0"
)
]A small first cache
Calls that cross the cache actor use await. Cached values need to conform to Sendable.
import SwiftStash
let storage = MemoryStorageEngine<String, String>()
let cache = await Cache(
policy: .lru(threshold: 5 * 60),
storagePolicy: storage
)
await cache.add("Ada", for: "current-user")
let name = await cache["current-user"]What it does
Cache is an actor. Reads, writes, and eviction happen inside its isolation domain.
Set a FIFO, LIFO, or LRU policy, then call evictExpired() or evictUntil(maxNbItems:).
Use memory when persistence does not matter. Use disk with JSON, strings, or a custom serializer when it does.
Cache state
Before
concurrencyA tour of Swift concurrencyactorsProtecting state with actors2 entries
After evictUntil(maxNbItems: 1)
concurrencyA tour of Swift concurrency1 entry
Platforms and source
SwiftStash is MIT-licensed. Its source, issues, releases, and package metadata are public in the modern-swift-dev GitHub organization.
Start with the guide, then choose a storage engine and eviction policy.
Read documentationBrowse generated Swift API documentation for the SwiftStash module.
Open API referenceInspect the package and its license, report an issue, or review published releases.
Open repository