-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastCache
More file actions
95 lines (47 loc) · 1.35 KB
/
Copy pathFastCache
File metadata and controls
95 lines (47 loc) · 1.35 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
you're given a cache
with 10k entries such that
User -> cache -> if exists do 1 if not do 2:
1. return that entry
2. look for it in the db and then remove the least used entry from
the cache and put that new entry in the cache and return it
examples:
1.
Web call
|
| 2
V
cache: 1,2,3,4,5,6
return 2 and update that it was used
2.
Web call
|
| 7
V
cache: 1,2,3,4,5,6
7 doesn't exist
so look for it in the db
remove the least used element, that's 1 for example
and add 7 to the cache
1+2.
Web call 1, just like with example 1
and then web call 7
but this time we won't delete 1 because it was just used
so we'll delete 2
implement the cache such that update and fetch will be O(1) operation
the cache is in memory
solution:
use a set and linked list
the elements in the cache will be sorted by timestamp of last used
the set will hold nodes for the linked list
an element in the linked list will look like this:
node {
value,
lastUsedTime,
next,
prev,
}
so when we add a new value to the cache we delete the first element in the list
and add the new element to the tail of the list
if we fetch an element that exits in the linked list, we get to it from the set,
then move move that element to the end of the list
we can change the pointers of its previous element because we have the prev pointer in the node