abaddon/filecache.hpp

35 lines
868 B
C++
Raw Normal View History

2020-09-10 22:28:42 +00:00
#pragma once
#include <functional>
#include <string>
#include <filesystem>
#include <vector>
#include <unordered_map>
#include <future>
#include "util.hpp"
#include "http.hpp"
2020-09-10 22:28:42 +00:00
class Cache {
public:
Cache();
~Cache();
using callback_type = std::function<void(std::string)>;
void GetFileFromURL(std::string url, callback_type cb);
2020-09-30 04:12:38 +00:00
std::string GetPathIfCached(std::string url);
2021-01-20 07:26:04 +00:00
void ClearCache();
2020-09-10 22:28:42 +00:00
private:
2020-12-11 05:12:43 +00:00
std::string GetCachedName(std::string str);
2020-09-10 22:28:42 +00:00
void CleanupFutures();
void RespondFromPath(std::filesystem::path path, callback_type cb);
void OnResponse(const http::response_type &r);
2020-09-10 22:28:42 +00:00
std::unique_ptr<Semaphore> m_semaphore;
2020-09-10 22:28:42 +00:00
std::unordered_map<std::string, std::vector<callback_type>> m_callbacks;
std::vector<std::future<void>> m_futures;
std::filesystem::path m_tmp_path;
2020-10-27 06:29:02 +00:00
bool m_canceled = false;
2020-09-10 22:28:42 +00:00
};