Skip to content

Commit

Permalink
Merge pull request #1304 from tdrwenski/fix-deleted-file-cleanup
Browse files Browse the repository at this point in the history
Fix remove deleted files from cache
  • Loading branch information
haileyajohnson committed Feb 7, 2024
2 parents b13d8b5 + 527dcfa commit aee6726
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/util/cache/FileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private static void schedule(TimerTask task, long delay) {
protected String name;
protected final int softLimit, minElements, hardLimit;
protected final long period; // msecs
private boolean removeDeleted = false;

private final AtomicBoolean disabled = new AtomicBoolean(false); // cache is disabled
protected final AtomicBoolean hasScheduled = new AtomicBoolean(false); // a cleanup is scheduled
Expand Down Expand Up @@ -147,6 +148,23 @@ public FileCache(int minElementsInMemory, int softLimit, int hardLimit, int peri
this("", minElementsInMemory, softLimit, hardLimit, period);
}

/**
* Constructor.
*
* @param name of file cache
* @param minElementsInMemory keep this number in the cache
* @param softLimit trigger a cleanup if it goes over this number.
* @param hardLimit if > 0, never allow more than this many elements. This causes a cleanup to be done in the calling
* thread.
* @param period if > 0, do periodic cleanups every this number of seconds.
* @param removeDeleted if true, then remove deleted files from the cache when a cleanup is performed.
*/
public FileCache(String name, int minElementsInMemory, int softLimit, int hardLimit, int period,
boolean removeDeleted) {
this(name, minElementsInMemory, softLimit, hardLimit, period);
this.removeDeleted = removeDeleted;
}

/**
* Constructor.
*
Expand Down Expand Up @@ -699,9 +717,11 @@ public int compareTo(Tracker o) {
synchronized void cleanup(int maxElements) {

try {
for (CacheElement.CacheFile cacheFile : files.values()) {
if (!Files.exists(Paths.get(cacheFile.ncfile.getLocation()))) {
remove(cacheFile);
if (removeDeleted) {
for (CacheElement.CacheFile cacheFile : files.values()) {
if (!Files.exists(Paths.get(cacheFile.ncfile.getLocation()))) {
remove(cacheFile);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class TestRandomAccessFileCacheCleanup {
@BeforeClass
public static void enableCache() {
RandomAccessFile.shutdown();
cache = new FileCache("RandomAccessFile", 0, 1, 1, 0);
cache = new FileCache("RandomAccessFile", 0, 1, 1, 0, true);
RandomAccessFile.setGlobalFileCache(cache);
assertThat(cache.showCache().size()).isEqualTo(0);
}
Expand Down

0 comments on commit aee6726

Please sign in to comment.