-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUPolicy.java
More file actions
35 lines (27 loc) · 878 Bytes
/
LRUPolicy.java
File metadata and controls
35 lines (27 loc) · 878 Bytes
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
import java.util.Queue;
import java.util.ArrayDeque;
import java.util.Map;
//LRU models on recency
public class LRUPolicy implements EvictionPolicy {
private Queue<String> queue; //stores keys in order of access
public LRUPolicy(){
this.queue = new ArrayDeque<>();
}
@Override
public void onInsert(String key){ //called when new entry is added to cache
queue.add(key);
}
@Override
public void onAccess(String key){ //called when something is read
//move accessed key to the end of the queue (most recently used)
queue.remove(key);
queue.add(key);
}
@Override
public String chooseEvictionKey(
Map<String, CacheEntry> entries,
Metrics metrics
){
return queue.poll(); //removes and returns the least recently used key
}
}