44 Cachier(
const std::string &cache_store_path,
45 bool ensure_cache_store_path =
true)
46 : cache_store_path(cache_store_path) {
49 if (ensure_cache_store_path) {
50 createDir(cache_store_path);
55 if (!(stat(cache_store_path.c_str(), &dir_stat) == 0 &&
56 S_ISDIR(dir_stat.st_mode) &&
57 access(cache_store_path.c_str(), W_OK) == 0)) {
59 <<
"Warning: Cache store path is not valid or not writeable, caching "
60 "feature will not work."
82 const std::string &content =
"",
84 DONT_OVERWRITE_CACHE) {
91 if (hash_result.error !=
"") {
92 return {0, hash_result.error};
96 if (cache_overwrite_option == DONT_OVERWRITE_CACHE &&
98 auto error =
"Error: cache exists, not over-writing it.";
104 createCacheFile(std::to_string(hash_result.key), content);
106 HashResult error = {0,
"Error: unable to create cache file."};
107 return cache_created ? hash_result : error;
119 return fileExists(cache_store_path +
120 std::filesystem::path::preferred_separator +
121 std::to_string(key));
142 return fileExists(cache_store_path +
143 std::filesystem::path::preferred_separator +
144 std::to_string(hash_result.key));
154 initialization_checked =
true;
172 struct stat fileStat;
174 if (stat(filename.c_str(), &fileStat) != 0 &&
175 !std::filesystem::is_regular_file(filename)) {
176 auto error =
"Error: " + filename +
" is not a valid file.";
181 size_t fileSize = fileStat.st_size;
182 time_t fileTime = fileStat.st_mtime;
185 std::ifstream file(filename, std::ios::binary);
186 std::vector<char> buffer(8);
187 file.read(buffer.data(), 8);
190 std::string fileHeader(buffer.begin(), buffer.end());
191 std::stringstream ss;
192 ss << fileSize << fileTime << fileHeader;
193 std::string fileString = filename + ss.str();
194 std::size_t fileHash = std::hash<std::string>{}(fileString);
196 return {fileHash,
""};
207 auto target_cache_file_path =
208 cache_store_path + std::filesystem::path::preferred_separator + key;
209 return read_file_content(target_cache_file_path);
213 std::string cache_store_path;
215 bool initialized =
false;
217 bool initialization_checked =
false;
224 std::size_t stringToSize_t(std::string str) {
225 std::stringstream sstream(str);
237 bool createCacheFile(
const std::string &filename,
238 const std::string &content =
"") {
239 std::string file_path = cache_store_path +
240 std::filesystem::path::preferred_separator +
243 std::ofstream file(file_path);
259 bool fileExists(
const std::string &path) {
260 return std::filesystem::exists(path);
270 bool createDir(
const std::string &path) {
271 if (!std::filesystem::exists(path))
272 return std::filesystem::create_directories(path);
285 std::string read_file_content(
const std::string &filename) {
286 std::ifstream file(filename);
287 if (!file.is_open()) {
291 std::string content((std::istreambuf_iterator<char>(file)),
292 (std::istreambuf_iterator<char>()));
302 if (initialization_checked ==
false) {
303 throw std::runtime_error(
"Initialization checks were not performed, did "
304 "you forgot calling Cachier::isInitialized "
305 "before using the library?");
The Cachier class.
Definition: cachier.h:20
bool cacheExists(const std::size_t &key)
cacheExists
Definition: cachier.h:117
Cachier(const std::string &cache_store_path, bool ensure_cache_store_path=true)
Cachier.
Definition: cachier.h:44
std::string getContent(const std::string &key)
getContent
Definition: cachier.h:206
CacheOverwriteOption
The CacheOverwriteOption enum.
Definition: cachier.h:25
HashResult addCache(const std::string &filename, const std::string &content="", const CacheOverwriteOption &cache_overwrite_option=DONT_OVERWRITE_CACHE)
addCache
Definition: cachier.h:81
bool isInitialized()
isInitialized
Definition: cachier.h:153
HashResult computeHash(const std::string &filename)
computeHash
Definition: cachier.h:170
bool cacheExists(const std::string &filename)
cacheExists
Definition: cachier.h:134
The HashResult class.
Definition: cachier.h:31