Hi, folks.
I need to cache images with dynamic url. I have 2 methods .getImageId() and .getImageUrl();
I need to cache my downloaded images with getImageId() key, because this id is unique and image url change all time.
How to set custom image id to cache? Please show me a way. Thanks.
+1
+1
In Glide 3 you can create your own ModelLoader and DataFetcher. DataFetcher's getId() method is used as the cache key, so you can implement it however you wish. Using urls, the simplest way to do this is to write a simple wrapper for the existing ModelLoader
Alternative you can copy/paste and customize Glide's existing ModelLoader for GlideUrls and override the getId() method the same way.
In Glide 4.0, ModelLoader's provide a Key to use as the primary identifier in the cache keys. In Glide 4 you can just create a custom ModelLoader and return a LoadData with your custom cache key and the rest of the fields filled out by a delegate ModelLoader.
@LPFinch @diego04
How about like this? Override the "getCacheKey()" Method
Usage:
Glide.with(...).load(new GlideUrlWithToken("http://....", token)).into(imgView);
public class GlideUrlWithToken extends GlideUrl{
private String mSourceUrl;
public GlideUrlWithToken(String url, String token) {
super(new StringBuilder(url)
.append(url.contains("?") ? "&token=" : "?token=") //already has other parameters
.append(token) // append the token at the end of url
.toString());
Preconditions.checkNotNull(url);
Preconditions.checkNotEmpty(url);
Preconditions.checkNotNull(token);
Preconditions.checkNotEmpty(token);
mSourceUrl = url;
}
@Override
public String getCacheKey() {
return mSourceUrl;
}
@Override
public String toString() {
return super.getCacheKey();
}
}
The above code is a solid solution, but see #501 also for more discussion.
Most helpful comment
@LPFinch @diego04
How about like this? Override the "getCacheKey()" Method
Usage: