Glide Version: 3.7.0
Integration libraries: Volley
Device/Android Version: 4.4.2
Issue details / Repro steps / Use case background: How long the glide disc cache is valid for. I want to load an image from a URL and save it to local disc so that it I don't need to use network request next time. Should I manually save that image into disc, or it is enough to use disc cache (e.g. if disc cache validity is unlimited time)?
Glide load line / GlideModule (if any) / list Adapter code (if any):
Glide.with(this).load("http://examle.com/image.jpg").into( mImageView); // in a fragment
Time-wise it's unlimited, but the disk cache is constrained by size. If it gets full the oldest item will be evicted; more precisely LRU algorithm is used.
This means that as long as the user is actively seeing the image, it'll stay in cache. If the user hasn't seen the image for a while, but has seen a lot of other ones, the first image may have to be re-downloaded.
For more info see DiskCache interface and its implementations.
Thanks a lot. Perfectly clear :).
Tip: for pinning functionality don't use the cache, but download it separately and load a local File you manage with .diskCacheStrategy(NONE) (or RESULT if the image is huge).
Thanks a lot. I'll do like this. Please correct me if I am doing it inefficiently: I am saving the bitmap to local file using SimpleTarget (resized to 200px x 150px) and then load image using
Glide
.with( this ) // in a Fragment
.load( new File("path\\to\\file.jpg") )
.skipMemoryCache( true )
.diskCacheStrategy( DiskCacheStrategy.NONE )
.into( mImageView );
Disabling caching is inefficient most of the time. And you may be saving that Bitmap on the UI thread.
Your use case should be as simple as:
Glide
.with(this)
.load(url) // no need for manually caching the local File, Glide'll save your url into a File behind the scenes
.override(200, 150) // sounds like you want full control over size for some reason
//.diskCacheStrategy(RESULT) // default, no need to specify
//.skipMemoryCache(true) // only useful if you're loading a single (large) image, which looks like you are
.into(mImageView)
;
When deciding between what I just wrote and your final solution above, consider if it's really a big deal if the user needs to redownload that image when that image hasn't been seen for so long that it got evicted. You didn't say much about the use case so my code may be irrelevant.
Thanks a lot. I actually download and store all the images locally when the image is loaded from online for the first time. The reason is, I provide the option to load the image from online library, or from Phone Gallery. I also want them to be able copy all the saved images whenever they want.
So, if I understand it correctly, caching is not necessary for my case as I am sort of (disk) caching myself.
Yes, that sounds right. I still recommend allowing memory cache.