Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disk cache not working everytime the app is restarted #4429

Closed
herbeth1u opened this issue Dec 1, 2020 · 5 comments
Closed

Disk cache not working everytime the app is restarted #4429

herbeth1u opened this issue Dec 1, 2020 · 5 comments

Comments

@herbeth1u
Copy link

herbeth1u commented Dec 1, 2020

Glide Version:
4.12.0-SNAPSHOT
4.11.0
4.8.0

Integration libraries: None

Device/Android Version:
OnePlus 6, Android 10 (OxygenOS 10.3.7)
Emulator, API 27

Issue details / Repro steps / Use case background:

  1. Start the app, let the image load from REMOTE.
  2. Kill the app.
  3. Restart the app.
  4. The image still loads from REMOTE.

Note: if I just navigate through the app and go back to the image, I get successful cache hits from either MEMORY_CACHE or DATA_DISK_CACHE (which guarantees me the cache exists and works). The issue only occurs when restarting the app.

One of the most notable consequences of this is I cannot use Glide to make an offline mode if the user temporarily loses its connection. Also it makes Glide use more bandwidth than it should if I believe the docs : https://bumptech.github.io/glide/doc/caching.html

Glide load line / GlideModule (if any) / list Adapter code (if any):

Glide.with(fragment).load("https://assets.stickpng.com/images/580b57fcd9996e24bc43c51f.png").into(imageView)

No GlideModule (although I still have the problem if I add one).

Layout XML:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/picture"
            android:layout_width="300dp"
            android:layout_height="70dp"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Stack trace / LogCat: No error message.

D/Glide: Finished loading BitmapDrawable from REMOTE for https://assets.stickpng.com/images/580b57fcd9996e24bc43c51f.png with size [1050x245] in 456.638897 ms

After restarting the app, I get the exact same log line (still REMOTE, same URL, same target size, same call).

Am I missing something here, or is this the expected behavior? After the app is killed, it looks like the whole cache is flushed: if I add onlyRetrieveFromCache(true) to the above call just before restarting, Glide cannot find the image (I get an exception in the logs). Or maybe there is a parameter to persist the disk cache?

Thanks.

@ahmadbajwa12315
Copy link

You can store image to cache once its loaded.
try to add this line in your glide code:
.apply(RequestOptions.skipMemoryCacheOf(false)).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))

@herbeth1u
Copy link
Author

Hey, thanks for your answer.
I tried your suggestion but it doesn't help (as expected from DiskCacheStrategy.NONE: the disk cache remains empty). That being said, setting DiskCacheStrategy.ALL doesn't help either.
Also, according to the docs, I guess Glide should cache at least one version of an image on the disk by default (without having to specify anything).

Using Android Studio's Device File Explorer, I was able to see that the default cache directory ( /data/user/0/<my_app>/cache/image_manager_disk_cache ) worked and was filled with the proper cache keys. When the app is killed, the directory is still ok. But as soon as I restart the app and Glide is reinitialized, I can see this directory is flushed.

It looks like Glide flushes this directory everytime it starts, but I haven't pinpointed what part of the code is responsible for this yet.

@sjudd
Copy link
Collaborator

sjudd commented Dec 2, 2020

Glide doesn't do anything to clear out the disk cache automatically. But there is a clearDiskCache method:

.

Maybe put a break point there and see what happens?

It's also possible something else in the app or in a library you use is going through and deleting things out of the cache. The system also has the ability to do it as well, either automatically or via clear cache in the application options.

@ahmadbajwa12315
Copy link

You can store image to cache once its loaded.
try to add this line in your glide code:
.apply(RequestOptions.skipMemoryCacheOf(false)).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))

try out this for cache
.apply(RequestOptions.skipMemoryCacheOf(true)).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))

@herbeth1u
Copy link
Author

herbeth1u commented Dec 3, 2020

It's also possible something else in the app or in a library you use is going through and deleting things out of the cache.

Thanks for the hint, I've found the culprit. As a matter of fact, I was using Exoplayer at the same time with SimpleCache(context.cacheDir) as its cache. Though SimpleCache's constructor clearly states:

  • Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
  • the directory cannot be used to store other files.

Adding a subfolder to this cache like so: SimpleCache(File(context.cacheDir, "cache")) fixed the problem, and I once again get successful DATA_DISK_CACHE hits.

The issue is solved, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants