-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlirs.py
216 lines (155 loc) · 5.33 KB
/
lirs.py
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
"""lirs.py: Implements the Least Inter-Reference Recency Set cache eviction policy."""
class Stack:
"""A FIFO Stack (As described in paper)"""
def __init__(self):
self.data = []
def push(self, item):
self.data.append(item)
def pop(self):
if len(self.data) == 0:
return None
value, *rest = self.data
self.data = rest
return value
def peek(self):
if len(self.data) == 0:
return None
value, *_ = self.data
return value
def remove(self, value, key):
# key = function item -> typeof(value)
matches = [(i, item) for i, item in enumerate(self.data) if key(item) == value]
if len(matches) == 0:
# print ("Not found")
return None
index, item = matches[0]
del self.data[index]
return item
def is_empty(self):
return len(self.data) == 0
NR = 0
HIRS = 1
LIRS = 2
status = ["NR", "HIRS", "LIRS"]
class Item:
"""Items to to saved in Stack.
Members:
value : str
status : int
0 = Non-Resident
1 = High Inter-Reference Recency Set
2 = Low Inter-Reference Recency Set
"""
def __init__(self, value, status):
self.value = value
self.status = status
class LIRSCache:
"""A cache that implements the LIRS policy and uses an injected backing store
Members:
S : Stack
Q : Stack
size: int
max_hirs : int
max_lirs : int
cache : Dict or MemCache
"""
def __init__(self, size, cache):
self.S = Stack()
self.Q = Stack()
self.size = size
self.max_hirs = max(1, 0.1 * self.size)
self.max_lirs = self.size - self.max_hirs
self.cache = cache
def put(self, key, value):
item = self.S.remove(key, key=lambda x: x.value)
if item is not None:
# Found in S
if item.status == HIRS:
# Remove from Q
self.Q.remove(key, key=lambda x: x.value)
self.evict()
item.status = LIRS
self.migrate()
elif item.status == NR:
# Miss
item.status = LIRS
self.evict()
self.migrate()
self.prune_S()
self.S.push(item)
# insert into cache
self.cache.put(key, value)
return
item = self.Q.remove(key, key=lambda x: x.value)
if item is not None:
# found in Q
self.Q.push(item)
self.prune_S()
# insert into cache
self.cache.put(key, value)
return
# Not found anywhere
lirs_count = len([item for item in self.S.data if item.status == LIRS])
if lirs_count < self.max_lirs:
item = Item(key, LIRS)
else:
item = Item(key, HIRS)
self.evict()
self.Q.push(item)
self.S.push(item)
self.prune_S()
# insert into cache
self.cache.put(key, value)
def get(self, key):
item = self.S.remove(key, key=lambda x: x.value)
if item is not None:
if item.status == NR:
# Miss
return None
else:
# Found in S
if item.status == LIRS:
self.S.push(item)
else:
# status=HIRS
self.Q.remove(key, key=lambda x: x.value)
item.status = LIRS
self.S.push(item)
self.migrate()
self.prune_S()
return self.cache.get(key)
item = self.Q.remove(key, key=lambda x: x.value)
if item is not None:
# found in Q
self.Q.push(item)
self.prune_S()
# insert into cache
return self.cache.get(key)
return None
def prune_S(self):
while not self.S.is_empty():
if self.S.peek().status == LIRS:
break
else:
item = self.S.pop()
self.Q.remove(item.value, key=lambda x: x.value)
if item.status == HIRS:
self.cache.remove(item.value)
def evict(self):
count = len(set([item.value for item in self.S.data if item.status == HIRS] + [item.value for item in self.Q.data]))
if count < self.max_hirs:
return
victim = self.Q.pop()
self.S.remove(victim.value, key=lambda x: x.value)
# remove from cache
key = victim.value
self.cache.remove(key)
victim.status = NR
self.S.push(victim)
def migrate(self):
bottom = self.S.pop()
bottom.status = HIRS
# self.evict()
self.Q.push(bottom)
def print_stack(stack):
print (",".join(["%s %s" % (item.value, status[item.status]) for item in stack.data]))