11 #if __cplusplus >= 201703L
23 : std::exception(), whatmsg(what_msg) {
27 virtual const char*
what() const noexcept {
35 virtual bool inCache(
const std::string & docName) noexcept(
false) = 0;
36 virtual std::string
getDoc (
const std::string & docName) noexcept(
false) = 0;
38 virtual void putDoc (
const std::string & docName,
39 const std::string & content) noexcept(
false) = 0;
58 std::string getFilename(
const std::string & docName) {
59 return cacheDir +
"/" + docName;
64 bool directoryExist(
const std::string &s) {
66 int ret = stat (s.c_str(), &buffer);
69 if ((buffer.st_mode & S_IFMT) == S_IFDIR) {
81 static void makeDirectory (
const std::string &s) {
82 #if __cplusplus >= 201703L
87 bool ret = std::filesystem::create_directories(s);
95 int ret = mkdir(s.c_str(), 0700);
98 int ret = _mkdir(s.c_str());
120 char * home = getenv(
"HOME");
122 home = getenv(
"LOCALAPPDATA");
125 cacheDir += std::string(home) +
"/.cache/";
129 char* xdg_cache_home = getenv(
"XDG_CACHE_HOME");
130 if (xdg_cache_home !=
nullptr)
131 cacheDir = std::string(xdg_cache_home) +
"/";
133 cacheDir +=
"bridges_data/cxx/";
138 char* forcedir = getenv(
"FORCE_BRIDGES_CACHEDIR");
139 if (forcedir !=
nullptr)
140 cacheDir = std::string(forcedir) +
"/";
146 virtual bool inCache(
const std::string & docName) noexcept(
false)
override {
147 std::string filename = getFilename(docName);
149 std::ifstream in(filename);
155 virtual std::string
getDoc (
const std::string & docName) noexcept(
false)
override {
156 std::string filename = getFilename(docName);
158 std::ifstream in(filename);
160 if (!in.good() || !(in.is_open()))
163 std::string contents;
164 in.seekg(0, std::ios::end);
165 contents.resize(in.tellg());
166 in.seekg(0, std::ios::beg);
167 in.read(&contents[0], contents.size());
176 virtual void putDoc (
const std::string & docName,
177 const std::string & content) noexcept(
false)
override {
178 if (!directoryExist(cacheDir))
179 makeDirectory(cacheDir);
181 std::string filename = getFilename(docName);
183 std::ofstream out(filename);
184 if (!out.good() || !(out.is_open()))
187 out << content.c_str();
188 if (!out.good() || !(out.is_open()))
197 bool evict(
const std::string& docName) {
201 return std::remove(f.c_str()) == 0;
208 std::vector<std::string> v;
212 : maxCache(maxFileNumber) {
217 virtual std::string
getDoc (
const std::string& hash_value)
override {
220 content = ca.
getDoc(hash_value);
221 updateLRU(hash_value);
226 virtual bool inCache(
const std::string& hash_value)
override {
233 virtual void putDoc(
const std::string& hash_value,
const std::string& content)
override {
235 ca.
putDoc(hash_value, content);
236 updateLRU(hash_value);
239 if (v.size() >= maxCache + 1) {
240 if (ca.
evict(v[v.size() - 1]))
248 void updateLRU(std::string hash_value) {
249 for (
auto it = v.begin(); it != v.end(); ) {
250 if (*it == hash_value) {
258 v.insert(
v.begin(), hash_value);
264 string vector_string = ca.
getDoc(
"lru");
265 std::istringstream ss(vector_string);
268 while (std::getline(ss, token,
',')) {
285 out_vector = out_vector +
"," +
s;
288 ca.
putDoc(
"lru", out_vector);
CacheException(const char *what_msg="")
Definition: Cache.h:22
virtual const char * what() const noexcept
Definition: Cache.h:27
virtual std::string getDoc(const std::string &docName) noexcept(false)=0
virtual bool inCache(const std::string &docName) noexcept(false)=0
virtual void putDoc(const std::string &docName, const std::string &content) noexcept(false)=0
object managing a disk cache for which ever purpose needed.
Definition: Cache.h:54
bool evict(const std::string &docName)
evicts a document from the cache
Definition: Cache.h:197
virtual ~SimpleCache()=default
virtual void putDoc(const std::string &docName, const std::string &content) noexcept(false) override
Definition: Cache.h:176
virtual std::string getDoc(const std::string &docName) noexcept(false) override
Definition: Cache.h:155
virtual bool inCache(const std::string &docName) noexcept(false) override
Definition: Cache.h:146
SimpleCache()
Definition: Cache.h:110
virtual void putDoc(const std::string &hash_value, const std::string &content) override
Definition: Cache.h:233
lruCache(int maxFileNumber=30)
Definition: Cache.h:211
virtual bool inCache(const std::string &hash_value) override
Definition: Cache.h:226
virtual ~lruCache()=default
virtual std::string getDoc(const std::string &hash_value) override
Definition: Cache.h:217
Support for drawing Bar charts.
Definition: alltypes.h:4