extended statement_cache with message bus events

This commit is contained in:
2025-08-10 22:12:13 +02:00
parent 60cef7e938
commit 4f017ac1de
4 changed files with 87 additions and 62 deletions
+18 -14
View File
@@ -4,6 +4,8 @@
#include "matador/sql/executor.hpp"
#include "matador/sql/statement.hpp"
#include "matador/utils/message_bus.hpp"
#include <list>
#include <mutex>
#include <unordered_map>
@@ -12,17 +14,18 @@ namespace matador::sql {
class connection_pool;
struct statement_cache_event {
enum class Type { Accessed, Added, Evicted };
Type type;
struct statement_event {
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;
struct statement_accessed_event : statement_event {};
struct statement_added_event : statement_event {};
struct statement_evicted_event : statement_event {};
struct statement_cache_config {
size_t max_size;
};
class statement_cache final {
@@ -36,7 +39,7 @@ private:
};
public:
explicit statement_cache(connection_pool &pool, size_t max_size = 50);
statement_cache(utils::message_bus &bus, 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;
@@ -49,13 +52,13 @@ public:
[[nodiscard]] size_t capacity() const;
[[nodiscard]] bool empty() const;
void subscribe(statement_cache_observer_interface &observer);
template<class EventType>
utils::subscription subscribe(std::function<void(const EventType&)> handler) {
return bus_.subscribe<EventType>(handler);
}
private:
void push(statement_cache_event::Type type, const std::string& sql) const;
private:
size_t max_size_{};
std::list<size_t> usage_list_; // LRU: front = most recent, back = least recent
std::unordered_map<size_t, cache_entry> cache_map_;
@@ -64,7 +67,8 @@ private:
connection_pool &pool_;
const sql::dialect &dialect_;
std::vector<statement_cache_observer_interface*> observers_;
utils::message_bus &bus_;
};
}
#endif //STATEMENT_CACHE_HPP
+3 -3
View File
@@ -152,16 +152,16 @@ public:
}
};
std::function<bool(const T&)> filt = nullptr;
std::function<bool(const T&)> local_filter = nullptr;
if (filter) {
filt = [w, filter = std::move(filter)](const T& m) -> bool {
local_filter = [w, filter = std::move(filter)](const T& m) -> bool {
if (w.expired()) {
return false;
}
return filter(m);
};
}
return subscribe<T>(std::move(handler), std::move(filt));
return subscribe<T>(std::move(handler), std::move(local_filter));
}
// Unsubscribe by type-specific id (RAII calls this)