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

Cache function #95

Closed
stephan-fischer opened this issue May 30, 2024 · 3 comments
Closed

Cache function #95

stephan-fischer opened this issue May 30, 2024 · 3 comments

Comments

@stephan-fischer
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm always frustrated when I need to repeatedly download photos from a URL because there is no caching mechanism in place. This results in increased data usage and slower performance when accessing the same images multiple times.

Describe the solution you'd like
I would like to have a caching mechanism within the Capacitor Media plugin that allows photos downloaded from a URL to be temporarily stored. This would enable faster access to these images when they are needed again, reducing data usage and improving performance.

For instance, using the SDWebImageDownloader and SDImageCache in iOS, caching and retrieving an image can be done seamlessly. The code snippet below demonstrates how this can be achieved:

import SDWebImage

func downloadAndCacheImage(from imageUrl: URL, completion: @escaping (UIImage?, Error?) -> Void) {
    let downloader = SDWebImageDownloader.shared
    let cache = SDImageCache.shared
    
    // Check if the image is already in cache
    let cacheKey = imageUrl.absoluteString
    if let cachedImage = cache.imageFromCache(forKey: cacheKey) {
        completion(cachedImage, nil)
        return
    }
    
    // If not, download the image
    downloader.downloadImage(with: imageUrl, options: [], progress: nil) { (image, data, error, finished) in
        guard let imageData = data, let downloadedImage = image, finished else {
            completion(nil, error)
            return
        }
        
        // Cache the downloaded image
        cache.store(downloadedImage, imageData: imageData, forKey: cacheKey, toDisk: true, completion: nil)
        
        completion(downloadedImage, nil)
    }
}

Describe alternatives you've considered
I have considered implementing a custom caching solution, but it would be more efficient and convenient if this feature were integrated directly into the Capacitor Media plugin. Another alternative is using third-party caching libraries, but this adds complexity and dependencies to the project.

Additional context
Adding a caching mechanism would greatly enhance the functionality of the Capacitor Media plugin. It would be beneficial for applications that frequently download and display images from the internet, providing a smoother user experience.

Thank you for considering this feature request!

@nkalupahana
Copy link
Collaborator

@stephan-fischer Hi! I'm not entirely sure I understand the use case. Under what circumstances would you want to add the exact same image to a user's photo library, over and over?

@stephan-fischer
Copy link
Contributor Author

If the images are hosted on a CDN, the download costs traffic. There are certainly users who save the images several times, be it for tests, penetration, etc.

@nkalupahana
Copy link
Collaborator

I'm sorry, but I'm still not seeing much of a use case for this feature. Users who are "testing" the app are probably just app developers, and adding a completely new feature to save developers a few seconds and a fraction of a fraction of a cent is probably not worth it :) Additionally, anyone who is trying to DoS/DDoS your CDN won't be doing it by clicking a button over and over in your app. They'll have some automated system to do it outside of the app, circumventing the caching (in the extremely unlikely case that someone specifically decides to target your app and cost you money -- in which case you have a much different set of problems haha). Adding this feature would also add a lot of unnecessary complexity. Some people might not want caching (e.g. if the image at the URL constantly changes), and then I'd need to add more information about how the caching works, and how to turn it off, and I'm just not seeing the benefits for something that has a very limited use case.

If you want caching, I recommend adding it to your CDN, instead of trying to implement it app-side, where it can easily be circumvented. (This is the industry standard approach.) Cloudflare is great for this -- putting Cloudflare in front of your CDN (completely free btw) will make subsequent requests free, and will automatically protect you from DoS/DDoS attacks. (https://developers.cloudflare.com/cache/concepts/default-cache-behavior/ -- when you set up Free Cloudflare for your site, images are automatically cached, so when images are repeatedly requested, subsequent requests won't cost you anything.)

@nkalupahana nkalupahana closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2024
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