JakeWharton / DiskLruCache

Java implementation of a Disk-based LRU cache which specifically targets Android compatibility.
http://jakewharton.github.io/DiskLruCache
Apache License 2.0
5.79k stars 1.18k forks source link

Avoid throwing an exception trying to delete that which does not exist. #43

Closed gubatron closed 11 years ago

gubatron commented 11 years ago

For some unknown reason, when you have a hundreds of thousands of clients a day using the DiskLruCache API on Android when some of them make use of trimToSize() (when tryng to flush(), or delete() the cache) they end up having the exception thrown by remove() because it tries to delete a file which does not exist.

I'm not sure what the cause is, but It's as if something is removing the file for that key and the cache doesn't have a chance to know about it, maybe it's a synchronization issue, maybe the file gets deleted by an external entity at the wrong time (and the phone is turned off, or the process is force closed at the wrong moment) and the cache thinks it still has a key that needs to be pruned. The result is that the cache can't never finish doing trimToSize() (which is called when trying to flush() or delete()), so you can't never even delete the cache unless you do it by force not using this API wiping out the entire directory and re-creating it.

When this happens, the whole cache becomes unusable, we've found instances where the cache files are months old thanks to this issue.