29
29
/**
30
30
* Abstract disc cache limited by some parameter. If cache exceeds specified limit then file with the most oldest last
31
31
* usage date will be deleted.
32
- *
32
+ *
33
33
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
34
34
* @since 1.0.0
35
35
* @see BaseDiscCache
36
36
* @see FileNameGenerator
37
37
*/
38
38
public abstract class LimitedDiscCache extends BaseDiscCache {
39
39
40
- private static final int INVALID_READ_SIZE = -1 ;
40
+ private static final int INVALID_SIZE = -1 ;
41
41
42
42
private final AtomicInteger cacheSize ;
43
43
@@ -93,7 +93,7 @@ public void put(String key, File file) {
93
93
94
94
while (curCacheSize + valueSize > sizeLimit ) {
95
95
int freedSize = removeNext ();
96
- if (freedSize == INVALID_READ_SIZE ) break ; // cache is empty (have nothing to delete)
96
+ if (freedSize == INVALID_SIZE ) break ; // cache is empty (have nothing to delete)
97
97
curCacheSize = cacheSize .addAndGet (-freedSize );
98
98
}
99
99
cacheSize .addAndGet (valueSize );
@@ -124,7 +124,7 @@ public void clear() {
124
124
/** Remove next file and returns it's size */
125
125
private int removeNext () {
126
126
if (lastUsageDates .isEmpty ()) {
127
- return INVALID_READ_SIZE ;
127
+ return INVALID_SIZE ;
128
128
}
129
129
Long oldestUsage = null ;
130
130
File mostLongUsedFile = null ;
@@ -144,15 +144,16 @@ private int removeNext() {
144
144
}
145
145
}
146
146
147
- int fileSize = getSize (mostLongUsedFile );
148
-
149
- if (!mostLongUsedFile .exists ()) {
150
- lastUsageDates .remove (mostLongUsedFile );
151
- return 0 ;
152
- }
153
-
154
- if ( mostLongUsedFile .delete ()) {
155
- lastUsageDates .remove (mostLongUsedFile );
147
+ int fileSize = 0 ;
148
+ if (mostLongUsedFile != null ) {
149
+ if (mostLongUsedFile .exists ()) {
150
+ fileSize = getSize (mostLongUsedFile );
151
+ if (mostLongUsedFile .delete ()) {
152
+ lastUsageDates .remove (mostLongUsedFile );
153
+ }
154
+ } else {
155
+ lastUsageDates .remove (mostLongUsedFile );
156
+ }
156
157
}
157
158
return fileSize ;
158
159
}
0 commit comments