|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper;
|
4 | 4 | import java.io.IOException;
|
| 5 | +import java.nio.file.FileVisitResult; |
5 | 6 | import java.nio.file.Files;
|
6 | 7 | import java.nio.file.Path;
|
| 8 | +import java.nio.file.SimpleFileVisitor; |
| 9 | +import java.nio.file.attribute.BasicFileAttributes; |
7 | 10 | import java.time.Duration;
|
8 | 11 | import java.time.Instant;
|
9 | 12 | import java.time.temporal.TemporalAmount;
|
@@ -74,17 +77,50 @@ private Path resolveContentFile(String operation, String filename) {
|
74 | 77 | return cacheNamespaceDir.resolve(operation).resolve(filename);
|
75 | 78 | }
|
76 | 79 |
|
77 |
| - private CacheIndex loadCacheIndex() throws IOException { |
| 80 | + private CacheIndex loadCacheIndex() { |
78 | 81 | final Path cacheIndexPath = cacheNamespaceDir.resolve(CACHE_INDEX_FILENAME);
|
79 | 82 | if (Files.exists(cacheIndexPath)) {
|
80 | 83 | log.debug("Loading cache index from {}", cacheIndexPath);
|
81 |
| - return objectMapper.readValue(cacheIndexPath.toFile(), CacheIndex.class); |
| 84 | + try { |
| 85 | + return objectMapper.readValue(cacheIndexPath.toFile(), CacheIndex.class); |
| 86 | + } catch (IOException e) { |
| 87 | + log.warn("Failed to load API cache index from {}", cacheIndexPath, e); |
| 88 | + wipeCacheDirectory(); |
| 89 | + return new CacheIndex(); |
| 90 | + } |
82 | 91 | }
|
83 | 92 | else {
|
84 | 93 | return new CacheIndex();
|
85 | 94 | }
|
86 | 95 | }
|
87 | 96 |
|
| 97 | + private void wipeCacheDirectory() { |
| 98 | + if (!Files.exists(cacheNamespaceDir)) { |
| 99 | + log.debug("Skipping wipe of non-existent cache directory {}", cacheNamespaceDir); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + log.debug("Wiping cache directory {}", cacheNamespaceDir); |
| 104 | + try { |
| 105 | + Files.walkFileTree(cacheNamespaceDir, new SimpleFileVisitor<Path>() { |
| 106 | + @Override |
| 107 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 108 | + if (file.getFileName().toString().endsWith(".json")) { |
| 109 | + try { |
| 110 | + log.debug("Wiping cache file {}", file); |
| 111 | + Files.delete(file); |
| 112 | + } catch (IOException e) { |
| 113 | + log.warn("Failed to delete cache file {}", file, e); |
| 114 | + } |
| 115 | + } |
| 116 | + return FileVisitResult.CONTINUE; |
| 117 | + } |
| 118 | + }); |
| 119 | + } catch (IOException e) { |
| 120 | + log.warn("Unexpected failure while wiping cache directory", e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
88 | 124 | @Override
|
89 | 125 | public <R> Mono<R> cache(String operation, Class<R> returnType, Mono<R> resolver, Object... keys) {
|
90 | 126 | final String keysKey = Stream.of(keys)
|
|
0 commit comments