-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.cpp
More file actions
52 lines (46 loc) · 915 Bytes
/
Copy pathCache.cpp
File metadata and controls
52 lines (46 loc) · 915 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;
int getCurrentTimeStamp(){
return 0;
}
template <typename K, typename V>
class CacheStore
{
unordered_map<K, pair<V, int>> _mep;
public:
void Add(K key, V value, int ttl) {
if(_mep.find(key) == _mep.end()) {
int expirationTime = getCurrentTimeStamp() + ttl;
_mep[key] = {value, expirationTime};
}
else {
}
}
bool Get(K key, V* out) {
int currentTime = getCurrentTimeStamp();
if(_mep.find(key) == _mep.end()) {
out = NULL;
return 0;
}
else {
pair<V, int> cur = _mep[key];
if (cur.second < currentTime) {
out = NULL;
return 0;
}
else {
*out = cur.first;
return 1;
}
}
}
};
int main() {
CacheStore<string, int> M;
M.Add("Flap", 1, 10);
M.Add("Flap.inc", 2, 100);
int* res = new int;
if(M.Get("Flap", res)) cout << *res << endl;
if(M.Get("Flap.inc", res)) cout << *res << endl;
return 0;
}