1111import java .io .FileWriter ;
1212import java .io .IOException ;
1313import java .io .InputStream ;
14+ import java .nio .file .Files ;
15+ import java .time .Duration ;
1416
1517public 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