Skip to content

Commit e1e4fb3

Browse files
author
Mattia Bertorello
committed
Refactoring FileDownloaderCache
1 parent 5dba31b commit e1e4fb3

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

arduino-core/src/cc/arduino/utils/network/FileDownloaderCache.java

+49-36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
1313
import processing.app.BaseNoGui;
14+
import processing.app.PreferencesData;
1415
import processing.app.helpers.FileUtils;
1516

1617
import javax.script.ScriptException;
@@ -37,6 +38,7 @@ public class FileDownloaderCache {
3738
private static Map<String, FileCached> cachedFiles = Collections
3839
.synchronizedMap(new HashMap<>());
3940
private static String cacheFolder;
41+
private static boolean enableCache;
4042

4143
static {
4244
final File settingsFolder;
@@ -63,42 +65,46 @@ public class FileDownloaderCache {
6365
} catch (Exception e) {
6466
log.error("Cannot initialized the cache", e);
6567
}
68+
enableCache = Boolean.valueOf(PreferencesData.get("cache.enable", "true"));
69+
if (!enableCache) {
70+
log.info("The cache is disable cache.enable=false");
71+
}
6672
}
6773

6874
public static Optional<FileCached> getFileCached(URL remoteURL)
6975
throws URISyntaxException, NoSuchMethodException, ScriptException,
7076
IOException {
7177

72-
final Optional<FileCached> fileCachedOpt;
73-
74-
// The file info must exist in the cachedFiles map but also the real file must exist in the file system
75-
if (cachedFiles.containsKey(remoteURL.toString()) && cachedFiles.get(remoteURL.toString()).exists()) {
76-
fileCachedOpt = Optional.of(cachedFiles.get(remoteURL.toString()));
77-
} else {
78-
// Update
79-
fileCachedOpt = FileDownloaderCache.updateCacheInfo(remoteURL, (remoteETagClean, cacheControl) -> {
80-
// Check cache control data
81-
if (cacheControl.isNoCache() || cacheControl.isMustRevalidate()) {
82-
log.warn("The file must not be cache due to cache control header {}",
83-
cacheControl);
84-
return Optional.empty();
85-
}
78+
final String[] splitPath = remoteURL.getPath().split("/");
79+
if (splitPath.length == 0) {
80+
log.warn("The remote path as no file name {}", remoteURL);
81+
return Optional.empty();
82+
}
8683

87-
final String[] splitPath = remoteURL.getPath().split("/");
88-
final Path cacheFilePath;
89-
if (splitPath.length > 0) {
90-
Deque<String> addFirstRemoteURL = new LinkedList<>(Arrays.asList(splitPath));
91-
addFirstRemoteURL.addFirst(remoteURL.getHost());
92-
cacheFilePath = Paths.get(cacheFolder, addFirstRemoteURL.toArray(new String[0]));
93-
return Optional.of(
94-
new FileCached(remoteETagClean, remoteURL.toString(), cacheFilePath.toString(),
95-
cacheControl));
96-
}
97-
log.warn("The remote path as no file name {}", remoteURL);
98-
return Optional.empty();
84+
// Take from the cache the file info or build from scratch
85+
final FileCached fileCachedOpt = Optional.ofNullable(cachedFiles.get(remoteURL.toString()))
86+
.orElseGet(() -> {
87+
Deque<String> addFirstRemoteURL = new LinkedList<>(Arrays.asList(splitPath));
88+
addFirstRemoteURL.addFirst(remoteURL.getHost());
89+
final Path cacheFilePath = Paths.get(cacheFolder, addFirstRemoteURL.toArray(new String[0]));
90+
return new FileCached(remoteURL.toString(), cacheFilePath.toString());
9991
});
92+
// If the file is change of the cache is disable run the HEAD request to check if the file is changed
93+
if (fileCachedOpt.isChange() || !enableCache) {
94+
// Update remote etag and cache control header
95+
return FileDownloaderCache.updateCacheInfo(remoteURL, (remoteETagClean, cacheControl) -> {
96+
// Check cache control data
97+
if (cacheControl.isNoCache() || cacheControl.isMustRevalidate() || cacheControl.isNoStore()) {
98+
log.warn("The file {} must not be cache due to cache control header {}",
99+
remoteURL, cacheControl);
100+
return Optional.empty();
101+
}
102+
fileCachedOpt.setLastETag(remoteETagClean);
103+
fileCachedOpt.setCacheControl(cacheControl);
104+
return Optional.of(fileCachedOpt);
105+
});
100106
}
101-
return fileCachedOpt;
107+
return Optional.of(fileCachedOpt);
102108
}
103109

104110
private static Optional<FileCached> updateCacheInfo(URL remoteURL, BiFunction<String, CacheControl, Optional<FileCached>> getNewFile)
@@ -159,27 +165,22 @@ private static Path getCachedInfoPath() {
159165

160166
@JsonIgnoreProperties(ignoreUnknown = true)
161167
static class FileCached {
162-
private String eTag;
163-
@JsonIgnore
164-
private final String lastETag;
165168
private final String remoteURL;
166169
private final String localPath;
170+
private String eTag;
171+
private String lastETag;
167172
private String md5;
168173
private String createdAt;
169-
private final CacheControl cacheControl;
174+
private CacheControl cacheControl;
170175

171176
FileCached() {
172-
this.lastETag = null;
173177
this.remoteURL = null;
174178
this.localPath = null;
175-
this.cacheControl = null;
176179
}
177180

178-
FileCached(String lastETag, String remoteURL, String localPath, CacheControl cacheControl) {
179-
this.lastETag = lastETag;
181+
FileCached(String remoteURL, String localPath) {
180182
this.remoteURL = remoteURL;
181183
this.localPath = localPath;
182-
this.cacheControl = cacheControl;
183184
}
184185

185186
@JsonIgnore
@@ -295,6 +296,18 @@ public CacheControl getCacheControl() {
295296
return cacheControl;
296297
}
297298

299+
public void seteTag(String eTag) {
300+
this.eTag = eTag;
301+
}
302+
303+
public void setLastETag(String lastETag) {
304+
this.lastETag = lastETag;
305+
}
306+
307+
public void setCacheControl(CacheControl cacheControl) {
308+
this.cacheControl = cacheControl;
309+
}
310+
298311
@Override
299312
public String toString() {
300313
return "FileCached{" +

0 commit comments

Comments
 (0)