feat: cache key server data for 5 min
This commit is contained in:
parent
405522f3b0
commit
e76d2abcf9
@ -28,6 +28,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <GFSDKBasic.h>
|
||||||
|
#include <GFSDKLog.h>
|
||||||
|
#include <GFSDKModule.h>
|
||||||
#include <GFSDKUI.h>
|
#include <GFSDKUI.h>
|
||||||
#include <qsharedpointer.h>
|
#include <qsharedpointer.h>
|
||||||
|
|
||||||
@ -35,10 +38,6 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "GFSDKBasic.h"
|
|
||||||
#include "GFSDKLog.h"
|
|
||||||
#include "GFSDKModule.h"
|
|
||||||
|
|
||||||
#define DUP(v) GFModuleStrDup(v)
|
#define DUP(v) GFModuleStrDup(v)
|
||||||
#define UDUP(v) UnStrDup(v)
|
#define UDUP(v) UnStrDup(v)
|
||||||
#define QDUP(v) QStrDup(v)
|
#define QDUP(v) QStrDup(v)
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
|
|
||||||
|
#include "GFModuleCommonUtils.hpp"
|
||||||
#include "GFModuleDefine.h"
|
#include "GFModuleDefine.h"
|
||||||
#include "VKSInterface.h"
|
#include "VKSInterface.h"
|
||||||
|
|
||||||
@ -62,9 +63,9 @@ EXECUTE_MODULE() {
|
|||||||
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
|
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
|
||||||
|
|
||||||
auto* vks = new VKSInterface();
|
auto* vks = new VKSInterface();
|
||||||
vks->GetByFingerprint(fingerprint);
|
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
|
||||||
QThread::currentThread(), [event](const QString& key) {
|
QThread::currentThread(), [event](const QString& key) {
|
||||||
|
// callback
|
||||||
CB(event, GFGetModuleID(),
|
CB(event, GFGetModuleID(),
|
||||||
{
|
{
|
||||||
{"ret", QString::number(0)},
|
{"ret", QString::number(0)},
|
||||||
@ -86,6 +87,7 @@ EXECUTE_MODULE() {
|
|||||||
});
|
});
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
|
vks->GetByFingerprint(fingerprint);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -97,9 +99,9 @@ EXECUTE_MODULE() {
|
|||||||
FLOG_DEBUG("try to get key info of key id: %1", key_id);
|
FLOG_DEBUG("try to get key info of key id: %1", key_id);
|
||||||
|
|
||||||
auto* vks = new VKSInterface();
|
auto* vks = new VKSInterface();
|
||||||
vks->GetByKeyId(key_id);
|
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
|
||||||
QThread::currentThread(), [event](const QString& key) {
|
QThread::currentThread(), [event](const QString& key) {
|
||||||
|
// callback
|
||||||
CB(event, GFGetModuleID(),
|
CB(event, GFGetModuleID(),
|
||||||
{
|
{
|
||||||
{"ret", QString::number(0)},
|
{"ret", QString::number(0)},
|
||||||
@ -108,7 +110,6 @@ EXECUTE_MODULE() {
|
|||||||
});
|
});
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
|
|
||||||
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
|
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
|
||||||
QThread::currentThread(),
|
QThread::currentThread(),
|
||||||
[event](const QString& error, const QString& data) {
|
[event](const QString& error, const QString& data) {
|
||||||
@ -121,6 +122,7 @@ EXECUTE_MODULE() {
|
|||||||
});
|
});
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
|
vks->GetByKeyId(key_id);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -132,7 +134,6 @@ EXECUTE_MODULE() {
|
|||||||
FLOG_DEBUG("try to get key info of key id: %1", key_text);
|
FLOG_DEBUG("try to get key info of key id: %1", key_text);
|
||||||
|
|
||||||
auto* vks = new VKSInterface();
|
auto* vks = new VKSInterface();
|
||||||
vks->UploadKey(key_text);
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
|
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
|
||||||
[event](const QString& fpr, const QJsonObject& status,
|
[event](const QString& fpr, const QJsonObject& status,
|
||||||
@ -147,7 +148,6 @@ EXECUTE_MODULE() {
|
|||||||
});
|
});
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
|
|
||||||
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
|
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
|
||||||
QThread::currentThread(),
|
QThread::currentThread(),
|
||||||
[event](const QString& error, const QString& data) {
|
[event](const QString& error, const QString& data) {
|
||||||
@ -160,11 +160,11 @@ EXECUTE_MODULE() {
|
|||||||
});
|
});
|
||||||
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
|
||||||
&VKSInterface::deleteLater);
|
&VKSInterface::deleteLater);
|
||||||
|
vks->UploadKey(key_text);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CB_SUCC(event);
|
CB_ERR(event, -1, "the type of this event is not supported");
|
||||||
}
|
}
|
||||||
END_EXECUTE_MODULE()
|
END_EXECUTE_MODULE()
|
||||||
|
|
||||||
|
@ -41,6 +41,9 @@
|
|||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
//
|
||||||
|
#include <GFModuleCommonUtils.hpp>
|
||||||
|
|
||||||
VKSInterface::VKSInterface(QString key_server, QObject* parent)
|
VKSInterface::VKSInterface(QString key_server, QObject* parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
target_key_server_(std::move(key_server)),
|
target_key_server_(std::move(key_server)),
|
||||||
@ -50,6 +53,15 @@ VKSInterface::VKSInterface(QString key_server, QObject* parent)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VKSInterface::GetByFingerprint(const QString& fingerprint) {
|
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")
|
QUrl url(QString("%1/vks/v1/by-fingerprint/%2")
|
||||||
.arg(target_key_server_)
|
.arg(target_key_server_)
|
||||||
.arg(fingerprint));
|
.arg(fingerprint));
|
||||||
@ -57,8 +69,17 @@ void VKSInterface::GetByFingerprint(const QString& fingerprint) {
|
|||||||
network_manager_->get(request);
|
network_manager_->get(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VKSInterface::GetByKeyId(const QString& keyId) {
|
void VKSInterface::GetByKeyId(const QString& key_id) {
|
||||||
QUrl url(QString("%1/vks/v1/by-keyid/%2").arg(target_key_server_).arg(keyId));
|
// 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);
|
QNetworkRequest request(url);
|
||||||
network_manager_->get(request);
|
network_manager_->get(request);
|
||||||
}
|
}
|
||||||
@ -122,6 +143,7 @@ void VKSInterface::on_reply_finished(QNetworkReply* reply) {
|
|||||||
if (url.path().contains("/vks/v1/by-fingerprint") ||
|
if (url.path().contains("/vks/v1/by-fingerprint") ||
|
||||||
url.path().contains("/vks/v1/by-keyid") ||
|
url.path().contains("/vks/v1/by-keyid") ||
|
||||||
url.path().contains("/vks/v1/by-email")) {
|
url.path().contains("/vks/v1/by-email")) {
|
||||||
|
GFCacheSaveWithTTL(QDUP(cache_key_), QDUP(QString(response_data)), 300);
|
||||||
emit SignalKeyRetrieved(QString(response_data));
|
emit SignalKeyRetrieved(QString(response_data));
|
||||||
} else if (url.path().contains("/vks/v1/upload")) {
|
} else if (url.path().contains("/vks/v1/upload")) {
|
||||||
if (json_response.isObject()) {
|
if (json_response.isObject()) {
|
||||||
|
@ -58,6 +58,7 @@ class VKSInterface : public QObject {
|
|||||||
void on_reply_finished(QNetworkReply* reply);
|
void on_reply_finished(QNetworkReply* reply);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QString cache_key_;
|
||||||
QString target_key_server_;
|
QString target_key_server_;
|
||||||
QNetworkAccessManager* network_manager_;
|
QNetworkAccessManager* network_manager_;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user