11
11
import org .apache .logging .log4j .LogManager ;
12
12
import org .apache .logging .log4j .Logger ;
13
13
import processing .app .BaseNoGui ;
14
+ import processing .app .PreferencesData ;
14
15
import processing .app .helpers .FileUtils ;
15
16
16
17
import javax .script .ScriptException ;
@@ -37,6 +38,7 @@ public class FileDownloaderCache {
37
38
private static Map <String , FileCached > cachedFiles = Collections
38
39
.synchronizedMap (new HashMap <>());
39
40
private static String cacheFolder ;
41
+ private static boolean enableCache ;
40
42
41
43
static {
42
44
final File settingsFolder ;
@@ -63,42 +65,46 @@ public class FileDownloaderCache {
63
65
} catch (Exception e ) {
64
66
log .error ("Cannot initialized the cache" , e );
65
67
}
68
+ enableCache = Boolean .valueOf (PreferencesData .get ("cache.enable" , "true" ));
69
+ if (!enableCache ) {
70
+ log .info ("The cache is disable cache.enable=false" );
71
+ }
66
72
}
67
73
68
74
public static Optional <FileCached > getFileCached (URL remoteURL )
69
75
throws URISyntaxException , NoSuchMethodException , ScriptException ,
70
76
IOException {
71
77
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
+ }
86
83
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 ());
99
91
});
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
+ });
100
106
}
101
- return fileCachedOpt ;
107
+ return Optional . of ( fileCachedOpt ) ;
102
108
}
103
109
104
110
private static Optional <FileCached > updateCacheInfo (URL remoteURL , BiFunction <String , CacheControl , Optional <FileCached >> getNewFile )
@@ -159,27 +165,22 @@ private static Path getCachedInfoPath() {
159
165
160
166
@ JsonIgnoreProperties (ignoreUnknown = true )
161
167
static class FileCached {
162
- private String eTag ;
163
- @ JsonIgnore
164
- private final String lastETag ;
165
168
private final String remoteURL ;
166
169
private final String localPath ;
170
+ private String eTag ;
171
+ private String lastETag ;
167
172
private String md5 ;
168
173
private String createdAt ;
169
- private final CacheControl cacheControl ;
174
+ private CacheControl cacheControl ;
170
175
171
176
FileCached () {
172
- this .lastETag = null ;
173
177
this .remoteURL = null ;
174
178
this .localPath = null ;
175
- this .cacheControl = null ;
176
179
}
177
180
178
- FileCached (String lastETag , String remoteURL , String localPath , CacheControl cacheControl ) {
179
- this .lastETag = lastETag ;
181
+ FileCached (String remoteURL , String localPath ) {
180
182
this .remoteURL = remoteURL ;
181
183
this .localPath = localPath ;
182
- this .cacheControl = cacheControl ;
183
184
}
184
185
185
186
@ JsonIgnore
@@ -295,6 +296,18 @@ public CacheControl getCacheControl() {
295
296
return cacheControl ;
296
297
}
297
298
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
+
298
311
@ Override
299
312
public String toString () {
300
313
return "FileCached{" +
0 commit comments