Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#pragma once
|
|
|
|
// In-memory key-value store, TTL cache, and pubsub broker skeletons.
|
|
// Gap: real implementations in 04-data.md own persistence, replication,
|
|
// durability guarantees, and multi-instance sharing.
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace molo::example::fw {
|
|
|
|
class KvStore {
|
|
public:
|
|
bool set(const std::string& key, const std::string& value);
|
|
bool get(const std::string& key, std::string& out) const;
|
|
bool del(const std::string& key);
|
|
std::size_t size() const;
|
|
|
|
private:
|
|
mutable std::mutex mu_;
|
|
std::map<std::string, std::string> data_;
|
|
};
|
|
|
|
struct CacheEntry {
|
|
std::string value;
|
|
long expires_at = 0;
|
|
};
|
|
|
|
class Cache {
|
|
public:
|
|
void put(const std::string& key, const std::string& value, long ttl_sec,
|
|
long now);
|
|
bool lookup(const std::string& key, std::string& out, long now);
|
|
void invalidate(const std::string& key);
|
|
std::size_t size() const;
|
|
|
|
private:
|
|
mutable std::mutex mu_;
|
|
std::map<std::string, CacheEntry> data_;
|
|
};
|
|
|
|
using PubsubHandler = std::function<void(const std::string& message)>;
|
|
|
|
class Pubsub {
|
|
public:
|
|
bool subscribe(const std::string& topic, PubsubHandler handler);
|
|
std::size_t publish(const std::string& topic, const std::string& message);
|
|
std::vector<std::string> history(const std::string& topic) const;
|
|
std::vector<std::string> topics() const;
|
|
|
|
private:
|
|
mutable std::mutex mu_;
|
|
std::map<std::string, std::vector<PubsubHandler>> subs_;
|
|
std::map<std::string, std::vector<std::string>> history_;
|
|
};
|
|
|
|
} // namespace molo::example::fw
|