Browse Source

Add comments to LRU cache class

changes/91/636791/1
James E. Blair 3 months ago
parent
commit
428a10df1f
1 changed files with 13 additions and 0 deletions
  1. 13
    0
      zuul-preview/main.cc

+ 13
- 0
zuul-preview/main.cc View File

@@ -36,11 +36,19 @@ vector<string> split(const string &in)
36 36
 }
37 37
 
38 38
 class 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 41
   list<pair<string, string>> queue;
42
+
43
+  // A map of hostname -> iterator that points into the queue, for
44
+  // quick lookup.
40 45
   unordered_map<string, list<pair<string, string>>::iterator> map;
46
+
47
+  // The maximum size of the cache.
41 48
   uint size;
42 49
 
43 50
 public:
51
+  // A constant returned by get if the entry is not found.
44 52
   string notfound = "";
45 53
 
46 54
   Cache(uint s)
@@ -49,6 +57,9 @@ public:
49 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 63
   string get(string key)
53 64
   {
54 65
     auto location = map.find(key);
@@ -62,6 +73,8 @@ public:
62 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 78
   void put(string key, string value)
66 79
   {
67 80
     auto location = map.find(key);

Loading…
Cancel
Save