Skip to content

Commit f1de280

Browse files
author
Ubuntu
committed
LRUCache implemented.
1 parent a0f9fac commit f1de280

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎cache.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import collections
2+
3+
'''
4+
More elboration on the code is needed.
5+
Exception Handling is required.
6+
'''
7+
class LRUCache:
8+
def __init__(self, capacity):
9+
self.capacity = capacity
10+
self.cache = collections.OrderedDict()
11+
12+
def get(self, key):
13+
try:
14+
value = self.cache.pop(key)
15+
self.cache[key] = value
16+
print("Cache Hit!!!")
17+
return value
18+
except KeyError:
19+
print("Cache Miss!!")
20+
return -1
21+
22+
def set(self, key, value):
23+
try:
24+
self.cache.pop(key)
25+
except KeyError:
26+
if len(self.cache) >= self.capacity:
27+
self.cache.popitem(last=False)
28+
self.cache[key] = value

‎event_loop.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@
66
from selector import sel
77
import os
88
from utils.event_loop_app_exception import EventLoopAppException
9+
from cache import LRUCache
10+
11+
CACHE_CAPACITY = 50
12+
NUM_OF_THREADS = 4
913

1014
class EventLoop:
1115

1216
def __init__(self, event_queue):
1317
self.event_queue = event_queue
1418
self.disk_io_queue = Queue()
19+
self.lru_cache = LRUCache(CACHE_CAPACITY)
1520

16-
for i in range(4):
21+
for i in range(NUM_OF_THREADS):
1722
t = Thread(target=self.read)
1823
t.start()
1924
print("Thread " + str(i) + " started!")
@@ -26,7 +31,19 @@ def execute(self):
2631
event = self.event_queue.dequeue()
2732
# self.event_queue.task_done()
2833
if event.is_disk_io():
29-
self.disk_io_queue.put(event)
34+
# Check whether the requested data is cached.
35+
cached_response_bytes = self.lru_cache.get(event.request_uri)
36+
37+
if cached_response_bytes != -1:
38+
event.response_bytes = cached_response_bytes
39+
event.disk_io = False
40+
event.CLIENT_SOCKET.send(HTTPResponse.respond(HTTP_200_OK, event))
41+
# event의 연결 Type에 따라 소켓 클라이언트와의 연결을 끊을지 말지 결정해야 함.
42+
# 일단은 임시로
43+
sel.unregister(event.CLIENT_SOCKET)
44+
event.CLIENT_SOCKET.close()
45+
else:
46+
self.disk_io_queue.put(event)
3047
else:
3148
event.CLIENT_SOCKET.send(HTTPResponse.respond(HTTP_200_OK, event))
3249
sel.unregister(event.CLIENT_SOCKET)
@@ -45,14 +62,13 @@ def read_aux(self):
4562
event = self.process_disk_io(event)
4663
# self.disk_io_queue.task_done()
4764
self.event_queue.enqueue(event)
48-
# Remove and return an item from the queue.
49-
# if optional args block is true and timeout is None,
50-
# block if necessary until an item is available.
5165

5266
def process_disk_io(self, event):
5367
try:
5468
with open(os.path.dirname(__file__) + '/resources' + event.request_uri, 'rb') as f:
5569
event.response_bytes = f.read()
70+
# Newly read file is inserted into cache.
71+
self.lru_cache.set(event.request_uri, event.response_bytes)
5672
except:
5773
raise EventLoopAppException(HTTP_404_NOT_FOUND, 'File does not exist. Cannot process event', event)
5874

0 commit comments

Comments
 (0)