aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
m---------modules0
-rw-r--r--src/core/function/CacheManager.cpp46
-rw-r--r--src/core/function/CacheManager.h2
-rw-r--r--src/sdk/GFSDKBasic.cpp22
-rw-r--r--src/sdk/GFSDKBasic.h33
-rw-r--r--src/test/core/GpgCoreTestCache.cpp54
6 files changed, 147 insertions, 10 deletions
diff --git a/modules b/modules
-Subproject 405522f3b09913544bbec43a802d4e13ccd84f1
+Subproject e76d2abcf93a2299382983cb3b0b515eb75a144
diff --git a/src/core/function/CacheManager.cpp b/src/core/function/CacheManager.cpp
index aed3a517..d54d0fac 100644
--- a/src/core/function/CacheManager.cpp
+++ b/src/core/function/CacheManager.cpp
@@ -145,14 +145,38 @@ class CacheManager::Impl : public QObject {
void FlushCacheStorage() { this->slot_flush_cache_storage(); }
- void SaveCache(const QString& key, QString value) {
- runtime_cache_storage_.insert(key, new QString(std::move(value)));
+ void SaveCache(const QString& key, QString value, qint64 ttl) {
+ LOG_D() << "save cache, key: " << key << "ttl: " << ttl;
+ runtime_cache_storage_.insert(
+ key, new CacheObject(
+ std::move(value),
+ ttl < 0 ? -1 : QDateTime::currentSecsSinceEpoch() + ttl));
}
auto LoadCache(const QString& key) -> QString {
+ if (!runtime_cache_storage_.contains(key)) return {};
+ LOG_D() << "hit cache, key: " << key;
+
auto* value = runtime_cache_storage_.object(key);
- if (value == nullptr) return {};
- return *value;
+ if (value == nullptr) {
+ LOG_E() << "hit cache but got nullptr by value, key" << key;
+ return {};
+ }
+
+ if (value->ttl < 0) {
+ return value->value;
+ }
+
+ // deal with expiration
+ auto current_timestamp = QDateTime::currentSecsSinceEpoch();
+ if (current_timestamp > value->ttl) {
+ LOG_D() << "hit cache but expired, key: " << key
+ << "expiration timestamp:" << value->ttl;
+ ResetCache(key);
+ return {};
+ }
+
+ return value->value;
}
void ResetCache(const QString& key) { runtime_cache_storage_.remove(key); }
@@ -176,7 +200,15 @@ class CacheManager::Impl : public QObject {
}
private:
- QCache<QString, QString> runtime_cache_storage_;
+ struct CacheObject {
+ QString value;
+ qint64 ttl;
+
+ CacheObject(QString value, qint64 ttl)
+ : value(std::move(value)), ttl(ttl) {}
+ };
+
+ QCache<QString, CacheObject> runtime_cache_storage_;
ThreadSafeMap<QString, QJsonDocument> durable_cache_storage_;
QJsonArray key_storage_;
QTimer* flush_timer_;
@@ -267,8 +299,8 @@ 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));
+void CacheManager::SaveCache(const QString& key, QString value, qint64 ttl) {
+ p_->SaveCache(key, std::move(value), ttl);
}
auto CacheManager::LoadCache(const QString& key) -> QString {
diff --git a/src/core/function/CacheManager.h b/src/core/function/CacheManager.h
index 634f7cb5..6a466b60 100644
--- a/src/core/function/CacheManager.h
+++ b/src/core/function/CacheManager.h
@@ -55,7 +55,7 @@ class GPGFRONTEND_CORE_EXPORT CacheManager
* @param key
* @param value
*/
- void SaveCache(const QString& key, QString value);
+ void SaveCache(const QString& key, QString value, qint64 ttl = -1);
/**
* @brief
diff --git a/src/sdk/GFSDKBasic.cpp b/src/sdk/GFSDKBasic.cpp
index 32076cd0..0bc190a9 100644
--- a/src/sdk/GFSDKBasic.cpp
+++ b/src/sdk/GFSDKBasic.cpp
@@ -28,6 +28,7 @@
#include "GFSDKBasic.h"
+#include "core/function/CacheManager.h"
#include "core/function/SecureMemoryAllocator.h"
#include "core/function/gpg/GpgCommandExecutor.h"
#include "core/utils/BuildInfoUtils.h"
@@ -117,3 +118,24 @@ auto GFAppRegisterTranslatorReader(const char* id,
? 0
: -1;
}
+
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheSave(const char* key,
+ const char* value) -> int {
+ GpgFrontend::CacheManager::GetInstance().SaveCache(GFUnStrDup(key),
+ GFUnStrDup(value));
+ return 0;
+}
+
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheGet(const char* key) -> const char* {
+ auto value =
+ GpgFrontend::CacheManager::GetInstance().LoadCache(GFUnStrDup(key));
+ return GFStrDup(value);
+}
+
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheSaveWithTTL(const char* key,
+ const char* value,
+ int ttl) -> int {
+ GpgFrontend::CacheManager::GetInstance().SaveCache(GFUnStrDup(key),
+ GFUnStrDup(value), ttl);
+ return 0;
+}
diff --git a/src/sdk/GFSDKBasic.h b/src/sdk/GFSDKBasic.h
index f6c80287..c9bf0e59 100644
--- a/src/sdk/GFSDKBasic.h
+++ b/src/sdk/GFSDKBasic.h
@@ -131,6 +131,35 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFAppActiveLocale() -> char*;
* @param size
* @return auto
*/
-auto GPGFRONTEND_MODULE_SDK_EXPORT
-GFAppRegisterTranslatorReader(const char* id, GFTranslatorDataReader reader) -> int;
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFAppRegisterTranslatorReader(
+ const char* id, GFTranslatorDataReader reader) -> int;
+
+/**
+ * @brief
+ *
+ * @param key
+ * @param value
+ * @return auto
+ */
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheSave(const char* key,
+ const char* value) -> int;
+
+/**
+ * @brief
+ *
+ * @param key
+ * @param value
+ * @return auto
+ */
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheSaveWithTTL(const char* key,
+ const char* value,
+ int ttl) -> int;
+
+/**
+ * @brief
+ *
+ * @param key
+ * @return const char*
+ */
+auto GPGFRONTEND_MODULE_SDK_EXPORT GFCacheGet(const char* key) -> const char*;
} \ No newline at end of file
diff --git a/src/test/core/GpgCoreTestCache.cpp b/src/test/core/GpgCoreTestCache.cpp
new file mode 100644
index 00000000..4d710944
--- /dev/null
+++ b/src/test/core/GpgCoreTestCache.cpp
@@ -0,0 +1,54 @@
+/**
+ * Copyright (C) 2021-2024 Saturneric <[email protected]>
+ *
+ * This file is part of GpgFrontend.
+ *
+ * GpgFrontend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GpgFrontend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The initial version of the source code is inherited from
+ * the gpg4usb project, which is under GPL-3.0-or-later.
+ *
+ * All the source code of GpgFrontend was modified and released by
+ * Saturneric <[email protected]> starting on May 12, 2021.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#include "GpgCoreTest.h"
+#include "core/GpgConstants.h"
+#include "core/function/CacheManager.h"
+#include "core/utils/GpgUtils.h"
+
+namespace GpgFrontend::Test {
+
+TEST_F(GpgCoreTest, CoreCacheTestA) {
+ CacheManager::GetInstance().SaveCache("ABC", "DEF");
+ ASSERT_EQ(CacheManager::GetInstance().LoadCache("ABC"), QString("DEF"));
+ ASSERT_EQ(CacheManager::GetInstance().LoadCache("ABCGG"), QString());
+}
+
+TEST_F(GpgCoreTest, CoreCacheTestB) {
+ CacheManager::GetInstance().SaveCache("ABCDE", "DEFG", 3);
+ ASSERT_EQ(CacheManager::GetInstance().LoadCache("ABCDE"), QString("DEFG"));
+}
+
+TEST_F(GpgCoreTest, CoreCacheTestC) {
+ CacheManager::GetInstance().SaveCache("ABCDEF", "DEF", 2);
+ ASSERT_EQ(CacheManager::GetInstance().LoadCache("ABCDEF"), QString("DEFG"));
+ sleep(4);
+ ASSERT_EQ(CacheManager::GetInstance().LoadCache("ABCDEF"), QString(""));
+}
+
+} // namespace GpgFrontend::Test \ No newline at end of file