Skip to content

Commit 162061f

Browse files
committed
prioritize cache files if less than a day old
1 parent 416b9c3 commit 162061f

3 files changed

Lines changed: 160 additions & 21 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.ornithemc.ploceus;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.time.Duration;
7+
import java.time.Instant;
8+
9+
public class CacheFiles {
10+
11+
public static final Duration ONE_HOUR = Duration.ofHours(1L);
12+
public static final Duration ONE_DAY = Duration.ofDays(1L);
13+
14+
public static boolean isStale(Path file, Duration maxAge) throws IOException {
15+
Instant modifiedTime = Files.getLastModifiedTime(file).toInstant();
16+
Instant minModifiedTime = Instant.now().minus(maxAge);
17+
18+
return modifiedTime.isBefore(minModifiedTime);
19+
}
20+
}

src/main/java/net/ornithemc/ploceus/LibraryUpgradesCache.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.BufferedWriter;
5+
import java.io.IOException;
56
import java.io.InputStreamReader;
67
import java.net.URI;
78
import java.nio.file.Files;
@@ -77,23 +78,62 @@ public List<Library> getLibraryUpgrades() {
7778
private List<Library> getLibraries() {
7879
List<Library> libs = null;
7980

80-
try {
81-
libs = getLibrariesFromMeta();
82-
} catch (Exception me) {
81+
Exception me = null;
82+
Exception ce = null;
83+
84+
boolean prioritizeCache = shouldPrioritizeCacheForLibraries();
85+
86+
if (prioritizeCache) {
8387
try {
8488
libs = getLibrariesFromCache();
85-
} catch (Exception ce) {
86-
project.getLogger().warn("unable to read library upgrades from cache for gen" + intermediaryGeneration() + " " + minecraftVersion(), ce);
89+
} catch (Exception e) {
90+
ce = e;
91+
}
92+
}
93+
94+
if (libs == null) {
95+
try {
96+
libs = getLibrariesFromMeta();
97+
} catch (Exception e) {
98+
me = e;
99+
}
100+
}
101+
102+
if (libs == null) {
103+
try {
104+
libs = getLibrariesFromCache();
105+
} catch (Exception e) {
106+
ce = e;
87107
}
108+
}
88109

89-
if (libs == null) {
90-
throw new IllegalStateException("unable to fetch library upgrades from meta for gen" + intermediaryGeneration() + " " + minecraftVersion() + ", and it is not in the cache", me);
110+
if (libs == null) {
111+
if (ce != null) {
112+
project.getLogger().warn("unable to read library upgrades from cache for gen" + intermediaryGeneration() + " " + minecraftVersion(), ce);
91113
}
114+
115+
throw new IllegalStateException("unable to fetch library upgrades from meta for gen" + intermediaryGeneration() + " " + minecraftVersion() + ", and it is not in the cache", me);
92116
}
93117

94118
return libs;
95119
}
96120

121+
private boolean shouldPrioritizeCacheForLibraries() {
122+
Path libsCache = librariesCache();
123+
124+
try {
125+
return Files.exists(libsCache) && !CacheFiles.isStale(libsCache, CacheFiles.ONE_DAY);
126+
} catch (IOException e) {
127+
try {
128+
Files.deleteIfExists(libsCache);
129+
} catch (IOException de) {
130+
throw new IllegalStateException("unable to delete corrupt cache file", e);
131+
}
132+
133+
return false;
134+
}
135+
}
136+
97137
private List<Library> getLibrariesFromMeta() throws Exception {
98138
String metaUrl = Constants.librariesMetaUrl(minecraftVersion(), intermediaryGeneration());
99139

src/main/java/net/ornithemc/ploceus/OslVersionCache.java

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.BufferedWriter;
5+
import java.io.IOException;
56
import java.io.InputStreamReader;
67
import java.net.URI;
78
import java.nio.file.Files;
@@ -101,23 +102,62 @@ public String getOslModuleBaseVersion(String version, String module) {
101102
private Map<String, String> getModuleBaseVersions(String version) {
102103
Map<String, String> baseVersions = null;
103104

104-
try {
105-
baseVersions = getModuleBaseVersionsFromMeta(version);
106-
} catch (Exception me) {
105+
Exception me = null;
106+
Exception ce = null;
107+
108+
boolean prioritizeCache = shouldPrioritizeCacheForModuleBaseVersions();
109+
110+
if (prioritizeCache) {
107111
try {
108112
baseVersions = getModuleBaseVersionsFromCache(version);
109-
} catch (Exception ce) {
110-
project.getLogger().warn("unable to read OSL module base versions from cache for gen" + intermediaryGeneration() + " " + version, ce);
113+
} catch (Exception e) {
114+
ce = e;
111115
}
116+
}
112117

113-
if (baseVersions == null) {
114-
throw new IllegalStateException("unable to fetch OSL module base versions from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me);
118+
if (baseVersions == null) {
119+
try {
120+
baseVersions = getModuleBaseVersionsFromMeta(version);
121+
} catch (Exception e) {
122+
me = e;
115123
}
116124
}
117125

126+
if (baseVersions == null) {
127+
try {
128+
baseVersions = getModuleBaseVersionsFromCache(version);
129+
} catch (Exception e) {
130+
ce = e;
131+
}
132+
}
133+
134+
if (baseVersions == null) {
135+
if (ce != null) {
136+
project.getLogger().warn("unable to read OSL module base versions from cache for gen" + intermediaryGeneration() + " " + version, ce);
137+
}
138+
139+
throw new IllegalStateException("unable to fetch OSL module base versions from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me);
140+
}
141+
118142
return baseVersions;
119143
}
120144

145+
private boolean shouldPrioritizeCacheForModuleBaseVersions() {
146+
Path baseVersionsCache = moduleBaseVersionsCache();
147+
148+
try {
149+
return Files.exists(baseVersionsCache) && !CacheFiles.isStale(baseVersionsCache, CacheFiles.ONE_DAY);
150+
} catch (IOException e) {
151+
try {
152+
Files.deleteIfExists(baseVersionsCache);
153+
} catch (IOException de) {
154+
throw new IllegalStateException("unable to delete corrupt cache file", e);
155+
}
156+
157+
return false;
158+
}
159+
}
160+
121161
private Map<String, String> getModuleBaseVersionsFromMeta(String version) throws Exception {
122162
String metaUrl = Constants.META_URL + Constants.oslVersionMetaEndpoint(intermediaryGeneration(), version);
123163

@@ -237,23 +277,62 @@ public String getOslModuleVersion(String module, String version, GameSide side)
237277
private String getModuleVersion(String module, String version, GameSide side) {
238278
String moduleVersion = null;
239279

240-
try {
241-
moduleVersion = getModuleVersionFromMeta(module, version, side);
242-
} catch (Exception me) {
280+
Exception me = null;
281+
Exception ce = null;
282+
283+
boolean prioritizeCache = shouldPrioritizeCacheForModuleVersions();
284+
285+
if (prioritizeCache) {
286+
try {
287+
moduleVersion = getModuleVersionFromCache(module, version, side);
288+
} catch (Exception e) {
289+
ce = e;
290+
}
291+
}
292+
293+
if (moduleVersion == null) {
294+
try {
295+
moduleVersion = getModuleVersionFromMeta(module, version, side);
296+
} catch (Exception e) {
297+
me = e;
298+
}
299+
}
300+
301+
if (moduleVersion == null) {
243302
try {
244303
moduleVersion = getModuleVersionFromCache(module, version, side);
245-
} catch (Exception ce) {
246-
project.getLogger().warn("unable to read OSL module version from cache for gen" + intermediaryGeneration() + " " + module + " " + version, ce);
304+
} catch (Exception e) {
305+
ce = e;
247306
}
307+
}
248308

249-
if (moduleVersion == null) {
250-
throw new IllegalStateException("unable to fetch OSL module version from meta for gen" + intermediaryGeneration() + " " + module + " " + version + ", and it is not in the cache", me);
309+
if (moduleVersion == null) {
310+
if (ce != null) {
311+
project.getLogger().warn("unable to read OSL module version from cache for gen" + intermediaryGeneration() + " " + version, ce);
251312
}
313+
314+
throw new IllegalStateException("unable to fetch OSL module version from meta for gen" + intermediaryGeneration() + " " + version + ", and it is not in the cache", me);
252315
}
253316

254317
return moduleVersion;
255318
}
256319

320+
private boolean shouldPrioritizeCacheForModuleVersions() {
321+
Path versionsCache = moduleVersionsCache();
322+
323+
try {
324+
return Files.exists(versionsCache) && !CacheFiles.isStale(versionsCache, CacheFiles.ONE_DAY);
325+
} catch (IOException e) {
326+
try {
327+
Files.deleteIfExists(versionsCache);
328+
} catch (IOException de) {
329+
throw new IllegalStateException("unable to delete corrupt cache file", e);
330+
}
331+
332+
return false;
333+
}
334+
}
335+
257336
private String getModuleVersionFromMeta(String module, String version, GameSide side) throws Exception {
258337
String metaUrl = Constants.META_URL + Constants.oslModuleVersionMetaEndpoint(intermediaryGeneration(), module, minecraftVersion(), version);
259338

0 commit comments

Comments
 (0)