2017-12-29 17:27:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <optional.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-01-07 00:20:37 +00:00
|
|
|
#include <unordered_map>
|
2017-12-29 18:28:11 +00:00
|
|
|
#include <vector>
|
2017-12-29 17:27:56 +00:00
|
|
|
|
|
|
|
struct Config;
|
|
|
|
struct IndexFile;
|
|
|
|
|
|
|
|
struct ICacheManager {
|
|
|
|
struct FakeCacheEntry {
|
|
|
|
std::string path;
|
|
|
|
std::string content;
|
|
|
|
std::string json;
|
|
|
|
};
|
|
|
|
|
2018-01-30 05:34:28 +00:00
|
|
|
static std::shared_ptr<ICacheManager> Make(Config* config);
|
|
|
|
static std::shared_ptr<ICacheManager> MakeFake(
|
2017-12-29 17:27:56 +00:00
|
|
|
const std::vector<FakeCacheEntry>& entries);
|
|
|
|
|
|
|
|
virtual ~ICacheManager();
|
|
|
|
|
|
|
|
// Tries to load a cache for |path|, returning null if there is none. The
|
|
|
|
// cache loader still owns the cache.
|
2018-01-07 00:20:37 +00:00
|
|
|
IndexFile* TryLoad(const std::string& path);
|
2017-12-29 17:27:56 +00:00
|
|
|
|
|
|
|
// Takes the existing cache or loads the cache at |path|. May return null if
|
|
|
|
// the cache does not exist.
|
2018-01-07 00:20:37 +00:00
|
|
|
std::unique_ptr<IndexFile> TryTakeOrLoad(const std::string& path);
|
2017-12-29 17:27:56 +00:00
|
|
|
|
|
|
|
// Takes the existing cache or loads the cache at |path|. Asserts the cache
|
|
|
|
// exists.
|
|
|
|
std::unique_ptr<IndexFile> TakeOrLoad(const std::string& path);
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
virtual void WriteToCache(IndexFile& file) = 0;
|
|
|
|
|
2017-12-29 18:19:39 +00:00
|
|
|
virtual optional<std::string> LoadCachedFileContents(
|
2018-01-07 00:20:37 +00:00
|
|
|
const std::string& path) = 0;
|
2017-12-29 18:19:39 +00:00
|
|
|
|
2017-12-29 17:27:56 +00:00
|
|
|
// Iterate over all loaded caches.
|
2018-01-07 00:20:37 +00:00
|
|
|
void IterateLoadedCaches(std::function<void(IndexFile*)> fn);
|
2017-12-29 17:27:56 +00:00
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
protected:
|
|
|
|
virtual std::unique_ptr<IndexFile> RawCacheLoad(const std::string& path) = 0;
|
|
|
|
std::unordered_map<std::string, std::unique_ptr<IndexFile>> caches_;
|
|
|
|
};
|