CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2016 |
SPMSupports SPM | ✗ |
Maintained by Christopher Luu.
CacheIsKing
is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), etc. Simply put, it’s designed to act like an NSCache
for everything, including Swift variables.
Hashable
NSCache
, the cache is cleared when the app enters the backgroundString
, Int
, and Float
keysitemForKey
uses generics so you don’t have to cast the return value when the type is inferred correctlyNSCache
, the cache can have a countLimit
set to ensure that the cache doesn’t get too largeSimply use the KingCache
class similar to how you’d use a NSCache
. Using the setItem
and itemForKey
methods allow you to use type inference to get the values you want.
let cache = KingCache()
cache.setItem(123, forKey: "123")
if let item: Int = cache.itemForKey(456) {
doSomethingWithItem(item)
}
You can also use subscripts to set/get items from the cache. Unfortunately since Swift doesn’t support subscript methods with generics yet, you’ll have to cast your items as necessary. Also currently only String
, Int
, and Float
keys are supported:
let cache = KingCache()
cache["123"] = 123
if let item = cache[456] as? Int {
doSomethingWithItem(item)
}
The KingCache
also has a countLimit
property, which allows you to set the maximum number of items in the cache. It currently evicts randomly until the countLimit
is met.
let cache = KingCache()
cache.countLimit = 2
cache[123] = 123
cache[234] = 234
cache[345] = 345
print("\(cache.count)") // shows a count of 2