NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
node_cache.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <neograph/api.h>
9
10#include <cstddef>
11#include <mutex>
12#include <optional>
13#include <set>
14#include <string>
15#include <unordered_map>
16
17namespace neograph::graph {
18
38class NEOGRAPH_API NodeCache {
39public:
40 NodeCache() = default;
41
43 void set_enabled(const std::string& node_name, bool enabled);
44
46 bool is_enabled(const std::string& node_name) const;
47
49 std::optional<NodeResult> lookup(const std::string& node_name,
50 const std::string& state_hash) const;
51
53 void store(const std::string& node_name,
54 const std::string& state_hash,
55 NodeResult result);
56
58 void clear();
59
61 std::size_t size() const;
62
64 std::size_t hit_count() const;
67 std::size_t miss_count() const;
68
69private:
70 static std::string make_key(const std::string& node_name,
71 const std::string& state_hash);
72
73 mutable std::mutex mu_;
74 std::set<std::string> enabled_nodes_;
75 std::unordered_map<std::string, NodeResult> entries_;
76 mutable std::size_t hits_ = 0;
77 mutable std::size_t misses_ = 0;
78};
79
83NEOGRAPH_API std::string hash_state_for_cache(const json& state_value);
84
85} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Per-node result cache.
Definition node_cache.h:38
void store(const std::string &node_name, const std::string &state_hash, NodeResult result)
Store the NodeResult for this (node, state_hash).
std::optional< NodeResult > lookup(const std::string &node_name, const std::string &state_hash) const
Lookup. nullopt if no cached result for this (node, state_hash).
void set_enabled(const std::string &node_name, bool enabled)
Enable / disable caching for a specific node.
std::size_t miss_count() const
Lifetime miss count (incremented on lookup that returns nullopt for an enabled node).
std::size_t hit_count() const
Lifetime hit count (incremented on lookup that returns a value).
bool is_enabled(const std::string &node_name) const
True iff set_enabled(node_name, true) was the last call for it.
void clear()
Drop all cached entries (per-node enable/disable state preserved).
std::size_t size() const
Number of entries currently held.
Graph engine type definitions: channels, edges, nodes, events, and control flow.
NEOGRAPH_API std::string hash_state_for_cache(const json &state_value)
Stable hash over the JSON state used as the cache-key suffix.
Extended result returned by node execution.
Definition types.h:297