finished statement cache class
This commit is contained in:
@@ -2,8 +2,7 @@
|
||||
#define STATEMENT_CACHE_HPP
|
||||
|
||||
#include "matador/sql/executor.hpp"
|
||||
|
||||
#include "matador/sql/interface/statement_proxy.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
@@ -13,9 +12,36 @@ namespace matador::sql {
|
||||
|
||||
class connection_pool;
|
||||
|
||||
struct statement_cache_event {
|
||||
enum class Type { Accessed, Added, Evicted };
|
||||
Type type;
|
||||
std::string sql;
|
||||
std::chrono::steady_clock::time_point timestamp;
|
||||
};
|
||||
|
||||
class statement_cache_observer_interface {
|
||||
public:
|
||||
virtual void on_event(const statement_cache_event&) = 0;
|
||||
virtual ~statement_cache_observer_interface() = default;
|
||||
};
|
||||
|
||||
class statement_cache final {
|
||||
private:
|
||||
using list_iterator = std::list<size_t>::iterator;
|
||||
|
||||
struct cache_entry {
|
||||
statement stmt;
|
||||
std::chrono::steady_clock::time_point last_access;
|
||||
list_iterator position;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit statement_cache(connection_pool &pool, size_t max_size = 50);
|
||||
statement_cache(const statement_cache &) = delete;
|
||||
statement_cache &operator=(const statement_cache &) = delete;
|
||||
statement_cache(statement_cache &&) = delete;
|
||||
statement_cache &operator=(statement_cache &&) = delete;
|
||||
~statement_cache() = default;
|
||||
|
||||
[[nodiscard]] utils::result<statement, utils::error> acquire(const query_context &ctx);
|
||||
|
||||
@@ -23,16 +49,22 @@ public:
|
||||
[[nodiscard]] size_t capacity() const;
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
void subscribe(statement_cache_observer_interface &observer);
|
||||
|
||||
private:
|
||||
void push(statement_cache_event::Type type, const std::string& sql) 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::unordered_map<size_t, cache_entry> cache_map_;
|
||||
std::mutex mutex_;
|
||||
|
||||
connection_pool &pool_;
|
||||
const sql::dialect &dialect_;
|
||||
|
||||
std::vector<statement_cache_observer_interface*> observers_;
|
||||
};
|
||||
}
|
||||
#endif //STATEMENT_CACHE_HPP
|
||||
|
||||
Reference in New Issue
Block a user