Skip to content

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

Closed
LPFinch opened this issue Sep 2, 2015 · 5 comments
Closed

Caching images with dynamic url #607

LPFinch opened this issue Sep 2, 2015 · 5 comments
Labels

Comments

@LPFinch
Copy link

LPFinch commented Sep 2, 2015

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.

@sjudd sjudd added the question label Sep 2, 2015
@kevindeasis
Copy link

+1

1 similar comment
@Shusshu
Copy link

Shusshu commented Sep 15, 2015

+1

@sjudd
Copy link
Collaborator

sjudd commented Sep 15, 2015

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.

@Guang1234567
Copy link

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

@TWiStErRob
Copy link
Collaborator

The above code is a solid solution, but see #501 also for more discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants