diff --git a/repertory/librepertory/src/file_manager/cache_size_mgr.cpp b/repertory/librepertory/src/file_manager/cache_size_mgr.cpp index d644c106..974004a0 100644 --- a/repertory/librepertory/src/file_manager/cache_size_mgr.cpp +++ b/repertory/librepertory/src/file_manager/cache_size_mgr.cpp @@ -42,32 +42,40 @@ E_SIMPLE2(max_cache_size_reached, warn, true, cache_size_mgr cache_size_mgr::instance_{}; -// TODO add timeout auto cache_size_mgr::expand(std::uint64_t size) -> api_error { - if (size == 0U) { - return api_error::success; - } - unique_mutex_lock lock(mtx_); + if (cfg_ == nullptr) { + notify_.notify_all(); return api_error::cache_not_initialized; } + if (size == 0U) { + notify_.notify_all(); + return api_error::success; + } + + auto last_cache_size{cache_size_}; cache_size_ += size; - auto max_cache_size = cfg_->get_max_cache_size_bytes(); + auto max_cache_size{cfg_->get_max_cache_size_bytes()}; - auto cache_dir = utils::file::directory{cfg_->get_cache_directory()}; + auto cache_dir{ + utils::file::directory{cfg_->get_cache_directory()}, + }; while (not get_stop_requested() && cache_size_ > max_cache_size && cache_dir.count() > 1U) { - event_system::instance().raise(cache_size_, - max_cache_size); - notify_.wait(lock); + if (last_cache_size != cache_size_) { + event_system::instance().raise(cache_size_, + max_cache_size); + last_cache_size = cache_size_; + } + notify_.wait(lock, 5s); } notify_.notify_all(); - return api_error::success; + return get_stop_requested() ? api_error::error : api_error::success; } auto cache_size_mgr::get_stop_requested() const -> bool { @@ -84,7 +92,9 @@ void cache_size_mgr::initialize(app_config *cfg) { stop_requested_ = false; - auto cache_dir = utils::file::directory{cfg_->get_cache_directory()}; + auto cache_dir{ + utils::file::directory{cfg_->get_cache_directory()}, + }; if (not cache_dir.create_directory()) { throw startup_exception(fmt::format("failed to create cache directory|{}", cache_dir.get_path()));