Skip to content

Instantly share code, notes, and snippets.

@kofigumbs
Created May 1, 2021 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kofigumbs/c37349b936daeed3b0dcb478cfbbb9bf to your computer and use it in GitHub Desktop.
Save kofigumbs/c37349b936daeed3b0dcb478cfbbb9bf to your computer and use it in GitHub Desktop.
struct CompilerCache: soul::patch::RefCountHelper<soul::patch::CompilerCache, CompilerCache> {
std::mutex mutex;
std::filesystem::path path;
CompilerCache(std::filesystem::path path): path(path) {
std::filesystem::create_directory(path);
}
void storeItemInCache(const char* key, const void* source, uint64_t sourceSize) override {
std::scoped_lock lock(mutex);
std::ofstream file(this->path / key, std::ifstream::out | std::ifstream::binary);
file.write((char*) source, sourceSize);
}
uint64_t readItemFromCache(const char* key, void* destination, uint64_t destinationSize) override {
std::scoped_lock lock(mutex);
std::ifstream file(this->path / key, std::ifstream::in | std::ifstream::binary);
if (!file.is_open())
return 0;
file.seekg(0, file.end);
uint64_t fileSize = file.tellg();
if (fileSize == 0)
return 0;
if (destination == nullptr || destinationSize < fileSize)
return fileSize;
file.seekg(0, file.beg);
file.read((char*) destination, destinationSize);
return file.peek() == EOF ? fileSize : 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment