Skip to content

Commit d87171b

Browse files
committed
fixat mer
1 parent 3f107c6 commit d87171b

4 files changed

Lines changed: 43 additions & 342 deletions

File tree

src/main/java/org/example/CacheFilter.java

Lines changed: 0 additions & 170 deletions
This file was deleted.
Lines changed: 15 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,24 @@
11
package org.example;
22

3-
import java.io.IOException;
3+
import java.util.concurrent.ConcurrentHashMap;
44

5-
/**
6-
* Interface for pluggable file caching implementations.
7-
* Allows for different cache backends (in-memory, Redis, Caffeine, etc.)
8-
*/
9-
public interface FileCache {
10-
/**
11-
* Retrieves cached file or fetches from provider if not cached.
12-
* Implementation must be thread-safe.
13-
*
14-
* @param cacheKey Unique cache key (typically includes webRoot + uri)
15-
* @param provider Function to fetch file bytes if not cached
16-
* @return File bytes, or null if file not found
17-
* @throws IOException if fetch fails
18-
*/
19-
byte[] getOrFetch(String cacheKey, FileProvider provider) throws IOException;
5+
public class FileCache {
6+
private static final ConcurrentHashMap<String, byte[]> cache = new ConcurrentHashMap<>();
207

21-
/**
22-
* Clear all cached entries.
23-
*/
24-
void clearCache();
25-
26-
/**
27-
* Get cache statistics.
28-
*/
29-
CacheStats getStats();
30-
31-
/**
32-
* Functional interface for file fetching providers.
33-
*/
34-
@FunctionalInterface
35-
interface FileProvider {
36-
byte[] fetch(String uri) throws IOException;
8+
public static byte[] get(String key) {
9+
return cache.get(key);
3710
}
3811

39-
/**
40-
* Cache statistics record.
41-
*/
42-
class CacheStats {
43-
public final int entries;
44-
public final long bytes;
45-
public final int maxEntries;
46-
public final long maxBytes;
47-
public final long totalAccesses;
48-
49-
// Default-konstruktor för empty stats
50-
public CacheStats() {
51-
this(0, 0, 100, 50 * 1024 * 1024, 0);
52-
}
53-
54-
public CacheStats(int entries, long bytes, int maxEntries, long maxBytes, long totalAccesses) {
55-
this.entries = entries;
56-
this.bytes = bytes;
57-
this.maxEntries = maxEntries;
58-
this.maxBytes = maxBytes;
59-
this.totalAccesses = totalAccesses;
60-
}
61-
62-
@Override
63-
public String toString() {
64-
String bytesFormatted = formatBytes(bytes);
65-
String maxBytesFormatted = formatBytes(maxBytes);
66-
67-
return String.format(
68-
"CacheStats{entries=%d/%d, bytes=%s/%s, utilization=%.1f%%, accesses=%d}",
69-
entries, maxEntries, bytesFormatted, maxBytesFormatted,
70-
(double) bytes / maxBytes * 100, totalAccesses
71-
);
72-
}
12+
public static void put(String key, byte[] content) {
13+
cache.put(key, content);
14+
}
7315

74-
private static String formatBytes(long bytes) {
75-
if (bytes <= 0) return "0 B";
76-
final String[] units = new String[]{"B", "KB", "MB", "GB"};
77-
int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
78-
return String.format("%.1f %s", bytes / Math.pow(1024, digitGroups), units[digitGroups]);
79-
}
16+
public static void clear() {
17+
cache.clear();
18+
}
19+
public static void clearCache() {
20+
FileCache.clear();
8021
}
22+
8123
}
24+

0 commit comments

Comments
 (0)