11#if __cplusplus >= 201703L
22 : std::exception(), whatmsg(what_msg) {
26 virtual const char*
what() const noexcept {
34 virtual bool inCache(
const std::string & docName)
noexcept(
false) = 0;
35 virtual std::string
getDoc (
const std::string & docName)
noexcept(
false) = 0;
37 virtual void putDoc (
const std::string & docName,
38 const std::string & content)
noexcept(
false) = 0;
57 std::string getFilename(
const std::string & docName) {
58 return cacheDir +
"/" + docName;
63 bool directoryExist(
const std::string &s) {
65 int ret = stat (s.c_str(), &buffer);
68 if ((buffer.st_mode & S_IFMT) == S_IFDIR) {
80 static void makeDirectory (
const std::string &s) {
81#if __cplusplus >= 201703L
86 bool ret = std::filesystem::create_directories(s);
94 int ret = mkdir(s.c_str(), 0700);
97 int ret = _mkdir(s.c_str());
118 char * home = getenv(
"HOME");
120 home = getenv(
"LOCALAPPDATA");
123 cacheDir += std::string(home) +
"/.cache/";
126 char* xdg_cache_home = getenv(
"XDG_CACHE_HOME");
127 if (xdg_cache_home !=
nullptr)
128 cacheDir = std::string(xdg_cache_home) +
"/";
130 cacheDir +=
"bridges_data/cxx/";
134 char* forcedir = getenv(
"FORCE_BRIDGES_CACHEDIR");
135 if (forcedir !=
nullptr)
136 cacheDir = std::string(forcedir) +
"/";
142 virtual bool inCache(
const std::string & docName)
noexcept(
false)
override {
143 std::string filename = getFilename(docName);
145 std::ifstream in(filename);
151 virtual std::string
getDoc (
const std::string & docName)
noexcept(
false)
override {
152 std::string filename = getFilename(docName);
154 std::ifstream in(filename);
156 if (!in.good() || !(in.is_open()))
159 std::string contents;
160 in.seekg(0, std::ios::end);
161 contents.resize(in.tellg());
162 in.seekg(0, std::ios::beg);
163 in.read(&contents[0], contents.size());
172 virtual void putDoc (
const std::string & docName,
173 const std::string & content)
noexcept(
false)
override {
174 if (!directoryExist(cacheDir))
175 makeDirectory(cacheDir);
177 std::string filename = getFilename(docName);
179 std::ofstream out(filename);
180 if (!out.good() || !(out.is_open()))
183 out << content.c_str();
184 if (!out.good() || !(out.is_open()))
193 bool evict(
const std::string& docName) {
197 return std::remove(f.c_str()) == 0;
204 std::vector<std::string> v;
208 : maxCache(maxFileNumber) {
213 virtual std::string
getDoc (
const std::string& hash_value)
override {
216 content = ca.
getDoc(hash_value);
217 updateLRU(hash_value);
222 virtual bool inCache(
const std::string& hash_value)
override {
229 virtual void putDoc(
const std::string& hash_value,
const std::string& content)
override {
231 ca.
putDoc(hash_value, content);
232 updateLRU(hash_value);
235 if (v.size() >= maxCache + 1) {
236 if (ca.
evict(v[v.size() - 1]))
244 void updateLRU(std::string hash_value) {
245 for (
auto it = v.begin(); it != v.end(); ) {
246 if (*it == hash_value) {
254 v.insert(
v.begin(), hash_value);
260 string vector_string = ca.
getDoc(
"lru");
261 std::istringstream ss(vector_string);
264 while (std::getline(ss, token,
',')) {
281 out_vector = out_vector +
"," +
s;
284 ca.
putDoc(
"lru", out_vector);
CacheException(const char *what_msg="")
Definition Cache.h:21
virtual const char * what() const noexcept
Definition Cache.h:26
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:53
bool evict(const std::string &docName)
evicts a document from the cache
Definition Cache.h:193
virtual ~SimpleCache()=default
virtual void putDoc(const std::string &docName, const std::string &content) noexcept(false) override
Definition Cache.h:172
virtual std::string getDoc(const std::string &docName) noexcept(false) override
Definition Cache.h:151
virtual bool inCache(const std::string &docName) noexcept(false) override
Definition Cache.h:142
SimpleCache()
Definition Cache.h:108
virtual void putDoc(const std::string &hash_value, const std::string &content) override
Definition Cache.h:229
lruCache(int maxFileNumber=30)
Definition Cache.h:207
virtual bool inCache(const std::string &hash_value) override
Definition Cache.h:222
virtual ~lruCache()=default
virtual std::string getDoc(const std::string &hash_value) override
Definition Cache.h:213
Support for drawing Bar charts.
Definition alltypes.h:4