服务器图片改变Glide缓存无法更新

问题

使用glide加载图片,glide有缓存,分为内存缓存和磁盘缓存,可以通过diskCacheStrategy设置不同的缓存策略。具体可以看官方文档(我使用的是glideV4 国内的介绍还比较少)。但是对于指定的url的图片,会出现服务器端的图片已经改变,但是本地加载的时候,glide发现有缓存,不会重新从服务器加载,导致一直显示老的图片。

解决

  1. 如果将diskCacheStrategy设置为NONE,内存缓存依然存在。可以设置 skipMemoryCache(true),这样每次加载都会从服务器重新加载。但是这样耗费流量,加重服务器负担。不好。

  2. 翻阅官方文档发现glide有个signature,就是为了解决这个问题。因为glide缓存是采<K, V>键值对存储,如果加载一个url的图片,K就是url,url不变,那么缓存V也不会变。signature的作用就是可以在K上附加一写Key,也就是我们可以在加载图片的时候,存储一个表明当前版本的时间戳,当更新时,改变时间戳,时间戳改变也就是K改变,那么就会重新加载图片。

    具体实现如下:

    加载图片,根据sp时间戳添加signature

      GlideApp.with(view.getContext())
                    .load(AppConstants.URL_USER_HEAD + uid)
                    .signature(new ObjectKey(SPUtils.getInstance(AppConstants.SP_NAME_USER).getString("head_signature", "")))
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(R.drawable.user_default_head)
                    .error(R.drawable.user_default_head)
                    .into(view);
    
          
          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    更新图片时,同时更新sp的时间戳:

    SPUtils.getInstance(AppConstants.SP_NAME_USER)
            .put("head_signature",String.valueOf(System.currentTimeMillis()));
    
          
          
    • 1
    • 2
    • 3

    这里有个不知道是不是问题的问题,每次加载都要读取sp,可能会有影响。

  3. 注意官方文档有一句话

    Urls - Although the best way to invalidate urls is to make sure the server changes the url and updates the client when the content at the url changes, you can also use ObjectKey to mix in arbitrary metadata (such as a version number) instead.

    也就是说最好的方式就是让服务器加一个时间戳,可是这样实现起来可能就不是很简单。暂时没有尝试。尝试了再来补充。先记下了。

更可行的方法:

每次更换图片的时候,需要将图片名称改变,如logo.png改成applogo.png。那这样url就随之改变了(前提是后台返回的url)
如:http://127.0.0.1:8061/Content/Images/applogo.png,这样Glide就能重新加载服务器上的图片了

问题

使用glide加载图片,glide有缓存,分为内存缓存和磁盘缓存,可以通过diskCacheStrategy设置不同的缓存策略。具体可以看官方文档(我使用的是glideV4 国内的介绍还比较少)。但是对于指定的url的图片,会出现服务器端的图片已经改变,但是本地加载的时候,glide发现有缓存,不会重新从服务器加载,导致一直显示老的图片。

解决

  1. 如果将diskCacheStrategy设置为NONE,内存缓存依然存在。可以设置 skipMemoryCache(true),这样每次加载都会从服务器重新加载。但是这样耗费流量,加重服务器负担。不好。

  2. 翻阅官方文档发现glide有个signature,就是为了解决这个问题。因为glide缓存是采<K, V>键值对存储,如果加载一个url的图片,K就是url,url不变,那么缓存V也不会变。signature的作用就是可以在K上附加一写Key,也就是我们可以在加载图片的时候,存储一个表明当前版本的时间戳,当更新时,改变时间戳,时间戳改变也就是K改变,那么就会重新加载图片。

    具体实现如下:

    加载图片,根据sp时间戳添加signature

      GlideApp.with(view.getContext())
                    .load(AppConstants.URL_USER_HEAD + uid)
                    .signature(new ObjectKey(SPUtils.getInstance(AppConstants.SP_NAME_USER).getString("head_signature", "")))
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(R.drawable.user_default_head)
                    .error(R.drawable.user_default_head)
                    .into(view);
    
        
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    更新图片时,同时更新sp的时间戳:

    SPUtils.getInstance(AppConstants.SP_NAME_USER)
            .put("head_signature",String.valueOf(System.currentTimeMillis()));
    
        
        
    • 1
    • 2
    • 3

    这里有个不知道是不是问题的问题,每次加载都要读取sp,可能会有影响。

  3. 注意官方文档有一句话

    Urls - Although the best way to invalidate urls is to make sure the server changes the url and updates the client when the content at the url changes, you can also use ObjectKey to mix in arbitrary metadata (such as a version number) instead.

    也就是说最好的方式就是让服务器加一个时间戳,可是这样实现起来可能就不是很简单。暂时没有尝试。尝试了再来补充。先记下了。

猜你喜欢

转载自blog.csdn.net/hizhangyuping/article/details/81098092
今日推荐