Swift 6 cache library

Cache Swift values inside an actor.

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.

Install 1.1.0

Add the package to your Package.swift, then add "SwiftStash" to the target dependencies that use it.

Package.swift
dependencies: [
    .package(
        url: "https://github.com/modern-swift-dev/swift-stash.git",
        from: "1.1.0"
    )
]

A small first cache

Store and read a value through the actor.

Calls that cross the cache actor use await. Cached values need to conform to Sendable.

Swift
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

Choose storage and control eviction.

Actor isolation

Cache is an actor. Reads, writes, and eviction happen inside its isolation domain.

Explicit eviction

Set a FIFO, LIFO, or LRU policy, then call evictExpired() or evictUntil(maxNbItems:).

Storage you choose

Use memory when persistence does not matter. Use disk with JSON, strings, or a custom serializer when it does.

Cache state

An explicit eviction changes the cache

Before

concurrencyA tour of Swift concurrency
actorsProtecting state with actors

2 entries

After evictUntil(maxNbItems: 1)

concurrencyA tour of Swift concurrency

1 entry

Platforms and source

Built as a Swift package.

SwiftStash is MIT-licensed. Its source, issues, releases, and package metadata are public in the modern-swift-dev GitHub organization.

  • macOS 15+
  • iOS 17+
  • tvOS 17+
  • watchOS 10+
  • visionOS 1+
  • Linux with Swift 6+