用c++11写一个unqlite的单例,并提供unqlite_kv_store_fmt的封装存储接口和读取数据库的接口,以及删除数据的接口,和删除90天前的数据接口
时间: 2024-10-12 09:04:31 浏览: 27
在C++11中,我们可以创建一个UnQLite的单例模式,同时封装`sqlite3`库的功能,以便提供KV存储、读取、删除数据以及删除旧数据等功能。以下是一个基本的示例:
```cpp
#include <iostream>
#include <sqlite3.h>
#include <chrono>
class UnQLiteSingleton {
private:
static std::unique_ptr<sqlite3> instance;
explicit UnQLiteSingleton() { openDatabase(); }
~UnQLiteSingleton() { closeConnection(); }
public:
// 单例获取接口
static UnQLiteSingleton& getInstance() {
if (!instance) {
instance = std::make_unique<sqlite3>();
if (sqlite3_open("my_database.db", &instance->db_) != SQLITE_OK) {
throw std::runtime_error{"Failed to open SQLite database"};
}
}
return *instance;
}
// KV存储接口
void store(const std::string& key, const std::string& value) {
sqlite3_stmt* stmt;
std::string sql = "INSERT INTO kv_store(key, value) VALUES (?, ?)";
int rc = sqlite3_prepare_v2(instance->db_, sql.c_str(), -1, &stmt, nullptr);
if (rc == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, value.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) != SQLITE_DONE) {
std::cerr << "Error storing data: " << sqlite3_errmsg(instance->db_) << std::endl;
}
sqlite3_finalize(stmt);
} else {
std::cerr << "Error preparing SQL statement: " << sqlite3_errmsg(instance->db_) << std::endl;
}
}
// 读取数据接口
std::optional<std::string> read(const std::string& key) {
std::optional<std::string> result;
sqlite3_stmt* stmt;
std::string sql = "SELECT value FROM kv_store WHERE key = ?";
int rc = sqlite3_prepare_v2(instance->db_, sql.c_str(), -1, &stmt, nullptr);
if (rc == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
if (sqlite3_step(stmt) == SQLITE_ROW) {
char* value = reinterpret_cast<char*>(sqlite3_column_text(stmt, 0));
result = value ? std::string{value} : std::nullopt;
}
sqlite3_finalize(stmt);
} else {
std::cerr << "Error preparing SQL statement: " << sqlite3_errmsg(instance->db_) << std::endl;
}
return result;
}
// 删除数据接口
bool remove(const std::string& key) {
sqlite3_stmt* stmt;
std::string sql = "DELETE FROM kv_store WHERE key = ?";
int rc = sqlite3_prepare_v2(instance->db_, sql.c_str(), -1, &stmt, nullptr);
if (rc == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
int status = sqlite3_step(stmt);
if (status == SQLITE_DONE) {
return true;
} else if (status == SQLITE_ERROR) {
std::cerr << "Error deleting data: " << sqlite3_errmsg(instance->db_) << std::endl;
}
}
sqlite3_finalize(stmt);
return false;
}
// 删除90天前数据接口(这里假设有一个timestamp字段)
bool pruneOldData(int days_to_prune = 90) {
sqlite3_stmt* stmt;
std::string sql = R"(DELETE FROM kv_store WHERE timestamp < datetime('now', '-days', ?))";
int rc = sqlite3_prepare_v2(instance->db_, sql.c_str(), -1, &stmt, nullptr);
if (rc == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, days_to_prune);
int status = sqlite3_step(stmt);
if (status == SQLITE_DONE) {
return true;
} else if (status == SQLITE_ERROR) {
std::cerr << "Error pruning old data: " << sqlite3_errmsg(instance->db_) << std::endl;
}
}
sqlite3_finalize(stmt);
return false;
}
private:
void openDatabase() {
// 初始化SQLite连接并处理错误
// 这里省略具体的打开过程,实际操作需要处理可能出现的错误
}
void closeConnection() {
// 关闭SQLite连接
if (sqlite3_close(instance->db_)) {
std::cerr << "Error closing SQLite connection" << std::endl;
}
instance.reset();
}
sqlite3* db_ = nullptr; // SQLite数据库句柄
};
std::unique_ptr<sqlite3> UnQLiteSingleton::instance;
// 使用示例
int main() {
UnQLiteSingleton::getInstance().store("test_key", "test_value");
auto value = UnQLiteSingleton::getInstance().read("test_key");
if (value) {
std::cout << "Read value: " << *value << std::endl;
}
UnQLiteSingleton::getInstance().remove("test_key");
// 删除90天前的数据
if (UnQLiteSingleton::getInstance().pruneOldData(90)) {
std::cout << "Pruned old data." << std::endl;
}
return 0;
}
```
阅读全文