TinyCache is a Swift package for saving and retrieving Codable objects from an in-memory cache with expirations.
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
.
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)
link |
Stars: 1 |
Last commit: 2 weeks ago |
🎉
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics