Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:33

0001 #pragma once
0002 
0003 #include <vector>
0004 #include <iostream>
0005 #include <chrono>
0006 #include <queue>
0007 #include <nlohmann/json.hpp>
0008 
0009 #include <nopayloadclient/exception.hpp>
0010 
0011 
0012 namespace nopayloadclient {
0013 
0014 using nlohmann::json;
0015 using std::string;
0016 
0017 class Cache {
0018 public:
0019     Cache() {};
0020     Cache(const json& config);
0021 
0022     /// should check if it exists AND
0023     /// entry is less than life time old
0024     bool contains(const string& url);
0025 
0026     json get(const string& url);
0027 
0028     /// put in first, then removed oldest entry
0029     /// until size is under max size again
0030     void set(const string& url, json& response);
0031     void trash();
0032     friend std::ostream& operator<< (std::ostream& os, const Cache& c);
0033 
0034 private:
0035     /// of the form {url_1: resp_1, url_2, resp_2, ...}
0036     json response_dict_;
0037     /// of the form {url_1: ts_1, url_2: ts_2, ...}
0038     json time_stamp_dict_;
0039     /// to find oldest element quickly
0040     std::queue<string> insertion_order_;
0041     void removeOldestEntry();
0042     bool isAboveMaxSize();
0043     double getSize();
0044     bool isEmpty();
0045     long long getNowTs();
0046     unsigned int life_time_;
0047     unsigned int max_mb_;
0048 };
0049 
0050 }