From 428a10df1f566a6c8b859c8be5c1ae3a41817567 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Sun, 10 Feb 2019 10:09:08 -0800 Subject: [PATCH] Add comments to LRU cache class --- zuul-preview/main.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc index be74564..831b456 100644 --- a/zuul-preview/main.cc +++ b/zuul-preview/main.cc @@ -36,11 +36,19 @@ vector split(const string &in) } class Cache { + // A queue of hostname, URL pairs. The head of the queue is always + // the most recently accessed entry, the tail is the least. list> queue; + + // A map of hostname -> iterator that points into the queue, for + // quick lookup. unordered_map>::iterator> map; + + // The maximum size of the cache. uint size; public: + // A constant returned by get if the entry is not found. string notfound = ""; Cache(uint s) @@ -49,6 +57,9 @@ public: size = s; } + // Lookup the hostname in the cache and return the URL if present, + // notfound otherwise. If the entry is present, it is moved to the + // head of the queue. string get(string key) { auto location = map.find(key); @@ -62,6 +73,8 @@ public: return val.second; } + // Add an entry to the cache. If the cache is full, drop the least + // recently used entry. void put(string key, string value) { auto location = map.find(key);