Glide: Caching images with dynamic url

Created on 2 Sep 2015  路  5Comments  路  Source: bumptech/glide

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.

question

Most helpful comment

@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();
    }
}

All 5 comments

+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 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.

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tryking picture Tryking  路  3Comments

mttmllns picture mttmllns  路  3Comments

kooeasy picture kooeasy  路  3Comments

r4m1n picture r4m1n  路  3Comments

ghost picture ghost  路  3Comments