diff options
author | saturneric <[email protected]> | 2023-11-07 07:18:06 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-11-07 07:18:06 +0000 |
commit | 3ad7fecdb6458fdd6f146bed19fe643c7f93e905 (patch) | |
tree | 522f7a5dd0389ad0771d01a50ea49ef646940894 /src/core/function/CacheManager.h | |
parent | refactor: improve the code structure of core (diff) | |
download | GpgFrontend-3ad7fecdb6458fdd6f146bed19fe643c7f93e905.tar.gz GpgFrontend-3ad7fecdb6458fdd6f146bed19fe643c7f93e905.zip |
refactor: remove CommonUtils at core
Diffstat (limited to 'src/core/function/CacheManager.h')
-rw-r--r-- | src/core/function/CacheManager.h | 72 |
1 files changed, 11 insertions, 61 deletions
diff --git a/src/core/function/CacheManager.h b/src/core/function/CacheManager.h index 2126decb..49db494e 100644 --- a/src/core/function/CacheManager.h +++ b/src/core/function/CacheManager.h @@ -29,84 +29,34 @@ #pragma once #include <nlohmann/json.hpp> -#include <optional> -#include <shared_mutex> #include "core/function/basic/GpgFunctionObject.h" namespace GpgFrontend { -template <typename Key, typename Value> -class ThreadSafeMap { - public: - using MapType = std::map<Key, Value>; - using IteratorType = typename MapType::iterator; - - void insert(const Key& key, const Value& value) { - std::unique_lock lock(mutex_); - map_[key] = value; - } - - std::optional<Value> get(const Key& key) { - std::shared_lock lock(mutex_); - auto it = map_.find(key); - if (it != map_.end()) { - return it->second; - } - return std::nullopt; - } - - bool exists(const Key& key) { - std::shared_lock lock(mutex_); - return map_.count(key) > 0; - } - - IteratorType begin() { return map_mirror_.begin(); } - - IteratorType end() { return map_mirror_.end(); } - - ThreadSafeMap& mirror() { - std::shared_lock lock(mutex_); - map_mirror_ = map_; - return *this; - } - - private: - MapType map_mirror_; - MapType map_; - mutable std::shared_mutex mutex_; -}; - class GPGFRONTEND_CORE_EXPORT CacheManager : public QObject, public SingletonFunctionObject<CacheManager> { Q_OBJECT public: - CacheManager(int channel = SingletonFunctionObject::GetDefaultChannel()); + explicit CacheManager( + int channel = SingletonFunctionObject::GetDefaultChannel()); + + ~CacheManager() override; void SaveCache(std::string key, const nlohmann::json& value, bool flush = false); - nlohmann::json LoadCache(std::string key); + auto LoadCache(std::string key) -> nlohmann::json; - nlohmann::json LoadCache(std::string key, nlohmann::json default_value); - - private: - std::string get_data_object_key(std::string key); + auto LoadCache(std::string key, nlohmann::json default_value) + -> nlohmann::json; - nlohmann::json load_cache_storage(std::string key, - nlohmann::json default_value); + auto ResetCache(std::string key) -> bool; - void load_all_cache_storage(); - - void flush_cache_storage(); - - void register_cache_key(std::string key); - - ThreadSafeMap<std::string, nlohmann::json> cache_storage_; - nlohmann::json key_storage_; - QTimer* m_timer_; - const std::string drk_key_ = "__cache_manage_data_register_key_list"; + private: + class Impl; + std::unique_ptr<Impl> p_; }; } // namespace GpgFrontend |