From 95997d27106daf91336847f50efaaa32279b7fc7 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 16 Oct 2023 17:54:05 +0800 Subject: fix: check and update copyright at files --- src/core/function/CacheManager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index d9aead66..1794735a 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric * * This file is part of GpgFrontend. * @@ -20,7 +20,7 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric starting on May 12, 2021. + * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * -- cgit v1.2.3 From a23b2fbc707406dec0dd924c089b4285bc7f0010 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Wed, 25 Oct 2023 15:40:43 +0800 Subject: feat: use rt listen publish event function in main windows' app version upgrade notification --- src/core/function/CacheManager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 1794735a..27133957 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -36,8 +36,8 @@ #include "spdlog/spdlog.h" GpgFrontend::CacheManager::CacheManager(int channel) - : m_timer_(new QTimer(this)), - SingletonFunctionObject(channel) { + : SingletonFunctionObject(channel), + m_timer_(new QTimer(this)) { connect(m_timer_, &QTimer::timeout, this, &CacheManager::flush_cache_storage); m_timer_->start(15000); -- cgit v1.2.3 From 124929609eabff19359caad276a10f1026793c0f Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 25 Oct 2023 18:26:26 +0800 Subject: fix: solve some code tidy issues --- src/core/function/CacheManager.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 27133957..1641c3f8 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -33,7 +33,6 @@ #include #include "function/DataObjectOperator.h" -#include "spdlog/spdlog.h" GpgFrontend::CacheManager::CacheManager(int channel) : SingletonFunctionObject(channel), -- cgit v1.2.3 From fd46d4667611c0db9cea3f06205727399b6fb5fd Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 29 Oct 2023 02:46:15 +0800 Subject: refactor: start to tidy up code using clang-tidy --- src/core/function/CacheManager.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 1641c3f8..8445c648 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -30,7 +30,6 @@ #include #include -#include #include "function/DataObjectOperator.h" -- cgit v1.2.3 From 3ad7fecdb6458fdd6f146bed19fe643c7f93e905 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 7 Nov 2023 15:18:06 +0800 Subject: refactor: remove CommonUtils at core --- src/core/function/CacheManager.cpp | 286 ++++++++++++++++++++++++++----------- 1 file changed, 204 insertions(+), 82 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 8445c648..161f9a0c 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -30,116 +30,238 @@ #include #include +#include +#include #include "function/DataObjectOperator.h" -GpgFrontend::CacheManager::CacheManager(int channel) - : SingletonFunctionObject(channel), - m_timer_(new QTimer(this)) { - connect(m_timer_, &QTimer::timeout, this, &CacheManager::flush_cache_storage); - m_timer_->start(15000); +namespace GpgFrontend { - load_all_cache_storage(); -} +template +class ThreadSafeMap { + public: + using MapType = std::map; + using IteratorType = typename MapType::iterator; -void GpgFrontend::CacheManager::SaveCache(std::string key, - const nlohmann::json& value, - bool flush) { - auto data_object_key = get_data_object_key(key); - cache_storage_.insert(key, value); + void insert(const Key& key, const Value& value) { + std::unique_lock lock(mutex_); + map_[key] = value; + } - if (std::find(key_storage_.begin(), key_storage_.end(), key) == - key_storage_.end()) { - SPDLOG_DEBUG("register new key of cache", key); - key_storage_.push_back(key); + auto get(const Key& key) -> std::optional { + std::shared_lock lock(mutex_); + auto it = map_.find(key); + if (it != map_.end()) { + return it->second; + } + return std::nullopt; } - if (flush) { - flush_cache_storage(); + auto exists(const Key& key) -> bool { + std::shared_lock lock(mutex_); + return map_.count(key) > 0; } -} -nlohmann::json GpgFrontend::CacheManager::LoadCache(std::string key) { - auto data_object_key = get_data_object_key(key); + auto begin() -> IteratorType { return map_mirror_.begin(); } + + auto end() -> IteratorType { return map_mirror_.end(); } - if (!cache_storage_.exists(key)) { - cache_storage_.insert(key, load_cache_storage(key, {})); + auto mirror() -> ThreadSafeMap& { + std::shared_lock lock(mutex_); + map_mirror_ = map_; + return *this; } - auto cache = cache_storage_.get(key); - if (cache) - return *cache; - else - return {}; -} + auto remove(std::string key) -> bool { + std::unique_lock lock(mutex_); + auto it = map_.find(key); + if (it != map_.end()) { + map_.erase(it); + return true; + } + return false; + } + + private: + MapType map_mirror_; + MapType map_; + mutable std::shared_mutex mutex_; +}; -nlohmann::json GpgFrontend::CacheManager::LoadCache( - std::string key, nlohmann::json default_value) { - auto data_object_key = get_data_object_key(key); - if (!cache_storage_.exists(key)) { - cache_storage_.insert(key, load_cache_storage(key, default_value)); +class CacheManager::Impl : public SingletonFunctionObject { + public: + Impl(CacheManager* parent, int channel) + : SingletonFunctionObject(channel), + parent_(parent), + m_timer_(new QTimer(parent)) { + connect(m_timer_, &QTimer::timeout, parent_, + [this]() { flush_cache_storage(); }); + m_timer_->start(15000); + + load_all_cache_storage(); } - auto cache = cache_storage_.get(key); - if (cache) - return *cache; - else - return {}; -} + void SaveCache(std::string key, const nlohmann::json& value, bool flush) { + auto data_object_key = get_data_object_key(key); + cache_storage_.insert(key, value); -std::string GpgFrontend::CacheManager::get_data_object_key(std::string key) { - return (boost::format("__cache_data_%1%") % key).str(); -} + if (std::find(key_storage_.begin(), key_storage_.end(), key) == + key_storage_.end()) { + SPDLOG_DEBUG("register new key of cache", key); + key_storage_.push_back(key); + } + + if (flush) { + flush_cache_storage(); + } + } -nlohmann::json GpgFrontend::CacheManager::load_cache_storage( - std::string key, nlohmann::json default_value) { - auto data_object_key = get_data_object_key(key); - auto stored_data = - GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( - data_object_key); + auto LoadCache(const std::string& key) -> nlohmann::json { + auto data_object_key = get_data_object_key(key); - if (stored_data.has_value()) { - return stored_data.value(); - } else { - return default_value; + if (!cache_storage_.exists(key)) { + cache_storage_.insert(key, load_cache_storage(key, {})); + } + + auto cache = cache_storage_.get(key); + if (cache) { + return *cache; + } + return {}; } -} -void GpgFrontend::CacheManager::flush_cache_storage() { - for (auto cache : cache_storage_.mirror()) { - auto key = get_data_object_key(cache.first); - SPDLOG_DEBUG("save cache into filesystem, key {}, value size: {}", key, - cache.second.size()); - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, - cache.second); + auto LoadCache(const std::string& key, nlohmann::json default_value) + -> nlohmann::json { + auto data_object_key = get_data_object_key(key); + if (!cache_storage_.exists(key)) { + cache_storage_.insert(key, + load_cache_storage(key, std::move(default_value))); + } + + auto cache = cache_storage_.get(key); + if (cache) { + return *cache; + } + return {}; } - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(drk_key_, - key_storage_); -} -void GpgFrontend::CacheManager::register_cache_key(std::string key) {} + auto ResetCache(const std::string& key) -> bool { + auto data_object_key = get_data_object_key(key); + return cache_storage_.remove(key); + } -void GpgFrontend::CacheManager::load_all_cache_storage() { - SPDLOG_DEBUG("start to load all cache from file system"); - auto stored_data = - GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(drk_key_); + private: + CacheManager* parent_; + ThreadSafeMap cache_storage_; + nlohmann::json key_storage_; + QTimer* m_timer_; + const std::string drk_key_ = "__cache_manage_data_register_key_list"; - // get cache data list from file system - nlohmann::json registered_key_list; - if (stored_data.has_value()) { - registered_key_list = std::move(stored_data.value()); + /** + * @brief Get the data object key object + * + * @param key + * @return std::string + */ + static auto get_data_object_key(std::string key) -> std::string { + return (boost::format("__cache_data_%1%") % key).str(); } - if (!registered_key_list.is_array()) { - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( - drk_key_, nlohmann::json::array()); - SPDLOG_ERROR("drk_key_ is not an array, abort."); - return; + /** + * @brief + * + * @param key + * @param default_value + * @return nlohmann::json + */ + static auto load_cache_storage(std::string key, nlohmann::json default_value) + -> nlohmann::json { + auto data_object_key = get_data_object_key(std::move(key)); + auto stored_data = + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( + data_object_key); + + if (stored_data.has_value()) { + return stored_data.value(); + } + return default_value; } - for (auto key : registered_key_list) { - load_cache_storage(key, {}); + /** + * @brief + * + */ + void load_all_cache_storage() { + SPDLOG_DEBUG("start to load all cache from file system"); + auto stored_data = + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(drk_key_); + + // get cache data list from file system + nlohmann::json registered_key_list; + if (stored_data.has_value()) { + registered_key_list = std::move(stored_data.value()); + } + + if (!registered_key_list.is_array()) { + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( + drk_key_, nlohmann::json::array()); + SPDLOG_ERROR("drk_key_ is not an array, abort."); + return; + } + + for (const auto& key : registered_key_list) { + load_cache_storage(key, {}); + } + + key_storage_ = registered_key_list; + } + + /** + * @brief + * + */ + void flush_cache_storage() { + for (const auto& cache : cache_storage_.mirror()) { + auto key = get_data_object_key(cache.first); + SPDLOG_DEBUG("save cache into filesystem, key {}, value size: {}", key, + cache.second.size()); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, + cache.second); + } + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(drk_key_, + key_storage_); } - key_storage_ = registered_key_list; -} \ No newline at end of file + /** + * @brief + * + * @param key + */ + void register_cache_key(const std::string& key) {} +}; + +CacheManager::CacheManager(int channel) + : SingletonFunctionObject(channel), + p_(std::make_unique(this, channel)) {} + +CacheManager::~CacheManager() = default; + +void CacheManager::SaveCache(std::string key, const nlohmann::json& value, + bool flush) { + p_->SaveCache(std::move(key), value, flush); +} + +auto CacheManager::LoadCache(std::string key) -> nlohmann::json { + return p_->LoadCache(key); +} + +auto CacheManager::LoadCache(std::string key, nlohmann::json default_value) + -> nlohmann::json { + return p_->LoadCache(key, std::move(default_value)); +} + +auto CacheManager::ResetCache(std::string key) -> bool { + return p_->ResetCache(key); +} + +} // namespace GpgFrontend -- cgit v1.2.3 From 4dcd2ac8c4f673fc21c4cf0072d6cb648ca64e7e Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 7 Nov 2023 15:57:28 +0800 Subject: refactor: separate typedef and impl --- src/core/function/CacheManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 161f9a0c..a5eac78e 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -33,7 +33,7 @@ #include #include -#include "function/DataObjectOperator.h" +#include "core/function/DataObjectOperator.h" namespace GpgFrontend { -- cgit v1.2.3 From 883db05d54510e76b6548e107593187e1306117d Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 04:28:46 -0800 Subject: feat: general improvements of aync execution and memory security --- src/core/function/CacheManager.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index a5eac78e..c6926c89 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -34,6 +34,7 @@ #include #include "core/function/DataObjectOperator.h" +#include "core/utils/MemoryUtils.h" namespace GpgFrontend { @@ -45,13 +46,13 @@ class ThreadSafeMap { void insert(const Key& key, const Value& value) { std::unique_lock lock(mutex_); - map_[key] = value; + (*map_)[key] = value; } auto get(const Key& key) -> std::optional { std::shared_lock lock(mutex_); - auto it = map_.find(key); - if (it != map_.end()) { + auto it = map_->find(key); + if (it != map_->end()) { return it->second; } return std::nullopt; @@ -59,32 +60,34 @@ class ThreadSafeMap { auto exists(const Key& key) -> bool { std::shared_lock lock(mutex_); - return map_.count(key) > 0; + return map_->count(key) > 0; } - auto begin() -> IteratorType { return map_mirror_.begin(); } + auto begin() -> IteratorType { return map_mirror_->begin(); } - auto end() -> IteratorType { return map_mirror_.end(); } + auto end() -> IteratorType { return map_mirror_->end(); } auto mirror() -> ThreadSafeMap& { std::shared_lock lock(mutex_); - map_mirror_ = map_; + *map_mirror_ = *map_; return *this; } auto remove(std::string key) -> bool { std::unique_lock lock(mutex_); - auto it = map_.find(key); - if (it != map_.end()) { - map_.erase(it); + auto it = map_->find(key); + if (it != map_->end()) { + map_->erase(it); return true; } return false; } private: - MapType map_mirror_; - MapType map_; + std::unique_ptr> map_mirror_ = + std::move(SecureCreateUniqueObject()); + std::unique_ptr> map_ = + std::move(SecureCreateUniqueObject()); mutable std::shared_mutex mutex_; }; -- cgit v1.2.3 From 37215a895a649345165027971690dfdcd9106a32 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 21:53:03 -0800 Subject: fix: use secure memory management at impl class --- src/core/function/CacheManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index c6926c89..87fe5e7c 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -245,7 +245,7 @@ class CacheManager::Impl : public SingletonFunctionObject { CacheManager::CacheManager(int channel) : SingletonFunctionObject(channel), - p_(std::make_unique(this, channel)) {} + p_(SecureCreateUniqueObject(this, channel)) {} CacheManager::~CacheManager() = default; -- cgit v1.2.3 From 9f7593eab9b2d6f6fa6d76a303975b355b6ab458 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sat, 30 Dec 2023 19:55:20 +0800 Subject: feat: do not load entire data to memory in libarchive operations --- src/core/function/CacheManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 87fe5e7c..80d5aeb4 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -226,7 +226,7 @@ class CacheManager::Impl : public SingletonFunctionObject { void flush_cache_storage() { for (const auto& cache : cache_storage_.mirror()) { auto key = get_data_object_key(cache.first); - SPDLOG_DEBUG("save cache into filesystem, key {}, value size: {}", key, + SPDLOG_TRACE("save cache into filesystem, key {}, value size: {}", key, cache.second.size()); GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, cache.second); -- cgit v1.2.3 From 3c40fa27823e70215261d3845275360f85e59623 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 16:11:24 +0800 Subject: fix: slove some known issues --- src/core/function/CacheManager.cpp | 57 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 80d5aeb4..f9fa41ac 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -91,15 +91,13 @@ class ThreadSafeMap { mutable std::shared_mutex mutex_; }; -class CacheManager::Impl : public SingletonFunctionObject { +class CacheManager::Impl : public QObject { + Q_OBJECT public: - Impl(CacheManager* parent, int channel) - : SingletonFunctionObject(channel), - parent_(parent), - m_timer_(new QTimer(parent)) { - connect(m_timer_, &QTimer::timeout, parent_, - [this]() { flush_cache_storage(); }); - m_timer_->start(15000); + Impl() : flush_timer_(new QTimer(this)) { + connect(flush_timer_, &QTimer::timeout, this, + &Impl::slot_flush_cache_storage); + flush_timer_->start(15000); load_all_cache_storage(); } @@ -115,7 +113,7 @@ class CacheManager::Impl : public SingletonFunctionObject { } if (flush) { - flush_cache_storage(); + slot_flush_cache_storage(); } } @@ -153,11 +151,28 @@ class CacheManager::Impl : public SingletonFunctionObject { return cache_storage_.remove(key); } + private slots: + + /** + * @brief + * + */ + void slot_flush_cache_storage() { + for (const auto& cache : cache_storage_.mirror()) { + auto key = get_data_object_key(cache.first); + SPDLOG_TRACE("save cache into filesystem, key {}, value size: {}", key, + cache.second.size()); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, + cache.second); + } + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(drk_key_, + key_storage_); + } + private: - CacheManager* parent_; ThreadSafeMap cache_storage_; nlohmann::json key_storage_; - QTimer* m_timer_; + QTimer* flush_timer_; const std::string drk_key_ = "__cache_manage_data_register_key_list"; /** @@ -219,22 +234,6 @@ class CacheManager::Impl : public SingletonFunctionObject { key_storage_ = registered_key_list; } - /** - * @brief - * - */ - void flush_cache_storage() { - for (const auto& cache : cache_storage_.mirror()) { - auto key = get_data_object_key(cache.first); - SPDLOG_TRACE("save cache into filesystem, key {}, value size: {}", key, - cache.second.size()); - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, - cache.second); - } - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(drk_key_, - key_storage_); - } - /** * @brief * @@ -245,7 +244,7 @@ class CacheManager::Impl : public SingletonFunctionObject { CacheManager::CacheManager(int channel) : SingletonFunctionObject(channel), - p_(SecureCreateUniqueObject(this, channel)) {} + p_(SecureCreateUniqueObject()) {} CacheManager::~CacheManager() = default; @@ -268,3 +267,5 @@ auto CacheManager::ResetCache(std::string key) -> bool { } } // namespace GpgFrontend + +#include "CacheManager.moc" \ No newline at end of file -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/function/CacheManager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index f9fa41ac..cf1417fb 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -108,7 +108,7 @@ class CacheManager::Impl : public QObject { if (std::find(key_storage_.begin(), key_storage_.end(), key) == key_storage_.end()) { - SPDLOG_DEBUG("register new key of cache", key); + GF_CORE_LOG_DEBUG("register new key of cache", key); key_storage_.push_back(key); } @@ -160,8 +160,8 @@ class CacheManager::Impl : public QObject { void slot_flush_cache_storage() { for (const auto& cache : cache_storage_.mirror()) { auto key = get_data_object_key(cache.first); - SPDLOG_TRACE("save cache into filesystem, key {}, value size: {}", key, - cache.second.size()); + GF_CORE_LOG_TRACE("save cache into filesystem, key {}, value size: {}", + key, cache.second.size()); GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, cache.second); } @@ -210,7 +210,7 @@ class CacheManager::Impl : public QObject { * */ void load_all_cache_storage() { - SPDLOG_DEBUG("start to load all cache from file system"); + GF_CORE_LOG_DEBUG("start to load all cache from file system"); auto stored_data = GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(drk_key_); @@ -223,7 +223,7 @@ class CacheManager::Impl : public QObject { if (!registered_key_list.is_array()) { GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( drk_key_, nlohmann::json::array()); - SPDLOG_ERROR("drk_key_ is not an array, abort."); + GF_CORE_LOG_ERROR("drk_key_ is not an array, abort."); return; } -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/core/function/CacheManager.cpp | 41 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index cf1417fb..88d3bddd 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -29,7 +29,6 @@ #include "CacheManager.h" #include -#include #include #include @@ -73,7 +72,7 @@ class ThreadSafeMap { return *this; } - auto remove(std::string key) -> bool { + auto remove(QString key) -> bool { std::unique_lock lock(mutex_); auto it = map_->find(key); if (it != map_->end()) { @@ -102,14 +101,14 @@ class CacheManager::Impl : public QObject { load_all_cache_storage(); } - void SaveCache(std::string key, const nlohmann::json& value, bool flush) { + void SaveCache(QString key, const nlohmann::json& value, bool flush) { auto data_object_key = get_data_object_key(key); cache_storage_.insert(key, value); - if (std::find(key_storage_.begin(), key_storage_.end(), key) == - key_storage_.end()) { + if (std::find(key_storage_.begin(), key_storage_.end(), + key.toStdString()) == key_storage_.end()) { GF_CORE_LOG_DEBUG("register new key of cache", key); - key_storage_.push_back(key); + key_storage_.push_back(key.toStdString()); } if (flush) { @@ -117,7 +116,7 @@ class CacheManager::Impl : public QObject { } } - auto LoadCache(const std::string& key) -> nlohmann::json { + auto LoadCache(const QString& key) -> nlohmann::json { auto data_object_key = get_data_object_key(key); if (!cache_storage_.exists(key)) { @@ -131,7 +130,7 @@ class CacheManager::Impl : public QObject { return {}; } - auto LoadCache(const std::string& key, nlohmann::json default_value) + auto LoadCache(const QString& key, nlohmann::json default_value) -> nlohmann::json { auto data_object_key = get_data_object_key(key); if (!cache_storage_.exists(key)) { @@ -146,7 +145,7 @@ class CacheManager::Impl : public QObject { return {}; } - auto ResetCache(const std::string& key) -> bool { + auto ResetCache(const QString& key) -> bool { auto data_object_key = get_data_object_key(key); return cache_storage_.remove(key); } @@ -170,19 +169,19 @@ class CacheManager::Impl : public QObject { } private: - ThreadSafeMap cache_storage_; + ThreadSafeMap cache_storage_; nlohmann::json key_storage_; QTimer* flush_timer_; - const std::string drk_key_ = "__cache_manage_data_register_key_list"; + const QString drk_key_ = "__cache_manage_data_register_key_list"; /** * @brief Get the data object key object * * @param key - * @return std::string + * @return QString */ - static auto get_data_object_key(std::string key) -> std::string { - return (boost::format("__cache_data_%1%") % key).str(); + static auto get_data_object_key(QString key) -> QString { + return QString("__cache_data_%1").arg(key); } /** @@ -192,7 +191,7 @@ class CacheManager::Impl : public QObject { * @param default_value * @return nlohmann::json */ - static auto load_cache_storage(std::string key, nlohmann::json default_value) + static auto load_cache_storage(QString key, nlohmann::json default_value) -> nlohmann::json { auto data_object_key = get_data_object_key(std::move(key)); auto stored_data = @@ -228,7 +227,7 @@ class CacheManager::Impl : public QObject { } for (const auto& key : registered_key_list) { - load_cache_storage(key, {}); + load_cache_storage(QString::fromStdString(key), {}); } key_storage_ = registered_key_list; @@ -239,7 +238,7 @@ class CacheManager::Impl : public QObject { * * @param key */ - void register_cache_key(const std::string& key) {} + void register_cache_key(const QString& key) {} }; CacheManager::CacheManager(int channel) @@ -248,21 +247,21 @@ CacheManager::CacheManager(int channel) CacheManager::~CacheManager() = default; -void CacheManager::SaveCache(std::string key, const nlohmann::json& value, +void CacheManager::SaveCache(QString key, const nlohmann::json& value, bool flush) { p_->SaveCache(std::move(key), value, flush); } -auto CacheManager::LoadCache(std::string key) -> nlohmann::json { +auto CacheManager::LoadCache(QString key) -> nlohmann::json { return p_->LoadCache(key); } -auto CacheManager::LoadCache(std::string key, nlohmann::json default_value) +auto CacheManager::LoadCache(QString key, nlohmann::json default_value) -> nlohmann::json { return p_->LoadCache(key, std::move(default_value)); } -auto CacheManager::ResetCache(std::string key) -> bool { +auto CacheManager::ResetCache(QString key) -> bool { return p_->ResetCache(key); } -- cgit v1.2.3 From 6c632d70b391f8b317c68f7db8cfd217f9370995 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 15 Jan 2024 17:22:32 +0800 Subject: feat: use qt json support components in data object and infos gathering module --- src/core/function/CacheManager.cpp | 75 ++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 44 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 88d3bddd..90396017 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -98,25 +98,23 @@ class CacheManager::Impl : public QObject { &Impl::slot_flush_cache_storage); flush_timer_->start(15000); + // load data from storage load_all_cache_storage(); } - void SaveCache(QString key, const nlohmann::json& value, bool flush) { + void SaveCache(QString key, const QJsonDocument& value, bool flush) { auto data_object_key = get_data_object_key(key); cache_storage_.insert(key, value); - if (std::find(key_storage_.begin(), key_storage_.end(), - key.toStdString()) == key_storage_.end()) { + if (!key_storage_.contains(key)) { GF_CORE_LOG_DEBUG("register new key of cache", key); - key_storage_.push_back(key.toStdString()); + key_storage_.push_back(key); } - if (flush) { - slot_flush_cache_storage(); - } + if (flush) slot_flush_cache_storage(); } - auto LoadCache(const QString& key) -> nlohmann::json { + auto LoadCache(const QString& key) -> QJsonDocument { auto data_object_key = get_data_object_key(key); if (!cache_storage_.exists(key)) { @@ -124,14 +122,12 @@ class CacheManager::Impl : public QObject { } auto cache = cache_storage_.get(key); - if (cache) { - return *cache; - } + if (cache.has_value()) return cache.value(); return {}; } - auto LoadCache(const QString& key, nlohmann::json default_value) - -> nlohmann::json { + auto LoadCache(const QString& key, QJsonDocument default_value) + -> QJsonDocument { auto data_object_key = get_data_object_key(key); if (!cache_storage_.exists(key)) { cache_storage_.insert(key, @@ -139,9 +135,7 @@ class CacheManager::Impl : public QObject { } auto cache = cache_storage_.get(key); - if (cache) { - return *cache; - } + if (cache.has_value()) return cache.value(); return {}; } @@ -159,18 +153,17 @@ class CacheManager::Impl : public QObject { void slot_flush_cache_storage() { for (const auto& cache : cache_storage_.mirror()) { auto key = get_data_object_key(cache.first); - GF_CORE_LOG_TRACE("save cache into filesystem, key {}, value size: {}", - key, cache.second.size()); - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(key, - cache.second); + GF_CORE_LOG_TRACE("save cache into filesystem, key {}", key); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( + key, QJsonDocument(cache.second)); } - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(drk_key_, - key_storage_); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( + drk_key_, QJsonDocument(key_storage_)); } private: - ThreadSafeMap cache_storage_; - nlohmann::json key_storage_; + ThreadSafeMap cache_storage_; + QJsonArray key_storage_; QTimer* flush_timer_; const QString drk_key_ = "__cache_manage_data_register_key_list"; @@ -189,18 +182,16 @@ class CacheManager::Impl : public QObject { * * @param key * @param default_value - * @return nlohmann::json + * @return QJsonObject */ - static auto load_cache_storage(QString key, nlohmann::json default_value) - -> nlohmann::json { + static auto load_cache_storage(QString key, QJsonDocument default_value) + -> QJsonDocument { auto data_object_key = get_data_object_key(std::move(key)); auto stored_data = GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( data_object_key); - if (stored_data.has_value()) { - return stored_data.value(); - } + if (stored_data.has_value()) return stored_data.value(); return default_value; } @@ -214,20 +205,16 @@ class CacheManager::Impl : public QObject { GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(drk_key_); // get cache data list from file system - nlohmann::json registered_key_list; - if (stored_data.has_value()) { - registered_key_list = std::move(stored_data.value()); - } - - if (!registered_key_list.is_array()) { + QJsonArray registered_key_list; + if (stored_data->isArray()) { + registered_key_list = stored_data->array(); + } else { GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( - drk_key_, nlohmann::json::array()); - GF_CORE_LOG_ERROR("drk_key_ is not an array, abort."); - return; + drk_key_, QJsonDocument(QJsonArray())); } for (const auto& key : registered_key_list) { - load_cache_storage(QString::fromStdString(key), {}); + load_cache_storage(key.toString(), {}); } key_storage_ = registered_key_list; @@ -247,17 +234,17 @@ CacheManager::CacheManager(int channel) CacheManager::~CacheManager() = default; -void CacheManager::SaveCache(QString key, const nlohmann::json& value, +void CacheManager::SaveCache(QString key, const QJsonDocument& value, bool flush) { p_->SaveCache(std::move(key), value, flush); } -auto CacheManager::LoadCache(QString key) -> nlohmann::json { +auto CacheManager::LoadCache(QString key) -> QJsonDocument { return p_->LoadCache(key); } -auto CacheManager::LoadCache(QString key, nlohmann::json default_value) - -> nlohmann::json { +auto CacheManager::LoadCache(QString key, QJsonDocument default_value) + -> QJsonDocument { return p_->LoadCache(key, std::move(default_value)); } -- cgit v1.2.3 From 4994f4eaa1211d402b791660ad6221154a4c2405 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 16 Jan 2024 11:49:50 +0800 Subject: fix: make task and threading system safer --- src/core/function/CacheManager.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 90396017..58adede4 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -30,7 +30,6 @@ #include #include -#include #include "core/function/DataObjectOperator.h" #include "core/utils/MemoryUtils.h" -- cgit v1.2.3 From e352e8e6b8d03a24ef5d52eef3e4d370807b5bbd Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 17 Jan 2024 19:39:47 +0800 Subject: fix: find and slove some bugs --- src/core/function/CacheManager.cpp | 86 +++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 29 deletions(-) (limited to 'src/core/function/CacheManager.cpp') diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp index 58adede4..719c962d 100644 --- a/src/core/function/CacheManager.cpp +++ b/src/core/function/CacheManager.cpp @@ -30,6 +30,7 @@ #include #include +#include #include "core/function/DataObjectOperator.h" #include "core/utils/MemoryUtils.h" @@ -101,9 +102,9 @@ class CacheManager::Impl : public QObject { load_all_cache_storage(); } - void SaveCache(QString key, const QJsonDocument& value, bool flush) { + void SaveDurableCache(QString key, const QJsonDocument& value, bool flush) { auto data_object_key = get_data_object_key(key); - cache_storage_.insert(key, value); + durable_cache_storage_.insert(key, value); if (!key_storage_.contains(key)) { GF_CORE_LOG_DEBUG("register new key of cache", key); @@ -113,36 +114,50 @@ class CacheManager::Impl : public QObject { if (flush) slot_flush_cache_storage(); } - auto LoadCache(const QString& key) -> QJsonDocument { + auto LoadDurableCache(const QString& key) -> QJsonDocument { auto data_object_key = get_data_object_key(key); - if (!cache_storage_.exists(key)) { - cache_storage_.insert(key, load_cache_storage(key, {})); + if (!durable_cache_storage_.exists(key)) { + durable_cache_storage_.insert(key, load_cache_storage(key, {})); } - auto cache = cache_storage_.get(key); + auto cache = durable_cache_storage_.get(key); if (cache.has_value()) return cache.value(); return {}; } - auto LoadCache(const QString& key, QJsonDocument default_value) + auto LoadDurableCache(const QString& key, QJsonDocument default_value) -> QJsonDocument { auto data_object_key = get_data_object_key(key); - if (!cache_storage_.exists(key)) { - cache_storage_.insert(key, - load_cache_storage(key, std::move(default_value))); + if (!durable_cache_storage_.exists(key)) { + durable_cache_storage_.insert( + key, load_cache_storage(key, std::move(default_value))); } - auto cache = cache_storage_.get(key); + auto cache = durable_cache_storage_.get(key); if (cache.has_value()) return cache.value(); return {}; } - auto ResetCache(const QString& key) -> bool { + auto ResetDurableCache(const QString& key) -> bool { auto data_object_key = get_data_object_key(key); - return cache_storage_.remove(key); + return durable_cache_storage_.remove(key); } + void FlushCacheStorage() { this->slot_flush_cache_storage(); } + + void SaveCache(const QString& key, QString value) { + runtime_cache_storage_.insert(key, new QString(std::move(value))); + } + + auto LoadCache(const QString& key) -> QString { + auto* value = runtime_cache_storage_.object(key); + if (value == nullptr) return {}; + return *value; + } + + void ResetCache(const QString& key) { runtime_cache_storage_.remove(key); } + private slots: /** @@ -150,7 +165,7 @@ class CacheManager::Impl : public QObject { * */ void slot_flush_cache_storage() { - for (const auto& cache : cache_storage_.mirror()) { + for (const auto& cache : durable_cache_storage_.mirror()) { auto key = get_data_object_key(cache.first); GF_CORE_LOG_TRACE("save cache into filesystem, key {}", key); GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( @@ -161,7 +176,8 @@ class CacheManager::Impl : public QObject { } private: - ThreadSafeMap cache_storage_; + QCache runtime_cache_storage_; + ThreadSafeMap durable_cache_storage_; QJsonArray key_storage_; QTimer* flush_timer_; const QString drk_key_ = "__cache_manage_data_register_key_list"; @@ -172,7 +188,7 @@ class CacheManager::Impl : public QObject { * @param key * @return QString */ - static auto get_data_object_key(QString key) -> QString { + static auto get_data_object_key(const QString& key) -> QString { return QString("__cache_data_%1").arg(key); } @@ -183,9 +199,9 @@ class CacheManager::Impl : public QObject { * @param default_value * @return QJsonObject */ - static auto load_cache_storage(QString key, QJsonDocument default_value) - -> QJsonDocument { - auto data_object_key = get_data_object_key(std::move(key)); + static auto load_cache_storage(const QString& key, + QJsonDocument default_value) -> QJsonDocument { + auto data_object_key = get_data_object_key(key); auto stored_data = GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( data_object_key); @@ -231,26 +247,38 @@ CacheManager::CacheManager(int channel) : SingletonFunctionObject(channel), p_(SecureCreateUniqueObject()) {} -CacheManager::~CacheManager() = default; +CacheManager::~CacheManager() { p_->FlushCacheStorage(); } -void CacheManager::SaveCache(QString key, const QJsonDocument& value, - bool flush) { - p_->SaveCache(std::move(key), value, flush); +void CacheManager::SaveDurableCache(const QString& key, + const QJsonDocument& value, bool flush) { + p_->SaveDurableCache(key, value, flush); } -auto CacheManager::LoadCache(QString key) -> QJsonDocument { - return p_->LoadCache(key); +auto CacheManager::LoadDurableCache(const QString& key) -> QJsonDocument { + return p_->LoadDurableCache(key); } -auto CacheManager::LoadCache(QString key, QJsonDocument default_value) +auto CacheManager::LoadDurableCache(const QString& key, + QJsonDocument default_value) -> QJsonDocument { - return p_->LoadCache(key, std::move(default_value)); + return p_->LoadDurableCache(key, std::move(default_value)); } -auto CacheManager::ResetCache(QString key) -> bool { - return p_->ResetCache(key); +auto CacheManager::ResetDurableCache(const QString& key) -> bool { + return p_->ResetDurableCache(key); +} + +void CacheManager::SaveCache(const QString& key, QString value) { + p_->SaveCache(key, std::move(value)); +} + +auto CacheManager::LoadCache(const QString& key) -> QString { + return p_->LoadCache(key); } +void CacheManager::ResetCache(const QString& key) { + return p_->ResetCache(key); +} } // namespace GpgFrontend #include "CacheManager.moc" \ No newline at end of file -- cgit v1.2.3