database header
This commit is contained in:
parent
2923f6f5c5
commit
acb7221e34
2 changed files with 41 additions and 7 deletions
|
|
@ -2,27 +2,34 @@
|
|||
#define DATABASE_SERVICE_HPP
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/system/detail/error_code.hpp>
|
||||
#include <cstddef>
|
||||
#include <boost/redis/connection.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
class DatabaseService {
|
||||
public:
|
||||
static DatabaseService& getInstance(boost::asio::io_context& io_context, std::size_t pool_size);
|
||||
void asyncSetKey(const std::string& key, const std::string& value, std::function<void(boost::system::error_code)> callback);
|
||||
void asyncGetKey(const std::string& key, std::function<void(boost::system::error_code, std::string)> callback); //we could use a varient here for the result
|
||||
|
||||
|
||||
private:
|
||||
std::shared_ptr<boost::redis::connection> getConnection(); //retrive a connection from the connection pool
|
||||
|
||||
private:
|
||||
DatabaseService(boost::asio::io_context& io_context, std::size_t pool_size = 4);
|
||||
~DatabaseService();
|
||||
DatabaseService(DatabaseService const&);
|
||||
void operator=(DatabaseService const&);
|
||||
|
||||
std::shared_ptr<boost::redis::connection> getConnection();
|
||||
|
||||
boost::asio::io_context& io_context_;
|
||||
std::size_t pool_size_;
|
||||
std::vector<std::shared_ptr<boost::redis::connection>> connection_pool_;
|
||||
std::size_t current_index_ = 0; //index used for round robin selection
|
||||
|
||||
static DatabaseService* INSTANCE;
|
||||
static std::mutex singleton_mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -1,16 +1,43 @@
|
|||
#include "../includes/database_service.hpp"
|
||||
#include <boost/redis/connection.hpp>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
std::mutex DatabaseService::singleton_mutex;
|
||||
DatabaseService* DatabaseService::INSTANCE = nullptr;
|
||||
|
||||
DatabaseService& DatabaseService::getInstance(boost::asio::io_context &io_context, std::size_t pool_size) {
|
||||
static DatabaseService INSTANCE;
|
||||
return INSTANCE;
|
||||
std::lock_guard<std::mutex> lock(singleton_mutex);
|
||||
if (!INSTANCE) {
|
||||
INSTANCE = new DatabaseService(io_context, pool_size);
|
||||
}
|
||||
return *INSTANCE;
|
||||
}
|
||||
|
||||
DatabaseService::DatabaseService(boost::asio::io_context& io_context, std::size_t pool_size) : io_context_(io_context), pool_size_(pool_size) {
|
||||
DatabaseService::DatabaseService(boost::asio::io_context& io_context, std::size_t pool_size) : io_context_(io_context), pool_size_(pool_size), current_index_(0) {
|
||||
if (pool_size_ <= 0) {
|
||||
throw std::invalid_argument("Connection pool size must be greater than zero.");
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < pool_size_; ++i) {
|
||||
auto conn = std::make_shared<boost::redis::connection>(io_context_);
|
||||
connection_pool_.emplace_back(conn);
|
||||
}
|
||||
}
|
||||
|
||||
DatabaseService::~DatabaseService() {
|
||||
connection_pool_.clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<boost::redis::connection> DatabaseService::getConnection() {
|
||||
std::lock_guard<std::mutex> lock(singleton_mutex);
|
||||
if (connection_pool_.empty()) {
|
||||
throw std::runtime_error("Connection pool is empty.");
|
||||
}
|
||||
|
||||
auto conn = connection_pool_[current_index_];
|
||||
current_index_ = (current_index_ + 1) % pool_size_;
|
||||
return conn;
|
||||
}
|
||||
Reference in a new issue