feat: cache key server data for 5 min

This commit is contained in:
saturneric 2024-11-20 16:30:01 +01:00
parent 405522f3b0
commit e76d2abcf9
4 changed files with 35 additions and 13 deletions

View File

@ -28,6 +28,9 @@
#pragma once
#include <GFSDKBasic.h>
#include <GFSDKLog.h>
#include <GFSDKModule.h>
#include <GFSDKUI.h>
#include <qsharedpointer.h>
@ -35,10 +38,6 @@
#include <QString>
#include <cstring>
#include "GFSDKBasic.h"
#include "GFSDKLog.h"
#include "GFSDKModule.h"
#define DUP(v) GFModuleStrDup(v)
#define UDUP(v) UnStrDup(v)
#define QDUP(v) QStrDup(v)

View File

@ -30,6 +30,7 @@
#include <QtCore>
#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()

View File

@ -41,6 +41,9 @@
#include <QUrlQuery>
#include <utility>
//
#include <GFModuleCommonUtils.hpp>
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()) {

View File

@ -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_;
};