From e76d2abcf93a2299382983cb3b0b515eb75a1447 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 20 Nov 2024 16:30:01 +0100 Subject: [PATCH] feat: cache key server data for 5 min --- include/GFModuleCommonUtils.hpp | 7 +++-- src/m_key_server_sync/KeyServerSyncModule.cpp | 14 +++++----- src/m_key_server_sync/VKSInterface.cpp | 26 +++++++++++++++++-- src/m_key_server_sync/VKSInterface.h | 1 + 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/include/GFModuleCommonUtils.hpp b/include/GFModuleCommonUtils.hpp index 1d23d2d..d64f203 100644 --- a/include/GFModuleCommonUtils.hpp +++ b/include/GFModuleCommonUtils.hpp @@ -28,6 +28,9 @@ #pragma once +#include +#include +#include #include #include @@ -35,10 +38,6 @@ #include #include -#include "GFSDKBasic.h" -#include "GFSDKLog.h" -#include "GFSDKModule.h" - #define DUP(v) GFModuleStrDup(v) #define UDUP(v) UnStrDup(v) #define QDUP(v) QStrDup(v) diff --git a/src/m_key_server_sync/KeyServerSyncModule.cpp b/src/m_key_server_sync/KeyServerSyncModule.cpp index 8455993..376bb08 100644 --- a/src/m_key_server_sync/KeyServerSyncModule.cpp +++ b/src/m_key_server_sync/KeyServerSyncModule.cpp @@ -30,6 +30,7 @@ #include +#include "GFModuleCommonUtils.hpp" #include "GFModuleDefine.h" #include "VKSInterface.h" @@ -62,9 +63,9 @@ EXECUTE_MODULE() { FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint); auto* vks = new VKSInterface(); - vks->GetByFingerprint(fingerprint); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, QThread::currentThread(), [event](const QString& key) { + // callback CB(event, GFGetModuleID(), { {"ret", QString::number(0)}, @@ -86,6 +87,7 @@ EXECUTE_MODULE() { }); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, &VKSInterface::deleteLater); + vks->GetByFingerprint(fingerprint); return 0; } @@ -97,9 +99,9 @@ EXECUTE_MODULE() { FLOG_DEBUG("try to get key info of key id: %1", key_id); auto* vks = new VKSInterface(); - vks->GetByKeyId(key_id); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, QThread::currentThread(), [event](const QString& key) { + // callback CB(event, GFGetModuleID(), { {"ret", QString::number(0)}, @@ -108,7 +110,6 @@ EXECUTE_MODULE() { }); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, &VKSInterface::deleteLater); - QObject::connect(vks, &VKSInterface::SignalErrorOccurred, QThread::currentThread(), [event](const QString& error, const QString& data) { @@ -121,6 +122,7 @@ EXECUTE_MODULE() { }); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, &VKSInterface::deleteLater); + vks->GetByKeyId(key_id); return 0; } @@ -132,7 +134,6 @@ EXECUTE_MODULE() { FLOG_DEBUG("try to get key info of key id: %1", key_text); auto* vks = new VKSInterface(); - vks->UploadKey(key_text); QObject::connect( vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(), [event](const QString& fpr, const QJsonObject& status, @@ -147,7 +148,6 @@ EXECUTE_MODULE() { }); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, &VKSInterface::deleteLater); - QObject::connect(vks, &VKSInterface::SignalErrorOccurred, QThread::currentThread(), [event](const QString& error, const QString& data) { @@ -160,11 +160,11 @@ EXECUTE_MODULE() { }); QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, &VKSInterface::deleteLater); - + vks->UploadKey(key_text); return 0; } - CB_SUCC(event); + CB_ERR(event, -1, "the type of this event is not supported"); } END_EXECUTE_MODULE() diff --git a/src/m_key_server_sync/VKSInterface.cpp b/src/m_key_server_sync/VKSInterface.cpp index 9b4f4fd..eb16097 100644 --- a/src/m_key_server_sync/VKSInterface.cpp +++ b/src/m_key_server_sync/VKSInterface.cpp @@ -41,6 +41,9 @@ #include #include +// +#include + VKSInterface::VKSInterface(QString key_server, QObject* parent) : QObject(parent), target_key_server_(std::move(key_server)), @@ -50,6 +53,15 @@ VKSInterface::VKSInterface(QString key_server, QObject* parent) } void VKSInterface::GetByFingerprint(const QString& fingerprint) { + // search cache by first + cache_key_ = + QString("module:key-server-sync:key-data:fpr:%1").arg(fingerprint); + auto value = UDUP(GFCacheGet(QDUP(cache_key_))); + if (!value.isEmpty()) { + emit SignalKeyRetrieved(value); + return; + } + QUrl url(QString("%1/vks/v1/by-fingerprint/%2") .arg(target_key_server_) .arg(fingerprint)); @@ -57,8 +69,17 @@ void VKSInterface::GetByFingerprint(const QString& fingerprint) { network_manager_->get(request); } -void VKSInterface::GetByKeyId(const QString& keyId) { - QUrl url(QString("%1/vks/v1/by-keyid/%2").arg(target_key_server_).arg(keyId)); +void VKSInterface::GetByKeyId(const QString& key_id) { + // search cache by first + cache_key_ = QString("module:key-server-sync:key-data:id:%1").arg(key_id); + auto value = UDUP(GFCacheGet(QDUP(cache_key_))); + if (!value.isEmpty()) { + emit SignalKeyRetrieved(value); + return; + } + + QUrl url( + QString("%1/vks/v1/by-keyid/%2").arg(target_key_server_).arg(key_id)); QNetworkRequest request(url); network_manager_->get(request); } @@ -122,6 +143,7 @@ void VKSInterface::on_reply_finished(QNetworkReply* reply) { if (url.path().contains("/vks/v1/by-fingerprint") || url.path().contains("/vks/v1/by-keyid") || url.path().contains("/vks/v1/by-email")) { + GFCacheSaveWithTTL(QDUP(cache_key_), QDUP(QString(response_data)), 300); emit SignalKeyRetrieved(QString(response_data)); } else if (url.path().contains("/vks/v1/upload")) { if (json_response.isObject()) { diff --git a/src/m_key_server_sync/VKSInterface.h b/src/m_key_server_sync/VKSInterface.h index e487ea4..67575db 100644 --- a/src/m_key_server_sync/VKSInterface.h +++ b/src/m_key_server_sync/VKSInterface.h @@ -58,6 +58,7 @@ class VKSInterface : public QObject { void on_reply_finished(QNetworkReply* reply); private: + QString cache_key_; QString target_key_server_; QNetworkAccessManager* network_manager_; };