Skip to content

Kitura/Kitura-Cache

Repository files navigation

Kitura

CI Swift 6.0+ Apple, Linux, Android, and Wasm Apache 2.0

KituraCache

KituraCache is an in-memory, thread-safe cache which allows you to store objects against a unique, Hashable key.

Requirements

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.

Usage

Add dependencies

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 package

import KituraCache

Example

To 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()

Documentation

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-errors

Preview it in a browser with:

swift package --disable-sandbox preview-documentation --target KituraCache

Community

Use Kitura GitHub Discussions for project-wide coordination and KituraCache issues for package-specific bugs or feature requests.

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

About

Kitura cache

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages