summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames E. Blair <jeblair@redhat.com>2019-02-10 10:09:08 -0800
committerJames E. Blair <jeblair@redhat.com>2019-02-10 10:09:08 -0800
commit428a10df1f566a6c8b859c8be5c1ae3a41817567 (patch)
tree5bca0cd0f12b63186bd3a8e72aeb8d5e2b3f6936
parenta5f2e260951edfea837c31bd615ee0cae543b75b (diff)
Add comments to LRU cache class
-rw-r--r--zuul-preview/main.cc13
1 files changed, 13 insertions, 0 deletions
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<string> split(const string &in)
36} 36}
37 37
38class Cache { 38class Cache {
39 // A queue of hostname, URL pairs. The head of the queue is always
40 // the most recently accessed entry, the tail is the least.
39 list<pair<string, string>> queue; 41 list<pair<string, string>> queue;
42
43 // A map of hostname -> iterator that points into the queue, for
44 // quick lookup.
40 unordered_map<string, list<pair<string, string>>::iterator> map; 45 unordered_map<string, list<pair<string, string>>::iterator> map;
46
47 // The maximum size of the cache.
41 uint size; 48 uint size;
42 49
43public: 50public:
51 // A constant returned by get if the entry is not found.
44 string notfound = ""; 52 string notfound = "";
45 53
46 Cache(uint s) 54 Cache(uint s)
@@ -49,6 +57,9 @@ public:
49 size = s; 57 size = s;
50 } 58 }
51 59
60 // Lookup the hostname in the cache and return the URL if present,
61 // notfound otherwise. If the entry is present, it is moved to the
62 // head of the queue.
52 string get(string key) 63 string get(string key)
53 { 64 {
54 auto location = map.find(key); 65 auto location = map.find(key);
@@ -62,6 +73,8 @@ public:
62 return val.second; 73 return val.second;
63 } 74 }
64 75
76 // Add an entry to the cache. If the cache is full, drop the least
77 // recently used entry.
65 void put(string key, string value) 78 void put(string key, string value)
66 { 79 {
67 auto location = map.find(key); 80 auto location = map.find(key);