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) +
"/";
82 virtual bool inCache(
const std::string & docName) noexcept(
false)
override {
83 std::string filename = getFilename(docName);
85 std::ifstream in(filename);
91 virtual std::string
getDoc (
const std::string & docName) noexcept(
false)
override {
92 std::string filename = getFilename(docName);
94 std::ifstream in(filename);
96 if (!in.good() || !(in.is_open()))
100 std::string contents;
101 in.seekg(0, std::ios::end);
102 contents.resize(in.tellg());
103 in.seekg(0, std::ios::beg);
104 in.read(&contents[0], contents.size());
113 virtual void putDoc (
const std::string & docName,
114 const std::string & content) noexcept(
false)
override {
115 if (!directoryExist(cacheDir))
116 makeDirectory(cacheDir);
118 std::string filename = getFilename(docName);
121 std::ofstream out(filename);
122 if (!out.good() || !(out.is_open()))
125 out << content.c_str();
126 if (!out.good() || !(out.is_open()))
135 bool evict(
const std::string& docName) {
139 return std::remove(f.c_str()) == 0;
147 std::vector<std::string> v;
151 : maxCache(maxFileNumber) {
156 virtual std::string
getDoc (
const std::string& hash_value)
override {
159 content = ca.
getDoc(hash_value);
160 updateLRU(hash_value);
165 virtual bool inCache(
const std::string& hash_value)
override {
172 virtual void putDoc(
const std::string& hash_value,
const std::string& content)
override {
174 ca.
putDoc(hash_value, content);
175 updateLRU(hash_value);
178 if (v.size() >= maxCache + 1) {
179 if (ca.
evict(v[v.size() - 1]))
189 void updateLRU(std::string hash_value) {
190 for (
auto it = v.begin(); it != v.end(); ) {
191 if (*it == hash_value) {
199 v.insert(v.begin(), hash_value);
205 string vector_string = ca.
getDoc(
"lru");
206 std::istringstream ss(vector_string);
209 while (std::getline(ss, token,
',')) {
226 out_vector = out_vector +
"," + s;
229 ca.
putDoc(
"lru", out_vector);
virtual void putDoc(const std::string &hash_value, const std::string &content) override
Definition: Cache.h:172
virtual std::string getDoc(const std::string &docName) noexcept(false) override
Definition: Cache.h:91
virtual void putDoc(const std::string &docName, const std::string &content) noexcept(false) override
Definition: Cache.h:113
SimpleCache()
Definition: Cache.h:67
virtual std::string getDoc(const std::string &hash_value) override
Definition: Cache.h:156
bool evict(const std::string &docName)
evicts a document from the cache
Definition: Cache.h:135
lruCache(int maxFileNumber=30)
Definition: Cache.h:150
virtual bool inCache(const std::string &docName) noexcept(false) override
Definition: Cache.h:82
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:165