Cache
Cache copied to clipboard
Can you provide an example of how to use it? I don't seem to know how to use it.
So, here's an example:
// main.swift
let imageCache = MemoryCache<NSData>(countLimit: 50, automaticallyRemoveAllObjects: true)
func imageApiCall(key: String, completion: @escaping (NSData) -> ()) {
// some expensive code
}
func fetchImage(key: String, completion: @escaping (NSData) -> ()) {
imageCache.get(key: key) { cachedData in
if let data = cachedData {
completion(data)
} else {
imageApiCall(key: key) { requestedData in
imageCache.set(key: key, value: requestedData)
completion(requestedData)
}
}
}
}
P.S Here's a class that lets you cache a wider range of objects.
Also, the author is pretty great. Check out this iOS related podcast he did a while back.