-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.hpp
249 lines (210 loc) · 3.6 KB
/
lru_cache.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#ifndef lru_cache
#define lru_cache
#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_map>
#include <iostream>
#include <map>
using namespace std;
typedef long long int ll;
class node
{
public:
string key;
string value;
node *prev;
node *next;
node(string k, string v)
{
key=k;
value=v;
prev=NULL;
next=NULL;
}
};
class lrucache
{
public:
int capacity;//total size of the cache
int count=0;//no. of elements currently in the cache
node *front,*rear;
unordered_map<string,node*> mapvalues;
lrucache()
{
capacity=0;
front=NULL;
rear=NULL;
}
lrucache(int cap)
{
capacity=cap;
front=NULL;
rear=NULL;
}
//print the contents of the cache
void display()
{
node *n1=front;
//n1=front;
while(n1!=NULL)
{
cout<<"cache key: "<<n1->key<<", cache value: "<<n1->value<<endl;
n1=n1->next;
}
}
//delete a node from cache
void deletenode(node *n1)
{
cout<<"count is "<<count<<endl;
count--;
mapvalues.erase(n1->key);
cout<<"erased from map"<<endl;
if(n1==NULL)
return;
if(front==rear)
{
front=NULL;
rear=NULL;
return;
}
if(rear==n1)
{
n1->prev->next=NULL;
rear=n1->prev;
free(n1);
return;
}
if(n1 == front)
{
n1->next->prev=NULL;
front=n1->next;
}
else
{
n1->prev->next=n1->next;
n1->next->prev=n1->prev;
}
free(n1);
cout<<"exiting deletenode"<<endl;
}
//create a node and push it to the front of deque
void pushfront(string k,string v)
{
count++;
if(front==NULL)
{
node *n1=new node(k,v);
mapvalues[k]=n1;
front=n1;
rear=n1;
return;
}
node *n1=new node(k,v);
mapvalues[k]=n1;
n1->next=front;
front->prev=n1;
front=n1;
}
//move an existing node to the front of deque(most recently used node <- n1)
void movetohead(node *n1)
{
if(n1==front)
return;
if(rear->prev==NULL)
{
rear->prev=n1;
n1->next=front;
n1->prev=NULL;
front=n1;
}
if(n1==rear)
{
rear=n1->prev;
n1->prev->next=NULL;
n1->next=front;
n1->prev=NULL;
front->prev=n1;
front=n1;
return;
}
n1->next->prev=n1->prev;
n1->prev->next=n1->next;
n1->next=front;
n1->prev=NULL;
front->prev=n1;
front=n1;
}
void put_in_cache(string k,string v){
if(capacity==0)
{
//cout<<"NO CACHE SPACE\n";
return;
}
if(mapvalues.find(k)==mapvalues.end()){//put
{
if(count<capacity)
{
pushfront(k,v);
}
else
{
deletenode(rear);//delete lru node to make space for new node in cache
pushfront(k,v);
}
}
}
}
//call update after 2 phase commit successful
void update_in_cache(string k,string v)
{
if(capacity==0)
{
//cout<<"NO CACHE SPACE\n";
return;
}
if(mapvalues.find(k)!=mapvalues.end())//update
{
node *n2=mapvalues[k];
if(n2->value.compare(v)!=0)
{
n2->value=v;
}
movetohead(n2);
cout<<"updated in cache "<<endl;
}
}
string get_from_cache(string k)
{
if(capacity==0)
{
//cout<<"NO CACHE SPACE\n";
return "-1";
}
if(mapvalues.find(k)==mapvalues.end())
{
}
else
{
node *n1=mapvalues[k];
string s=n1->value;
movetohead(n1);
return s;
//No need to contact SS. Directly return value(json format string) to Client
}
}
void delete_from_cache(string key)
{
cout<<"entered delete_from_cache:"<<endl;
if(mapvalues.find(key)!=mapvalues.end())
{
cout<<"found key in map "<<key<<endl;
node *n1=mapvalues[key];
cout<<"addr is "<<n1<<endl;
deletenode(n1);
cout<<"deleted from cache in delete_from_cache func:"<<endl;
}
}
};
lrucache cache(4);
#endif