Browse Source

We use only one global connection to the database

master
Michael Uleysky 4 months ago
parent
commit
28a003c488
  1. 229
      include/cache.h
  2. 14
      src/cache.cpp

229
include/cache.h

@ -1,14 +1,149 @@
#pragma once
#include "merrors.h"
#include "GPL.h"
#include <functional>
#include <libpq-fe.h>
#include <sqlite3.h>
#include <time.h>
#include <variant>
using michlib::GPL;
using michlib::int_cast;
using michlib::MString;
using michlib::pointer_cast;
class SQLiteConnection
{
public:
using DBType = sqlite3*;
using FuncType = std::function<void(DBType)>;
private:
static DBType db;
static size_t count;
static std::vector<FuncType> destructs;
public:
SQLiteConnection()
{
count++;
if(db == nullptr)
{
MString oldprefix = GPL.UsePrefix("SQLITE");
MString name = GPL.ParameterSValue("db", "");
GPL.UsePrefix(oldprefix);
auto ret = sqlite3_open(name.Buf(), &db);
if(ret != SQLITE_OK)
{
sqlite3_close(db);
db = nullptr;
}
}
}
SQLiteConnection([[maybe_unused]] const SQLiteConnection& sq): SQLiteConnection() {}
SQLiteConnection(SQLiteConnection&&) = default;
SQLiteConnection& operator=([[maybe_unused]] const SQLiteConnection& sq)
{
*this = {};
return *this;
}
SQLiteConnection& operator=(SQLiteConnection&&) = default;
~SQLiteConnection()
{
if(count == 0) michlib::errmessage("Destructor of SQLiteConnection called on count==0");
if(count > 1)
count--;
else
{
count = 0;
if(db != nullptr)
{
for(const auto& f: destructs) f(db);
sqlite3_close(db);
}
db = nullptr;
}
}
static void AddDestructor(FuncType&& f) { destructs.emplace_back(std::move(f)); }
operator DBType() const { return db; }
static DBType GetDB() { return db; }
explicit operator bool() const { return db != nullptr; }
};
class PostgreSQLConnection
{
public:
using DBType = PGconn*;
using FuncType = std::function<void(DBType)>;
private:
static DBType conn;
static size_t count;
static std::vector<FuncType> destructs;
public:
PostgreSQLConnection()
{
count++;
if(conn == nullptr)
{
MString oldprefix = GPL.UsePrefix("POSTGRES");
MString name = GPL.ParameterSValue("connection", "");
GPL.UsePrefix(oldprefix);
conn = PQconnectdb(name.Buf());
if(PQstatus(conn) != CONNECTION_OK)
{
michlib::errmessage(PQerrorMessage(conn));
PQfinish(conn);
conn = nullptr;
}
}
}
PostgreSQLConnection([[maybe_unused]] const PostgreSQLConnection& pq): PostgreSQLConnection() {}
PostgreSQLConnection(PostgreSQLConnection&&) = default;
PostgreSQLConnection& operator=([[maybe_unused]] const PostgreSQLConnection& pq)
{
*this = {};
return *this;
}
PostgreSQLConnection& operator=(PostgreSQLConnection&&) = default;
~PostgreSQLConnection()
{
if(count == 0) michlib::errmessage("Destructor of PostgreSQLConnection called on count==0");
if(count > 1)
count--;
else
{
count = 0;
if(conn != nullptr)
{
for(const auto& f: destructs) f(conn);
PQfinish(conn);
}
conn = nullptr;
}
}
static void AddDestructor(FuncType&& f) { destructs.emplace_back(std::move(f)); }
operator DBType() const { return conn; }
static DBType GetDB() { return conn; }
explicit operator bool() const { return conn != nullptr; }
};
class GenericCache
{
public:
@ -27,18 +162,17 @@ class FakeCache: public GenericCache
class SQLiteCache: public GenericCache
{
sqlite3* db = nullptr;
static bool regdest;
SQLiteConnection db;
public:
bool Init(const MString& name)
bool Init()
{
Close();
auto ret = sqlite3_open(name.Buf(), &db);
if(ret != SQLITE_OK)
if(!db) return false;
if(!regdest)
{
Close();
return false;
}
// Create table
sqlite3_stmt* sqst;
int i;
@ -51,18 +185,25 @@ class SQLiteCache: public GenericCache
if(i != SQLITE_DONE)
{
sqlite3_finalize(sqst);
Close();
return false;
}
sqlite3_finalize(sqst);
sqlite3_busy_timeout(db, 1000);
return true;
}
void Close()
db.AddDestructor(
[](SQLiteConnection::DBType db)
{
if(db != nullptr) sqlite3_close(db);
db = nullptr;
sqlite3_stmt* sqst = nullptr;
int i = SQLITE_OK;
if(i == SQLITE_OK) i = sqlite3_prepare_v2(db, "DELETE from `cache` WHERE exptime<?1;", -1, &sqst, 0);
if(i == SQLITE_OK) i = sqlite3_bind_int64(sqst, 1, time(nullptr));
if(i == SQLITE_OK) i = sqlite3_step(sqst);
sqlite3_finalize(sqst);
});
regdest = true;
}
return true;
}
virtual bool Put(const MString& key, const MString& value, size_t ttl) const override
@ -107,26 +248,16 @@ class SQLiteCache: public GenericCache
return {"", false};
}
virtual ~SQLiteCache() override
{
if(!*this) return;
sqlite3_stmt* sqst = nullptr;
int i = SQLITE_OK;
if(i == SQLITE_OK) i = sqlite3_prepare_v2(db, "DELETE from `cache` WHERE exptime<?1;", -1, &sqst, 0);
if(i == SQLITE_OK) i = sqlite3_bind_int64(sqst, 1, time(nullptr));
if(i == SQLITE_OK) i = sqlite3_step(sqst);
sqlite3_finalize(sqst);
sqlite3_close_v2(db);
}
virtual ~SQLiteCache() override = default;
explicit operator bool() const { return db != nullptr; }
};
class PostgreSQLCache: public GenericCache
{
PGconn* conn = nullptr;
static bool regdest;
PostgreSQLConnection conn;
bool CheckCon() const
{
@ -148,18 +279,12 @@ class PostgreSQLCache: public GenericCache
}
public:
bool Init(const MString& name)
bool Init()
{
Close();
conn = PQconnectdb(name.Buf());
if(!conn) return false;
if(PQstatus(conn) != CONNECTION_OK)
if(!regdest)
{
michlib::errmessage(PQerrorMessage(conn));
Close();
return false;
}
// Create table
if(false)
{
@ -169,18 +294,21 @@ class PostgreSQLCache: public GenericCache
michlib::errmessage(PQresStatus(PQresultStatus(res)));
michlib::errmessage(PQerrorMessage(conn));
PQclear(res);
Close();
}
else
PQclear(res);
}
return true;
}
void Close()
conn.AddDestructor(
[](PostgreSQLConnection::DBType conn)
{
if(conn != nullptr) PQfinish(conn);
conn = nullptr;
auto* res = PQexec(conn, "DELETE FROM cache WHERE exptime<localtimestamp;");
PQclear(res);
});
regdest = true;
}
return true;
}
virtual bool Put(const MString& key, const MString& value, size_t ttl) const override
@ -235,14 +363,7 @@ class PostgreSQLCache: public GenericCache
return {std::move(val), true};
}
virtual ~PostgreSQLCache() override
{
if(!CheckCon()) return;
auto* res = PQexec(conn, "DELETE FROM cache WHERE exptime<localtimestamp;");
PQclear(res);
Close();
}
virtual ~PostgreSQLCache() override = default;
explicit operator bool() const { return conn != nullptr; }
};
@ -262,14 +383,14 @@ inline GenericCache* CreateCache(const MString& cachedesc)
if(name == "sqlite")
{
auto ret = new SQLiteCache;
ret->Init(par);
ret->Init();
if(*ret) return ret;
delete ret;
}
if(name == "postgre" || name == "postgres" || name == "postgresql")
{
auto ret = new PostgreSQLCache;
ret->Init(par);
ret->Init();
if(*ret) return ret;
delete ret;
}

14
src/cache.cpp

@ -0,0 +1,14 @@
#define MICHLIB_NOSOURCE
#include "cache.h"
SQLiteConnection::DBType SQLiteConnection::db = nullptr;
size_t SQLiteConnection::count = 0;
std::vector<SQLiteConnection::FuncType> SQLiteConnection::destructs = {};
PostgreSQLConnection::DBType PostgreSQLConnection::conn = nullptr;
size_t PostgreSQLConnection::count = 0;
std::vector<PostgreSQLConnection::FuncType> PostgreSQLConnection::destructs = {};
bool SQLiteCache::regdest = false;
bool PostgreSQLCache::regdest = false;
Loading…
Cancel
Save