Browse Source

Add LRU cache

changes/91/636791/1
James E. Blair 3 months ago
parent
commit
a5f2e26095
1 changed files with 64 additions and 2 deletions
  1. 64
    2
      zuul-preview/main.cc

+ 64
- 2
zuul-preview/main.cc View File

@@ -20,6 +20,7 @@
20 20
 #include <config.h>
21 21
 #include <pthread.h>
22 22
 #include <cpprest/http_client.h>
23
+#include <bits/stdc++.h>
23 24
 
24 25
 using namespace std;
25 26
 
@@ -34,16 +35,73 @@ vector<string> split(const string &in)
34 35
   return parts;
35 36
 }
36 37
 
38
+class Cache {
39
+  list<pair<string, string>> queue;
40
+  unordered_map<string, list<pair<string, string>>::iterator> map;
41
+  uint size;
42
+
43
+public:
44
+  string notfound = "";
45
+
46
+  Cache(uint s)
47
+    : queue {}, map {}, size(0)
48
+  {
49
+    size = s;
50
+  }
51
+
52
+  string get(string key)
53
+  {
54
+    auto location = map.find(key);
55
+    if (location == map.end())
56
+      return notfound;
57
+
58
+    pair<string, string> val = *(location->second);
59
+    queue.erase(location->second);
60
+    queue.push_front(val);
61
+    cout << "get push " << val.second << endl;
62
+    return val.second;
63
+  }
64
+
65
+  void put(string key, string value)
66
+  {
67
+    auto location = map.find(key);
68
+    if (location != map.end())
69
+      return;
70
+
71
+    if (queue.size() == size) {
72
+      pair<string, string> last = queue.back();
73
+      cout << "put pop " << last.second << endl;
74
+      queue.pop_back();
75
+      map.erase(last.first);
76
+    }
77
+
78
+    cout << "put push " << value << endl;
79
+    queue.push_front(make_pair(key, value));
80
+    map[key] = queue.begin();
81
+  }
82
+};
83
+
84
+
37 85
 int main(int, char**)
38 86
 {
39 87
   web::http::client::http_client client("https://zuul.opendev.org");
40 88
 
41 89
   string hostname;
90
+  Cache cache = Cache(2);
42 91
   while (getline(cin, hostname)) {
43 92
     // Expected hostname:
44 93
     // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
45 94
     // Apache will drop "preview.opendev.org", so our expected input will be:
46 95
     // site.75031cad206c4014ad7a3387091d15ab.openstack
96
+    // site.7c16d914db5a4c4b91cd9a31d119dd48.openstack
97
+    // site.688b70499b9a41a08f498ed6e932960c.openstack
98
+    // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
99
+
100
+    string val = cache.get(hostname);
101
+    if (val != cache.notfound) {
102
+      cout << val << endl;
103
+      continue;
104
+    }
47 105
 
48 106
     auto parts = split(hostname);
49 107
     if (parts.size() < 3) {
@@ -53,9 +111,11 @@ int main(int, char**)
53 111
     auto artifact = parts[0];
54 112
     auto buildid = parts[1];
55 113
     auto tenant = parts[2];
114
+    /*
56 115
     cout << artifact << endl
57 116
          << buildid << endl
58 117
          << tenant << endl;
118
+    */
59 119
 
60 120
      // 75031cad206c4014ad7a3387091d15ab
61 121
     auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
@@ -64,11 +124,13 @@ int main(int, char**)
64 124
         web::http::methods::GET, uri.to_string()).get();
65 125
     // body is a web::json::value
66 126
     auto body = response.extract_json().get();
67
-    cout << response.status_code() << endl;
68
-    cout << body.serialize() << endl;
127
+    //cout << response.status_code() << endl;
128
+    //cout << body.serialize() << endl;
69 129
 
70 130
     // TODO: use artifact
71 131
     // body["log_url"].as_string() returns a const std::string&
72 132
     cout << body["log_url"].as_string() << endl;
133
+
134
+    cache.put(hostname, body["log_url"].as_string());
73 135
   }
74 136
 }

Loading…
Cancel
Save