Swiftpack.co - codykerns/TinyCache as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by codykerns.
codykerns/TinyCache 1.0
A Swift package for saving and retrieving Codable objects from an in-memory cache with expirations.
⭐️ 1
🕓 2 weeks ago
.package(url: "https://github.com/codykerns/TinyCache.git", from: "1.0")

Swift

TinyCache

Description

TinyCache is a Swift package for saving and retrieving Codable objects from an in-memory cache with expirations.

Requirements

  • Swift 5.4

Usage

To save an object to the cache:

let key = "key"
let value = "value"

TinyCache.cache(key, value: value)

To retrieve it from the cache, pass the type of object and key:

if let value = TinyCache.value(String.self, "key") {
    // value is a String
}

If the type doesn't match what's in the cache, the returned value will be nil.

Expirations

By default, TinyCache caches a value using CacheDuration.medium (30 minutes). To customize the duration of each cached value until expiration, pass a duration:

let key = "key"
let value = "value"

TinyCache.cache(key, value: value, duration: .short) // 10 minutes

For a specific number of minutes:

let key = "key"
let value = "value"

TinyCache.cache(key, value: value, duration: .minutes(12)) // 12 minutes

For a specific expiration date:

let key = "key"
let value = "value"
let expiration = Date()

TinyCache.cache(key, value: value, expiration: expiration)

License

MIT License.

GitHub

link
Stars: 1
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.0
2 weeks ago

🎉

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics