GpgFrontend Project
A Free, Powerful, Easy-to-Use, Compact, Cross-Platform, and Installation-Free OpenPGP(pgp) Crypto Tool.
CacheManager.h
1 
29 #ifndef GPGFRONTEND_CACHEMANAGER_H
30 #define GPGFRONTEND_CACHEMANAGER_H
31 
32 #include <string>
33 
34 #include "core/GpgFunctionObject.h"
35 
36 namespace GpgFrontend {
37 
38 template <typename Key, typename Value>
40  public:
41  using MapType = std::map<Key, Value>;
42  using IteratorType = typename MapType::iterator;
43 
44  void insert(const Key& key, const Value& value) {
45  std::unique_lock lock(mutex_);
46  map_[key] = value;
47  }
48 
49  std::optional<Value> get(const Key& key) {
50  std::shared_lock lock(mutex_);
51  auto it = map_.find(key);
52  if (it != map_.end()) {
53  return it->second;
54  }
55  return std::nullopt;
56  }
57 
58  bool exists(const Key& key) {
59  std::shared_lock lock(mutex_);
60  return map_.count(key) > 0;
61  }
62 
63  IteratorType begin() { return map_mirror_.begin(); }
64 
65  IteratorType end() { return map_mirror_.end(); }
66 
67  ThreadSafeMap& mirror() {
68  std::shared_lock lock(mutex_);
69  map_mirror_ = map_;
70  return *this;
71  }
72 
73  private:
74  MapType map_mirror_;
75  MapType map_;
76  mutable std::shared_mutex mutex_;
77 };
78 
79 class GPGFRONTEND_CORE_EXPORT CacheManager
80  : public QObject,
81  public SingletonFunctionObject<CacheManager> {
82  Q_OBJECT
83  public:
85 
86  void SaveCache(std::string key, const nlohmann::json& value,
87  bool flush = false);
88 
89  nlohmann::json LoadCache(std::string key);
90 
91  nlohmann::json LoadCache(std::string key, nlohmann::json default_value);
92 
93  private:
94  std::string get_data_object_key(std::string key);
95 
96  nlohmann::json load_cache_storage(std::string key,
97  nlohmann::json default_value);
98 
99  void load_all_cache_storage();
100 
101  void flush_cache_storage();
102 
103  void register_cache_key(std::string key);
104 
106  nlohmann::json key_storage_;
107  QTimer* m_timer_;
108  const std::string drk_key_ = "__cache_manage_data_register_key_list";
109 };
110 
111 } // namespace GpgFrontend
112 
113 #endif
Definition: CacheManager.h:81
Definition: GpgFunctionObject.h:150
static int GetDefaultChannel()
Get the Default Channel object.
Definition: GpgFunctionObject.h:251
Definition: CacheManager.h:39
Definition: CoreCommonUtil.cpp:31