Skip to content

Commit 7e4d002

Browse files
committed
Merge pull request nostra13#316 from ncoolz/master
Handle deletion of files from disc cache, keep lastUsageDates actual.
2 parents 9a8a128 + dc6fb95 commit 7e4d002

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

library/src/com/nostra13/universalimageloader/cache/disc/LimitedDiscCache.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
*/
3838
public abstract class LimitedDiscCache extends BaseDiscCache {
3939

40+
private static final int INVALID_READ_SIZE = -1;
41+
4042
private final AtomicInteger cacheSize;
4143

4244
private final int sizeLimit;
@@ -88,9 +90,10 @@ public void run() {
8890
public void put(String key, File file) {
8991
int valueSize = getSize(file);
9092
int curCacheSize = cacheSize.get();
93+
9194
while (curCacheSize + valueSize > sizeLimit) {
9295
int freedSize = removeNext();
93-
if (freedSize == 0) break; // cache is empty (have nothing to delete)
96+
if (freedSize == INVALID_READ_SIZE ) break; // cache is empty (have nothing to delete)
9497
curCacheSize = cacheSize.addAndGet(-freedSize);
9598
}
9699
cacheSize.addAndGet(valueSize);
@@ -121,9 +124,8 @@ public void clear() {
121124
/** Remove next file and returns it's size */
122125
private int removeNext() {
123126
if (lastUsageDates.isEmpty()) {
124-
return 0;
127+
return INVALID_READ_SIZE;
125128
}
126-
127129
Long oldestUsage = null;
128130
File mostLongUsedFile = null;
129131
Set<Entry<File, Long>> entries = lastUsageDates.entrySet();
@@ -143,11 +145,17 @@ private int removeNext() {
143145
}
144146

145147
int fileSize = getSize(mostLongUsedFile);
146-
if (mostLongUsedFile.delete()) {
148+
149+
if (!mostLongUsedFile.exists()) {
150+
lastUsageDates.remove(mostLongUsedFile);
151+
return 0;
152+
}
153+
154+
if ( mostLongUsedFile.delete()) {
147155
lastUsageDates.remove(mostLongUsedFile);
148156
}
149157
return fileSize;
150158
}
151159

152160
protected abstract int getSize(File file);
153-
}
161+
}

0 commit comments

Comments
 (0)