19 virtual bool inCache(
const std::string & docName) noexcept(
false) = 0;
20 virtual std::string getDoc (
const std::string & docName) noexcept(
false) = 0;
22 virtual void putDoc (
const std::string & docName,
23 const std::string & content) noexcept(
false) = 0;
30 std::string getFilename(
const std::string & docName) {
31 return cacheDir +
"/" + docName;
36 bool directoryExist(
const std::string &s) {
38 int ret = stat (s.c_str(), &buffer);
41 if ((buffer.st_mode & S_IFMT) == S_IFDIR) {
53 void makeDirectory (
const std::string &s) {
55 int ret = mkdir(s.c_str(), 0700);
58 int ret = _mkdir(s.c_str());
68 char * home = getenv(
"HOME");
70 home = getenv(
"LOCALAPPDATA");
73 cacheDir += std::string(home) +
"/";
80 virtual bool inCache(
const std::string & docName) noexcept(
false)
override {
81 std::string filename = getFilename(docName);
83 std::ifstream in(filename);
89 virtual std::string
getDoc (
const std::string & docName) noexcept(
false)
override {
90 std::string filename = getFilename(docName);
92 std::ifstream in(filename);
94 if (!in.good() || !(in.is_open()))
99 in.seekg(0, std::ios::end);
100 contents.resize(in.tellg());
101 in.seekg(0, std::ios::beg);
102 in.read(&contents[0], contents.size());
111 virtual void putDoc (
const std::string & docName,
112 const std::string & content) noexcept(
false)
override {
113 if (!directoryExist(cacheDir))
114 makeDirectory(cacheDir);
116 std::string filename = getFilename(docName);
119 std::ofstream out(filename);
120 if (!out.good() || !(out.is_open()))
123 out << content.c_str();
124 if (!out.good() || !(out.is_open()))
133 bool evict(
const std::string& docName) {
137 return std::remove(f.c_str()) == 0;
145 std::vector<std::string> v;
149 : maxCache(maxFileNumber) {
152 virtual std::string
getDoc (
const std::string& hash_value)
override {
155 content = ca.
getDoc(hash_value);
156 updateLRU(hash_value);
161 virtual bool inCache(
const std::string& hash_value)
override {
168 virtual void putDoc(
const std::string& hash_value,
const std::string& content)
override {
170 ca.
putDoc(hash_value, content);
171 updateLRU(hash_value);
174 if (v.size() >= maxCache + 1) {
175 if (ca.
evict(v[v.size() - 1]))
185 void updateLRU(std::string hash_value) {
186 for (
auto it = v.begin(); it != v.end(); ) {
187 if (*it == hash_value) {
195 v.insert(v.begin(), hash_value);
201 string vector_string = ca.
getDoc(
"lru");
202 std::istringstream ss(vector_string);
205 while (std::getline(ss, token,
',')) {
222 out_vector = out_vector +
"," + s;
225 ca.
putDoc(
"lru", out_vector);
virtual void putDoc(const std::string &hash_value, const std::string &content) override
Definition: Cache.h:168
virtual std::string getDoc(const std::string &docName) noexcept(false) override
Definition: Cache.h:89
virtual void putDoc(const std::string &docName, const std::string &content) noexcept(false) override
Definition: Cache.h:111
SimpleCache()
Definition: Cache.h:67
virtual std::string getDoc(const std::string &hash_value) override
Definition: Cache.h:152
bool evict(const std::string &docName)
evicts a document from the cache
Definition: Cache.h:133
lruCache(int maxFileNumber=30)
Definition: Cache.h:148
virtual bool inCache(const std::string &docName) noexcept(false) override
Definition: Cache.h:80
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
virtual bool inCache(const std::string &hash_value) override
Definition: Cache.h:161