Glide : How to find if the image is already cached and use the cached version?

末鹿安然 提交于 2019-12-02 20:16:02
  1. You don't need a custom ModelLoader to show the GIF from cache if present and fetch it otherwise, that's actually Glide's default behavior. Just using a standard load line should work fine:

    Glide.with(TheActivity.this)
       .load("http://sampleurl.com/sample.gif")
       .diskCacheStrategy(DiskCacheStrategy.SOURCE)
       .into(theImageView);
    

Your code will prevent Glide from downloading the GIF and will only show the GIF if it is already cached, which it sounds like you don't want.

  1. Yes, the old image will eventually be removed. By default Glide uses an LRU cache, so when the cache is full, the least recently used image will be removed. You can easily customize the size of the cache to help this along if you want. See the Configuration wiki page for how to change the cache size.

  2. Unfortunately there isn't any way to influence the contents of the cache directly. You cannot either remove an item explicitly, or force one to be kept. In practice with an appropriate disk cache size you usually don't need to worry about doing either. If you display your image often enough, it won't be evicted. If you try to cache additional items and run out of space in the cache, older items will be evicted automatically to make space.

 Glide.with(context)
 .load("http://sampleurl.com/sample.gif")
 .skipMemoryCache(true)
 .into(imageView);

You already noticed that we called .skipMemoryCache(true) to specifically tell Glide to skip the memory cache. This means that Glide will not put the image in the memory cache. It's important to understand, that this only affects the memory cache! Glide will still utilize the disk cache to avoid another network request for the next request to the same image URL.for more read this Glide Cache & request optimization.

Happy coding!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!