Skip to content
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

Feature request: cache control #1

Open
jnutting opened this issue Jan 13, 2023 · 0 comments
Open

Feature request: cache control #1

jnutting opened this issue Jan 13, 2023 · 0 comments

Comments

@jnutting
Copy link

Sometimes, we have some visual elements in an app that we want to fetch once and never have to worry about them being flushed from a cache when it hits a size limit, but at the same time we have other things we might fetch that we are OK with having them dropped from a cache if it hits a limit. If the user of this could somehow specify which cache to use (instead of it always being URLCache.default), that would be helpful.

For example, let's say that some of the main controls in an app, like button images for example, are fetched from a backend, and once they're all fetched, we want to be sure they will stay. In that case, we could pass in a specific URLCache that we know has both enough memory size and storage size to keep everything, even saving it all to storage so that it's available the next time the app runs. Meanwhile, we might have other things that we're fetching from a backend, say for some kind of photo browser, that we want to keep some of them in an in-memory cache to keep the app responsive, but we don't want to cache them to local storage at all, because we know that the next time the app runs, the set of images will be entirely different. So for that, we could prepare a cache that has a large enough in-memory size, but no disk storage at all.

For example:

/* If I had these defined somewhere: */

// A small cache with storage, just large enough for all our GUI controls:
let guiCache = URLCache(memoryCapacity: 2 * 1024 * 1024, diskCapacity: 2 * 1024 * 1024)
// A large in-memory cache for browsing remote photos, none of which will be saved locally:
let photoCache = URLCache(memoryCapacity: 128 * 1024 * 1024, diskCapacity: 0)

/* Then I'd like to be able to pass use them with your class like this: */

 NSAsyncCachedImage("https://example.com/plus-icon.png", cache: guiCache) { image in
    // This button image will stay around, and probably never need to be fetched again
    image
}

 NSAsyncCachedImage(pathToSomeRemotePhoto, cache: photoCache) { image in
    // This image will display, and be available for some time thanks to the large in-memory cache, but will not be stored
    image
}

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

1 participant