-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.java
More file actions
53 lines (42 loc) · 1.63 KB
/
Cache.java
File metadata and controls
53 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Map;
import java.util.HashMap;
public class Cache {
private int capacity; // max amount of entries in the cache
private EvictionPolicy evictionPolicy; // eviction policy to use
private Metrics metrics; // cache performance metrics
private Map<String, CacheEntry> entries; // map of cache entries
public Cache(int capacity, EvictionPolicy evictionPolicy){ // constructor
this.capacity = capacity;
this.evictionPolicy = evictionPolicy;
this.metrics = new Metrics();
this.entries = new HashMap<>();
}
public String get(String query){ // retrieve result from cache
if (entries.containsKey(query)){
CacheEntry entry = entries.get(query);
entry.recordAccess();
evictionPolicy.onAccess(query);
metrics.recordHit();
return entry.getResult();
}
metrics.recordMiss(); // record cache miss
if (entries.size() >= capacity){ // evict an entry if at capacity
String keyToEvict = evictionPolicy.chooseEvictionKey(
entries,
metrics
);
if (keyToEvict != null){
entries.remove(keyToEvict);
metrics.recordEviction();
}
}
String result = "Result for [" + query + "]"; // simulate query execution
CacheEntry newEntry = new CacheEntry(query, result); // creates new cache entry
entries.put(query, newEntry); //adds new cache entry
evictionPolicy.onInsert(query); // notify eviction policy of insertion
return result;
}
public Metrics getMetrics(){
return metrics; // return cache performance metrics
}
}