-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
32 lines (22 loc) · 974 Bytes
/
Copy pathMain.java
File metadata and controls
32 lines (22 loc) · 974 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
import java.util.Random;
public class Main {
public static void main(String[] args){
EvictionPolicy policy = new GreedyPolicy(); // choose eviction policy, in this case, Greedy
Cache cache = new Cache(5, policy); // create cache with capacity 5
String[] queries = { // sample queries
"users", "orders", "products", "reviews", "inventory", "sessions"
};
Random rand = new Random();
for (int i = 0; i < 1000; i++){ // simulates 1000 query accesses
String key = queries[rand.nextInt(queries.length)];
if (rand.nextDouble() < 0.7){ // favor read operations to reflect cache heavy workloads
cache.get(key);
}
}
Metrics m = cache.getMetrics();
System.out.println("Hits: " + m.getHits());
System.out.println("Misses: " + m.getMisses());
System.out.println("Evictions: " + m.getEvictions());
System.out.println("Hit Rate: " + m.getHitRate());
}
}