Add LRU cache

This commit is contained in:
James E. Blair 2019-02-10 10:04:44 -08:00
parent 31d726c6e6
commit a5f2e26095
1 changed files with 64 additions and 2 deletions

View File

@ -20,6 +20,7 @@
#include <config.h>
#include <pthread.h>
#include <cpprest/http_client.h>
#include <bits/stdc++.h>
using namespace std;
@ -34,16 +35,73 @@ vector<string> split(const string &in)
return parts;
}
class Cache {
list<pair<string, string>> queue;
unordered_map<string, list<pair<string, string>>::iterator> map;
uint size;
public:
string notfound = "";
Cache(uint s)
: queue {}, map {}, size(0)
{
size = s;
}
string get(string key)
{
auto location = map.find(key);
if (location == map.end())
return notfound;
pair<string, string> val = *(location->second);
queue.erase(location->second);
queue.push_front(val);
cout << "get push " << val.second << endl;
return val.second;
}
void put(string key, string value)
{
auto location = map.find(key);
if (location != map.end())
return;
if (queue.size() == size) {
pair<string, string> last = queue.back();
cout << "put pop " << last.second << endl;
queue.pop_back();
map.erase(last.first);
}
cout << "put push " << value << endl;
queue.push_front(make_pair(key, value));
map[key] = queue.begin();
}
};
int main(int, char**)
{
web::http::client::http_client client("https://zuul.opendev.org");
string hostname;
Cache cache = Cache(2);
while (getline(cin, hostname)) {
// Expected hostname:
// site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
// Apache will drop "preview.opendev.org", so our expected input will be:
// site.75031cad206c4014ad7a3387091d15ab.openstack
// site.7c16d914db5a4c4b91cd9a31d119dd48.openstack
// site.688b70499b9a41a08f498ed6e932960c.openstack
// site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
string val = cache.get(hostname);
if (val != cache.notfound) {
cout << val << endl;
continue;
}
auto parts = split(hostname);
if (parts.size() < 3) {
@ -53,9 +111,11 @@ int main(int, char**)
auto artifact = parts[0];
auto buildid = parts[1];
auto tenant = parts[2];
/*
cout << artifact << endl
<< buildid << endl
<< tenant << endl;
*/
// 75031cad206c4014ad7a3387091d15ab
auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
@ -64,11 +124,13 @@ int main(int, char**)
web::http::methods::GET, uri.to_string()).get();
// body is a web::json::value
auto body = response.extract_json().get();
cout << response.status_code() << endl;
cout << body.serialize() << endl;
//cout << response.status_code() << endl;
//cout << body.serialize() << endl;
// TODO: use artifact
// body["log_url"].as_string() returns a const std::string&
cout << body["log_url"].as_string() << endl;
cache.put(hostname, body["log_url"].as_string());
}
}