2

I have an image identify by specific URL, to get this image the request URL need timestamp and signature params (signature base on other params). The URL will be change every time client request because it depends on timestamp and signature. So when using Glide to load image as below:

Glide.with(mContext).load(url).into(imageView);

The problem is, with the same image but it loads many times and cache over and over (because of different url)

I want to build a cache wrapper to override equals method of Cache Key but I don't know how to start with Glide

I'm very grateful, if anyone could help

Thanks

4

1 回答 1

2

最后我定制了 GlideUrl 来满足我的要求

public class GlideUrlKey extends GlideUrl {
    public GlideUrlKey(URL url) { super(url); }
    public GlideUrlKey(String url) { super(url); }
    public GlideUrlKey(URL url, Headers headers) { super(url, headers); }
    public GlideUrlKey(String url, Headers headers) { super(url, headers); }

    @Override public String getCacheKey() {
        String url = toStringUrl();
        if (url.contains("fl_id") && url.contains("fl_rel")) {
            String tempUrl = url.substring(0, url.lastIndexOf("&data%5Bsign%5D"));
            if (tempUrl.length() == 0) {
                tempUrl = url.substring(0, url.lastIndexOf("data[sign]") - 1);
                if (tempUrl.length() == 0) {
                    tempUrl = url.substring(0, url.lastIndexOf("fl_rel"));
                }
            }
            return tempUrl;
        } else {
            return url;
        }
    }
}
于 2015-08-19T08:08:57.310 回答