From 57d98845104d3b3b475ab4e65d4e6e0727dd3a43 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 21 Jan 2025 13:46:51 -0600 Subject: [PATCH] refactor --- .../src/file_manager/cache_size_mgr.cpp | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) 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()));