Skip to content

Commit b2ef693

Browse files
committed
updaterat i StaticFileHandler och skapat CacheFilter
# Conflicts: # src/main/java/org/example/StaticFileHandler.java
1 parent 3b34b91 commit b2ef693

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.example;
2+
3+
import java.io.IOException;
4+
5+
public class CacheFilter {
6+
private final FileCache cache = new FileCache();
7+
8+
public byte[] getOrFetch(String uri, FileProvider provider) throws IOException {
9+
if (cache.contains(uri)) {
10+
System.out.println("✓ Cache hit for: " + uri);
11+
return cache.get(uri);
12+
}
13+
System.out.println("✗ Cache miss for: " + uri);
14+
byte[] fileBytes = provider.fetch(uri);
15+
cache.put(uri, fileBytes);
16+
return fileBytes;
17+
}
18+
19+
@FunctionalInterface
20+
public interface FileProvider {
21+
byte[] fetch(String uri) throws IOException;
22+
}
23+
}

0 commit comments

Comments
 (0)