DiskLruCache icon indicating copy to clipboard operation
DiskLruCache copied to clipboard

Multi-thread: Infinite loop in trimToSize

Open la-urre opened this issue 9 years ago • 5 comments

Hello guys,

I use DiskLruCache in a multi-thread environment. I get an infinite loop in the trimToSize method:

while (size > maxSize) { Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next(); remove(toEvict.getKey()); }

The remove call fails to remove the entry, as an other thread is editing the toEvict entry. Shouldn't we check if the remove worked, and for example silently return if it did not ?

la-urre avatar May 15 '15 11:05 la-urre

Yup, it should definitely skip entries being edited.

swankjesse avatar May 16 '15 01:05 swankjesse

Tried a fix, let me know what you think about it.

la-urre avatar May 18 '15 10:05 la-urre

It's close to what we want. I think it's going to run faster as a single loop that runs until the size is sufficiently small, or there's nothing left to remove. (Since we only allocate one iterator). Even better if you can fast-fail at the front and avoid allocating the iterator at all if the cache is already sufficiently small.

swankjesse avatar May 18 '15 13:05 swankjesse

Send a pull request?

swankjesse avatar May 18 '15 13:05 swankjesse

And even better, send the pull request against OkHttp? Its DiskLruCache is developed further than this one. Someday we should just simplify this project into a repackaging of a subset of that code.

swankjesse avatar May 18 '15 13:05 swankjesse