Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Lost cache in some condition #16

Open
teoking opened this issue Sep 18, 2012 · 7 comments
Open

Lost cache in some condition #16

teoking opened this issue Sep 18, 2012 · 7 comments

Comments

@teoking
Copy link

teoking commented Sep 18, 2012

I found an issue: you created a new cache and just then the app is crashed, the cache will be lost.

I check the journal file and found that: the new cache you just created is marked with "DIRTY", and even if I called commit, there's no "CLEAN" followed that "DIRTY", at this time, if the app crashes, the cache leaves "DIRTY" but not "CLEAN" which it should be. Then you launch the app again, the "DIRTY" cache with no "CLEAN" followed will be deleted, and you lost the cache.

This scenario cannot covered by android test cases.

I found this may be caused by the journal is opened by BufferedWriter which has a 8K buffer internally. So the latest "CLEAN" state is actually in the buffer but not in disk when the crash happens.

Add the following line in completeEdit method seems can solve this:
journalWriter.flush();

@swankjesse
Copy link
Collaborator

This is a policy decision in the API. You can manually call flush() in your own code if you'd like to flush the journal after every write. DiskLruCache doesn't do this by default because it wants to minimize the number of slow disk writes it makes.

@swankjesse
Copy link
Collaborator

That said, this does need to be documented!

@teoking
Copy link
Author

teoking commented Sep 19, 2012

I see your point. I'd like not flush journal after every write, only after write "CLEAN" or "DIRTY", while not flush after write "READ"(not worth to do).
Back to my case, my app is an android app which may be closed or interrupted unpredictably, and I has only one chance to create a cache(after a video is downloaded) and use it elsewhere. If someone has the similar case with me, I suggest flush after every commit immediately.

@lexer
Copy link

lexer commented Feb 4, 2013

@swankjesse could you suggest where it is better to flush DiskLruCache for Android apps to prevent loosing of image cache after app close or crash?

@swankjesse
Copy link
Collaborator

Calling flush after committing an edit would work.

@lexer
Copy link

lexer commented Feb 4, 2013

Thanks!

@kroikie
Copy link

kroikie commented Aug 20, 2013

Thank you so much for this, flush works!

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

No branches or pull requests

4 participants