39 lines
935 B
C++
39 lines
935 B
C++
#ifndef STATEMENT_CACHE_HPP
|
|
#define STATEMENT_CACHE_HPP
|
|
|
|
#include "matador/sql/executor.hpp"
|
|
|
|
#include "matador/sql/interface/statement_proxy.hpp"
|
|
|
|
#include <list>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
|
|
namespace matador::sql {
|
|
|
|
class connection_pool;
|
|
|
|
class statement_cache final {
|
|
public:
|
|
explicit statement_cache(connection_pool &pool, size_t max_size = 50);
|
|
|
|
[[nodiscard]] utils::result<statement, utils::error> acquire(const query_context &ctx);
|
|
|
|
[[nodiscard]] size_t size() const;
|
|
[[nodiscard]] size_t capacity() const;
|
|
[[nodiscard]] bool empty() const;
|
|
|
|
private:
|
|
using list_iterator = std::list<size_t>::iterator;
|
|
|
|
size_t max_size_{};
|
|
std::list<size_t> usage_list_; // LRU: front = most recent, back = least recent
|
|
std::unordered_map<size_t, std::pair<statement, list_iterator>> cache_map_;
|
|
std::mutex mutex_;
|
|
|
|
connection_pool &pool_;
|
|
const sql::dialect &dialect_;
|
|
};
|
|
}
|
|
#endif //STATEMENT_CACHE_HPP
|