-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem10_MultiLevelCacheSystem.java
More file actions
116 lines (85 loc) · 2.77 KB
/
Problem10_MultiLevelCacheSystem.java
File metadata and controls
116 lines (85 loc) · 2.77 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.*;
class VideoData {
String videoId;
String data;
VideoData(String videoId, String data) {
this.videoId = videoId;
this.data = data;
}
}
class MultiLevelCache {
int L1_LIMIT = 10000;
int L2_LIMIT = 100000;
LinkedHashMap<String, VideoData> L1;
LinkedHashMap<String, VideoData> L2;
HashMap<String, VideoData> L3;
int l1Hits = 0;
int l2Hits = 0;
int l3Hits = 0;
MultiLevelCache() {
L1 = new LinkedHashMap<String, VideoData>(L1_LIMIT, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<String, VideoData> eldest) {
return size() > L1_LIMIT;
}
};
L2 = new LinkedHashMap<String, VideoData>(L2_LIMIT, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry<String, VideoData> eldest) {
return size() > L2_LIMIT;
}
};
L3 = new HashMap<String, VideoData>();
for (int i = 1; i <= 20; i++) {
String id = "video_" + i;
L3.put(id, new VideoData(id, "VideoContent_" + i));
}
}
public VideoData getVideo(String videoId) {
if (L1.containsKey(videoId)) {
l1Hits++;
System.out.println("L1 Cache HIT (0.5ms)");
return L1.get(videoId);
}
System.out.println("L1 Cache MISS");
if (L2.containsKey(videoId)) {
l2Hits++;
System.out.println("L2 Cache HIT (5ms)");
VideoData v = L2.get(videoId);
L1.put(videoId, v);
return v;
}
System.out.println("L2 Cache MISS");
if (L3.containsKey(videoId)) {
l3Hits++;
System.out.println("L3 Database HIT (150ms)");
VideoData v = L3.get(videoId);
L2.put(videoId, v);
return v;
}
System.out.println("Video not found");
return null;
}
public void getStatistics() {
int total = l1Hits + l2Hits + l3Hits;
double l1Rate = 0;
double l2Rate = 0;
double l3Rate = 0;
if (total > 0) {
l1Rate = (l1Hits * 100.0) / total;
l2Rate = (l2Hits * 100.0) / total;
l3Rate = (l3Hits * 100.0) / total;
}
System.out.println("L1: Hit Rate " + l1Rate + "%");
System.out.println("L2: Hit Rate " + l2Rate + "%");
System.out.println("L3: Hit Rate " + l3Rate + "%");
}
}
public class Problem10_MultiLevelCacheSystem {
public static void main(String[] args) {
MultiLevelCache cache = new MultiLevelCache();
cache.getVideo("video_5");
cache.getVideo("video_5");
cache.getVideo("video_12");
cache.getVideo("video_5");
cache.getStatistics();
}
}