Skip to content

Commit a2c84fe

Browse files
committed
adjusted CachePersistingService to prevent FileNotFoundException on initial start without caches
1 parent e7f7574 commit a2c84fe

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/main/java/me/steffenjacobs/syntacticjsonmatcher/service/CachePersistingService.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,31 @@ public void persistCache(LRUMap cache, String file) {
3333
sb.append(cache.get(e));
3434
sb.append("\n");
3535
}
36-
File cacheLocation = new File(DISK_CACHE_LOCATION);
37-
if (!cacheLocation.exists()) {
38-
cacheLocation.mkdirs();
39-
}
36+
37+
createDiskCacheLocationIfNecessary();
38+
4039
try (PrintWriter out = new PrintWriter(DISK_CACHE_LOCATION + file)) {
4140
out.println(sb.toString());
4241
} catch (FileNotFoundException e) {
4342
LOG.error(e.getMessage(), e);
4443
}
4544
}
4645

46+
private void createDiskCacheLocationIfNecessary() {
47+
File cacheLocation = new File(DISK_CACHE_LOCATION);
48+
if (!cacheLocation.exists()) {
49+
cacheLocation.mkdirs();
50+
}
51+
}
52+
4753
public void load(LRUMap cache, String file, Function<String, Object> keyTransformation, Function<String, Object> valueTransformation) {
4854
try {
55+
createDiskCacheLocationIfNecessary();
56+
File f = new File(DISK_CACHE_LOCATION + file);
57+
if (!f.exists()) {
58+
LOG.info("No cache file found for cache {}. It will be created automatically.", file);
59+
return;
60+
}
4961
List<String> lines = Files.readAllLines(Paths.get(DISK_CACHE_LOCATION, file));
5062
for (String line : lines) {
5163
if (!line.isEmpty()) {

0 commit comments

Comments
 (0)