Glide自定义缓存key

2,440 阅读1分钟

注:基于Glide v3.7.0版本

最近后台做了文档安全,返回来的图片链接每次都是变化的,Glide默认的CacheKey(图片链接)就不生效了,但图片id是不变的,于是打算用id作为CacheKey。

代码修改如下:

新建一个类继承GlideUrl

public class MyGlideUrlWithId extends GlideUrl {    
    
    private String id;    
    
    public MyGlideUrlWithId(String url, String id) {        
        super(url);        
        this.id = id;    
    }    

    @Override    
    public String getCacheKey() {        
        return !TextUtils.isEmpty(id) ? id : super.getCacheKey();    
    }

}

使用时:

Glide.with(mContext).load(new MyGlideUrlWithId(url, id)).into(imageView);

参考

blog.csdn.net/guolin_blog…