first commit
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
#include "dbredis.h"
|
||||
#include <QDateTime>
|
||||
|
||||
QSqlDatabase DBRedis::m_DataBase;
|
||||
QString DBRedis::m_ConnectName = "dbredis";
|
||||
bool DBRedis::m_HasInit = false;
|
||||
|
||||
|
||||
DBRedis::DBRedis()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DBRedis::~DBRedis()
|
||||
{
|
||||
if (m_DataBase.isOpen())
|
||||
{
|
||||
m_DataBase.close();
|
||||
}
|
||||
}
|
||||
|
||||
int DBRedis::deleteExpiredEntries()
|
||||
{
|
||||
int res = init();
|
||||
if (res)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
qint64 currentTimestamp = QDateTime::currentDateTime().toSecsSinceEpoch();
|
||||
QSqlQuery query(m_DataBase);
|
||||
query.prepare("DELETE FROM data_store WHERE expiry > 0 AND expiry < :currentTimestamp");
|
||||
query.bindValue(":currentTimestamp", currentTimestamp);
|
||||
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << "Error deleting expired entries:" << query.lastError().text();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int DBRedis::setData(const QString & key, const QString & value, uint32_t expiry)
|
||||
{
|
||||
int res = deleteExpiredEntries();
|
||||
if (res)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_DataBase);
|
||||
query.prepare("SELECT COUNT(*) FROM data_store WHERE key = :key");
|
||||
query.bindValue(":key", key);
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << "Error checking existing key";
|
||||
return 1;
|
||||
}
|
||||
int count = 0;
|
||||
while (query.next())
|
||||
{
|
||||
count += query.value(0).toInt();
|
||||
}
|
||||
if (count > 0)
|
||||
{
|
||||
query.prepare("UPDATE data_store SET value = :value, expiry = :expiry WHERE key = :key");
|
||||
query.bindValue(":value", value);
|
||||
query.bindValue(":expiry", expiry > 0 ? QDateTime::currentDateTime().toSecsSinceEpoch() + expiry : 0);
|
||||
query.bindValue(":key", key);
|
||||
}
|
||||
else
|
||||
{
|
||||
query.prepare("INSERT INTO data_store (key, value, expiry) VALUES (:key, :value, :expiry)");
|
||||
query.bindValue(":key", key);
|
||||
query.bindValue(":value", value);
|
||||
query.bindValue(":expiry", expiry > 0 ? QDateTime::currentDateTime().toSecsSinceEpoch() + expiry : 0);
|
||||
}
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << "Error setting value:" << query.lastError().text();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DBRedis::getData(const QString & key, QString & value)
|
||||
{
|
||||
value.clear();
|
||||
int res = deleteExpiredEntries();
|
||||
if (res)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_DataBase);
|
||||
query.prepare("SELECT value FROM data_store WHERE key = :key");
|
||||
query.bindValue(":key", key);
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << "Error getting value:" << query.lastError().text();
|
||||
return 1;
|
||||
}
|
||||
if (!query.next())
|
||||
{
|
||||
qDebug() << "Error getting value:" << query.lastError().text();
|
||||
return 2;
|
||||
}
|
||||
value = query.value(0).toString();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DBRedis::deleteData(const QString & key)
|
||||
{
|
||||
int res = deleteExpiredEntries();
|
||||
if (res)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
QSqlQuery query(m_DataBase);
|
||||
query.prepare("DELETE FROM data_store WHERE key = :key");
|
||||
query.bindValue(":key", key);
|
||||
if (!query.exec())
|
||||
{
|
||||
qDebug() << "Error deleting value:" << query.lastError().text();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DBRedis::init()
|
||||
{
|
||||
if (m_HasInit)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
m_DataBase = QSqlDatabase::addDatabase("QSQLITE", m_ConnectName);
|
||||
m_DataBase.setDatabaseName("dbredis.db");
|
||||
if (m_DataBase.open())
|
||||
{
|
||||
QString table = "CREATE TABLE IF NOT EXISTS data_store(key TEXT, value TEXT, expiry INTEGER)";
|
||||
QSqlQuery query(QSqlDatabase::database(m_ConnectName));
|
||||
if (!query.exec(table.toUtf8()))
|
||||
{
|
||||
qDebug() << "Error deleting value:" << query.lastError().text();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Error db open fail";
|
||||
return 3;
|
||||
}
|
||||
m_HasInit = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#ifndef __DBREDIS_H
|
||||
#define __DBREDIS_H
|
||||
|
||||
#include <QtSql>
|
||||
|
||||
|
||||
class DBRedis
|
||||
{
|
||||
public:
|
||||
DBRedis();
|
||||
~DBRedis();
|
||||
/***
|
||||
* @描述 : 设定值
|
||||
* @入参 : key 键
|
||||
value 值
|
||||
expiry 单位为秒 默认为 0
|
||||
0 为永不过期
|
||||
其他值必须大于0
|
||||
* @输出 : 无
|
||||
* @返回 : 0 成功
|
||||
1 数据库错误
|
||||
3 数据库错误
|
||||
*/
|
||||
static int setData(const QString&key, const QString&value, uint32_t expiry = 0);
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* @描述 : 获取值
|
||||
* @入参 : key 键
|
||||
* @输出 : value 值
|
||||
* @返回 : 0 成功
|
||||
1 数据库错误
|
||||
2 不存在该数据或已过期
|
||||
3 数据库错误
|
||||
*/
|
||||
static int getData(const QString&key, QString&value);
|
||||
|
||||
|
||||
/***
|
||||
* @描述 : 删除值
|
||||
* @入参 : key 键
|
||||
* @输出 : 无
|
||||
* @返回 : 0 成功
|
||||
1 数据库错误
|
||||
3 数据库错误
|
||||
*/
|
||||
static int deleteData(const QString&key);
|
||||
private:
|
||||
static QSqlDatabase m_DataBase;
|
||||
static QString m_ConnectName;
|
||||
static bool m_HasInit;
|
||||
private:
|
||||
/***
|
||||
* @描述 : 初始化
|
||||
* @入参 : 无
|
||||
* @输出 : 无
|
||||
* @返回 : 0 成功
|
||||
1 数据库错误
|
||||
3 数据库错误
|
||||
*/
|
||||
static int init();
|
||||
|
||||
|
||||
/***
|
||||
* @描述 : 删除过期的条目
|
||||
* @入参 : 无
|
||||
* @输出 : 无
|
||||
* @返回 : 0 成功
|
||||
1 数据库错误
|
||||
3 数据库错误
|
||||
*/
|
||||
static int deleteExpiredEntries();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // !__DBREDIS_H
|
||||
|
||||
|
||||
Reference in New Issue
Block a user