-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Caching images with dynamic url #607
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
Comments
+1 |
1 similar comment
+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<GlideUrl, InputStream> that delegates all methods but getId(). 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. |
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. |
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.
The text was updated successfully, but these errors were encountered: