Cache icon indicating copy to clipboard operation
Cache copied to clipboard

Can you provide an example of how to use it? I don't seem to know how to use it.

Open emerana opened this issue 5 years ago • 1 comments

emerana avatar Dec 16 '20 07:12 emerana

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.

tomatrow avatar Jan 06 '21 05:01 tomatrow