Skip to content

Commit ef5aaa9

Browse files
committed
chore: Dictionary is downloaded once per 4 hours in CLI mode.
1 parent eedba06 commit ef5aaa9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

build-jar.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mvn clean package assembly:single

src/main/java/com/github/exadmin/cyberferret/async/RunnableCheckOnlineDictionary.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import java.io.FileWriter;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14+
import java.nio.file.Files;
15+
import java.time.Duration;
1416

1517
public class RunnableCheckOnlineDictionary extends ARunnable {
18+
private static final Duration DICTIONARY_REFRESH_INTERVAL = Duration.ofHours(4);
1619

1720
public RunnableCheckOnlineDictionary(boolean isCLIMode) {
1821
super(isCLIMode);
@@ -27,10 +30,27 @@ protected void _run() throws Exception {
2730
File savePath = new File(prefix + FxConstants.DICTIONARY_FILE_PATH_ENCRYPTED);
2831

2932
if (savePath.exists()) {
33+
if (isCLIMode()) {
34+
try {
35+
long lastModifiedMillis = Files.getLastModifiedTime(savePath.toPath()).toMillis();
36+
long ageMillis = System.currentTimeMillis() - lastModifiedMillis;
37+
if (ageMillis >= 0 && ageMillis < DICTIONARY_REFRESH_INTERVAL.toMillis()) {
38+
logInfo("Skipping dictionary download, local cache is still fresh: {}", savePath.getAbsolutePath());
39+
return;
40+
}
41+
} catch (IOException ex) {
42+
logError("Failed to read change date for {}", savePath.getAbsolutePath(), ex);
43+
}
44+
}
45+
3046
boolean wasDeleted = savePath.delete();
3147
if (wasDeleted) logInfo("Previous version of downloaded copy was cleaned by path {}", savePath);
3248
}
3349

50+
downloadOnlineDictionary(savePath);
51+
}
52+
53+
protected void downloadOnlineDictionary(File savePath) {
3454
logInfo("Downloading latest online dictionary");
3555
try (CloseableHttpClient client = HttpClients.createDefault()) {
3656
HttpGet request = new HttpGet(FxConstants.CYBER_FERRET_ONLINE_DICTIONARY_URL);

0 commit comments

Comments
 (0)