#include "cache.h" #include "indexer.h" #include namespace { std::string GetCachedFileName(const std::string& cache_directory, std::string source_file) { assert(!cache_directory.empty()); std::replace(source_file.begin(), source_file.end(), '\\', '_'); std::replace(source_file.begin(), source_file.end(), '/', '_'); std::replace(source_file.begin(), source_file.end(), ':', '_'); std::replace(source_file.begin(), source_file.end(), '.', '_'); return cache_directory + source_file + ".json"; } } // namespace std::unique_ptr LoadCachedFile(IndexerConfig* config, const std::string& filename) { if (!config->enableCacheRead) return nullptr; optional file_content = ReadContent(GetCachedFileName(config->cacheDirectory, filename)); if (!file_content) return nullptr; optional indexed = Deserialize(filename, *file_content); if (indexed) return MakeUnique(indexed.value()); return nullptr; } void WriteToCache(IndexerConfig* config, const std::string& filename, IndexedFile& file) { if (!config->enableCacheWrite) return; std::string indexed_content = Serialize(file); std::ofstream cache; cache.open(GetCachedFileName(config->cacheDirectory, filename)); assert(cache.good()); cache << indexed_content; cache.close(); }