#include "Kv.hpp" #include "Caps.hpp" namespace molo::example::fw { bool KvStore::set(const std::string& key, const std::string& value) { std::lock_guard lock(mu_); if (!data_.count(key) && data_.size() >= caps::kMaxKvEntries) return false; data_[key] = value; return true; } bool KvStore::get(const std::string& key, std::string& out) const { std::lock_guard lock(mu_); auto it = data_.find(key); if (it == data_.end()) return false; out = it->second; return true; } bool KvStore::del(const std::string& key) { std::lock_guard lock(mu_); return data_.erase(key) > 0; } std::size_t KvStore::size() const { std::lock_guard lock(mu_); return data_.size(); } void Cache::put(const std::string& key, const std::string& value, long ttl_sec, long now) { std::lock_guard lock(mu_); if (!data_.count(key) && data_.size() >= caps::kMaxCacheEntries) return; data_[key] = CacheEntry{value, now + ttl_sec}; } bool Cache::lookup(const std::string& key, std::string& out, long now) { std::lock_guard lock(mu_); auto it = data_.find(key); if (it == data_.end()) return false; if (it->second.expires_at <= now) { data_.erase(it); return false; } out = it->second.value; return true; } void Cache::invalidate(const std::string& key) { std::lock_guard lock(mu_); data_.erase(key); } std::size_t Cache::size() const { std::lock_guard lock(mu_); return data_.size(); } bool Pubsub::subscribe(const std::string& topic, PubsubHandler handler) { std::lock_guard lock(mu_); if (!subs_.count(topic) && subs_.size() >= caps::kMaxPubsubTopics) return false; subs_[topic].push_back(std::move(handler)); return true; } std::size_t Pubsub::publish(const std::string& topic, const std::string& message) { std::lock_guard lock(mu_); auto& hist = history_[topic]; hist.push_back(message); if (hist.size() > caps::kMaxPubsubHistory) hist.erase(hist.begin()); auto it = subs_.find(topic); if (it == subs_.end()) return 0; for (auto& h : it->second) h(message); return it->second.size(); } std::vector Pubsub::history(const std::string& topic) const { std::lock_guard lock(mu_); auto it = history_.find(topic); if (it == history_.end()) return {}; return it->second; } std::vector Pubsub::topics() const { std::lock_guard lock(mu_); std::vector out; for (const auto& [k, v] : subs_) out.push_back(k); return out; } } // namespace molo::example::fw