KituraCache is an in-memory, thread-safe cache which allows you to store objects against a unique, Hashable key.
KituraCache requires Swift 6.0 or newer. Install Swift from Swift.org. Compatibility with older Swift versions is not guaranteed.
CI validates Linux and macOS with Swift Testing, builds the Apple platforms declared in Package.swift, builds API documentation with Swift-DocC, and compile-checks Android and Wasm with the official Swift SDK bundles.
Add the Kitura-Cache package to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest Kitura-Cache release.
.package(url: "https://github.com/Kitura/Kitura-Cache.git", from: "x.x.x")Add KituraCache to your target's dependencies:
.target(
name: "Example",
dependencies: [
.product(name: "KituraCache", package: "Kitura-Cache")
]
)import KituraCacheTo use KituraCache, add the dependencies and import the package as defined above, then initialize:
let cache = KituraCache()If no arguments are provided, the default cache will be non-expiring. On platforms that provide Dispatch, a check will be made every minute to determine whether any entries need to be removed. Expired entries are also ignored when fetched and cleaned up when keys are read.
To add an entry to the cache, or update an entry if the key already exists:
In the following examples, item is a struct with an integer id field.
cache.setObject(item, forKey: item.id)To retrieve an entry from the cache:
let cache = KituraCache()
...
if let item = cache.object(forKey: 1) {
//Object with key of 1 retrieved from cache.
...
}
else {
//No object stored in cache with key of 1.
...
}To delete a single entry, pass the entry's key as a parameter:
cache.removeObject(forKey: 1)To reset the cache and its Statistics:
cache.flush()KituraCache uses Swift-DocC for API documentation. Generate the documentation locally with:
swift package --allow-writing-to-directory .build generate-documentation --target KituraCache --output-path .build/KituraCache.doccarchive --warnings-as-errorsPreview it in a browser with:
swift package --disable-sandbox preview-documentation --target KituraCacheUse Kitura GitHub Discussions for project-wide coordination and KituraCache issues for package-specific bugs or feature requests.
This library is licensed under Apache 2.0. Full license text is available in LICENSE.