Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
emerana opened this issue Dec 16, 2020 · 1 comment

Comments

@emerana
Copy link

emerana commented Dec 16, 2020

No description provided.

@tomatrow
Copy link

tomatrow commented Jan 6, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants