Skip to content

Commit 9fe527e

Browse files
committed
Better cache method
1 parent f2a1bd2 commit 9fe527e

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

Diff for: src/main/java/dev/latvian/apps/tinyserver/http/file/FileResponseHandler.java

+6-17
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@
77

88
@FunctionalInterface
99
public interface FileResponseHandler {
10-
record Cache(Duration duration, boolean gzip, boolean cacheStyleAndScripts) implements FileResponseHandler {
10+
record Cache(Duration duration, Duration styleAndScriptDuration, boolean gzip) implements FileResponseHandler {
1111
@Override
1212
public HTTPResponse apply(HTTPResponse response, boolean directory, Path path) {
1313
if (!directory) {
14-
if (!duration.isZero()) {
15-
if (cacheStyleAndScripts) {
16-
response = response.publicCache(duration);
17-
} else {
18-
var n = path.toString();
19-
20-
if (n.endsWith(".css") || n.endsWith(".js")) {
21-
response = response.noCache();
22-
} else {
23-
response = response.publicCache(duration);
24-
}
25-
}
26-
}
14+
var n = path.getFileName().toString();
15+
response = response.publicCache(n.endsWith(".css") || n.endsWith(".js") ? styleAndScriptDuration : duration);
2716

2817
if (gzip) {
2918
response = response.gzip();
@@ -37,12 +26,12 @@ public HTTPResponse apply(HTTPResponse response, boolean directory, Path path) {
3726
FileResponseHandler CACHE_5_MIN = cache(Duration.ofMinutes(5L));
3827
FileResponseHandler CACHE_1_HOUR = cache(Duration.ofHours(1L));
3928

40-
static FileResponseHandler cache(Duration duration, boolean gzip, boolean cacheStyleAndScripts) {
41-
return new Cache(duration, gzip, cacheStyleAndScripts);
29+
static FileResponseHandler cache(Duration duration, Duration styleAndScriptDuration, boolean gzip) {
30+
return new Cache(duration, styleAndScriptDuration, gzip);
4231
}
4332

4433
static FileResponseHandler cache(Duration duration) {
45-
return new Cache(duration, true, false);
34+
return new Cache(duration, duration, true);
4635
}
4736

4837
HTTPResponse apply(HTTPResponse response, boolean directory, Path path);

0 commit comments

Comments
 (0)