ClickHouse/src/Common/FileCacheFactory.cpp

56 lines
1.2 KiB
C++
Raw Normal View History

2022-01-13 11:57:56 +00:00
#include "FileCacheFactory.h"
#include "FileCache.h"
namespace DB
{
2022-01-22 22:56:24 +00:00
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
2022-01-13 11:57:56 +00:00
FileCacheFactory & FileCacheFactory::instance()
{
static FileCacheFactory ret;
return ret;
}
FileCacheFactory::CacheByBasePath FileCacheFactory::getAll()
{
std::lock_guard lock(mutex);
return caches;
}
2022-01-13 11:57:56 +00:00
FileCachePtr FileCacheFactory::getImpl(const std::string & cache_base_path, std::lock_guard<std::mutex> &)
{
auto it = caches.find(cache_base_path);
if (it == caches.end())
return nullptr;
return it->second;
}
FileCachePtr FileCacheFactory::get(const std::string & cache_base_path)
{
std::lock_guard lock(mutex);
auto cache = getImpl(cache_base_path, lock);
if (cache)
return cache;
throw Exception(ErrorCodes::BAD_ARGUMENTS, "No cache found by path: {}", cache_base_path);
}
FileCachePtr FileCacheFactory::getOrCreate(
const std::string & cache_base_path, const FileCacheSettings & file_cache_settings)
2022-01-13 11:57:56 +00:00
{
std::lock_guard lock(mutex);
auto cache = getImpl(cache_base_path, lock);
if (cache)
return cache;
cache = std::make_shared<LRUFileCache>(cache_base_path, file_cache_settings);
2022-01-13 11:57:56 +00:00
caches.emplace(cache_base_path, cache);
return cache;
}
}