Users can change a picture (replace it). Once the user changes their image, I want the new image to get cached in Glide and the old image to get thrown out of the cache.
I've read everything online but I'm still at a loss on how to implement a good solution to this.
I've tried skipping the local memory and disk caches like when setting the image:
GlideApp.with(fragment)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(view);
This solution is SLOW because it now calls the new image every single time - it never caches the new image.
Glide documentation says:
the best way to invalidate a cache file is to change your identifier when the content changes (url, uri, file path etc) when possible. - https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
But that is not possible for me, so Glide documentation then says:
Since it’s often difficult or impossible to change identifiers, Glide also offers the signature() API to mix in additional data that you control into your cache key.
And it gives this example:
Glide.with(yourFragment)
.load(yourFileDataModel)
.signature(new ObjectKey(yourVersionMetadata))
.into(yourImageView);
But here comes the issue. What would be a good "yourVersionMetadata"? How do I create and maintain it? I've seen examples like so:
.signature(new ObjectKey(Long.toString(System.currentTimeMillis())))
This causes the disk cache key to change every time I load the image, so it is SLOW. I just need it to change when the user replaces the image. Not every time the image loads.
Someone wrote:
You can do something like generate a new UUID or increment an integer whenever the image changes. If you go that route, you'll have to keep track of the current signature for each image somewhere. - https://github.com/bumptech/glide/issues/2841
I don't understand how to do this.
I also tried the Async task for completely deleting cache. It works, but again it's super slow (and Glide doesn't recommend using this approach).
I don't know how I can just insert the current signature (which should be faster) rather than creating a new signature every time the image loads. Help? It seems to replace an image and recaching it shouldn't be so difficult!