-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLRUCache.java
More file actions
182 lines (155 loc) · 6.74 KB
/
LRUCache.java
File metadata and controls
182 lines (155 loc) · 6.74 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.*;
public class LRUCache {
int cacheCapacity = 5;
int currentCapacity = 0;
Node head = null;
Node tail = null;
Map<Integer, Node> cache = new HashMap<>();
public void get(int key) {
System.out.println();
System.out.println("==========================================================");
System.out.println("CACHE GET OPERATION");
System.out.println("CPU Requests: " + key);
Node s = cache.get(key);
if (s == null) {
System.out.println("Cache Status : Miss");
System.out.println("Cached Data : -1");
System.out.println("==========================================================");
}
else {
System.out.println("Cache Status : Hit");
System.out.println("Cached Data : " + s.value);
System.out.println("==========================================================");
if (s == tail) {
tail = s;
} else if (s == head) {
Node oldTail = tail;
tail.next = head;
tail = head;
tail.prev = oldTail;
head = head.next;
head.prev = null;
tail.next = null;
} else {
s.prev.next = s.next;
s.next.prev = s.prev;
s.next = null;
tail.next = s;
s.prev = tail;
tail = s;
}
}
System.out.println();
}
public void put(int key, String value) {
if (!cache.containsKey(key)) {
Node newNode = new Node(key, value);
if (currentCapacity != cacheCapacity) {
if (head == null && tail == null) {
head = tail = newNode;
cache.put(newNode.key, newNode);
currentCapacity++;
System.out.println();
System.out.println("==========================================================");
System.out.println("CACHE PUT OPERATION");
System.out.println("Inserted Key : " + newNode.key);
System.out.println("Cached Data : " + newNode.value);
System.out.println("Cache Status : SUCCESS");
System.out.println("==========================================================");
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
cache.put(newNode.key, newNode);
currentCapacity++;
System.out.println();
System.out.println("==========================================================");
System.out.println("CACHE PUT OPERATION");
System.out.println("Inserted Key : " + newNode.key);
System.out.println("Cached Data : " + newNode.value);
System.out.println("Cache Status : SUCCESS");
System.out.println("==========================================================");
}
} else { // cache is full
cache.remove(head.key);
currentCapacity--;
if (head.next == null) { // this is when cacheCapacity is 1
Node tempHead = head;
head = newNode;
tail = newNode;
cache.put(newNode.key, newNode);
currentCapacity++;
System.out.println();
System.out.println("==========================================================");
System.out.println("CACHE PUT OPERATION");
System.out.println("CACHE CAPACITY EXCEEDED ----> Initiating LRU Eviction Policy...");
System.out.println("Evicted Key : " + tempHead.key);
System.out.println("Inserted Key : " + newNode.key);
System.out.println("Cached Data : " + newNode.value);
System.out.println("Cache Status : SUCCESS");
System.out.println("==========================================================");
} else { // this is when cacheCapacity is > 1
Node tempHead = head;
head = head.next;
head.prev = null;
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
cache.put(newNode.key, newNode);
currentCapacity++;
System.out.println();
System.out.println("==========================================================");
System.out.println("CACHE PUT OPERATION");
System.out.println("CACHE CAPACITY EXCEEDED ----> Initiating LRU Eviction Policy...");
System.out.println("Evicted Key : " + tempHead.key);
System.out.println("Inserted Key : " + newNode.key);
System.out.println("Cached Data : " + newNode.value);
System.out.println("Cache Status : SUCCESS");
System.out.println("==========================================================");
}
}
} else { // if same key is present then update its value and move the node to tail
Node p = cache.get(key);
p.value = value;
if (p == tail) {
tail = p;
} else if (p == head) {
Node oldTail = tail;
tail.next = head;
tail = head;
tail.prev = oldTail;
head = head.next;
head.prev = null;
tail.next = null;
} else {
p.prev.next = p.next;
p.next.prev = p.prev;
tail.next = p;
p.prev = tail;
p.next = null;
tail = p;
}
}
}
public void displayCache() {
System.out.println();
System.out.println("==========================================================");
System.out.println("Current Cache State:");
Node s = head;
while (s != null) {
System.out.println("[key: " + s.key + ", Value: " + s.value + "]");
s = s.next;
}
System.out.println("==========================================================");
}
public class Node {
int key;
String value;
Node next = null;
Node prev = null;
public Node(int key, String value) {
this.key = key;
this.value = value;
}
}
}