NeoGraph 0.16.0
A C++20 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 <cstdint>
12#include <functional>
13#include <mutex>
14#include <optional>
15#include <set>
16#include <string>
17#include <unordered_map>
18
19namespace neograph::graph {
20
21struct RunContext;
22
31enum class CacheScope {
32 Execution,
33 Reusable,
34};
35
46 CacheScope scope = CacheScope::Execution;
47 std::function<std::string(const RunContext&)> context_key;
48};
49
75class NEOGRAPH_API NodeCache {
76public:
77 NodeCache() = default;
78
80 void set_enabled(const std::string& node_name, bool enabled);
81
83 bool is_enabled(const std::string& node_name) const;
84
86 void set_policy(const std::string& node_name, CacheKeyPolicy policy);
87
89 CacheKeyPolicy policy_for(const std::string& node_name) const;
90
92 std::optional<NodeResult> lookup(const std::string& node_name,
93 const std::string& state_hash) const;
94
96 std::optional<NodeResult> lookup(const std::string& node_name,
97 const std::string& state_hash,
98 const std::string& scope_key) const;
99
101 void store(const std::string& node_name,
102 const std::string& state_hash,
103 NodeResult result);
104
106 void store(const std::string& node_name,
107 const std::string& state_hash,
108 const std::string& scope_key,
109 NodeResult result);
110
112 void clear();
113
116 void set_max_entries(std::size_t max_entries);
117
119 std::size_t max_entries() const;
120
122 std::size_t size() const;
123
125 std::size_t hit_count() const;
128 std::size_t miss_count() const;
130 std::size_t eviction_count() const;
131
132private:
133 struct CacheEntry {
134 NodeResult result;
135 std::uint64_t last_used = 0;
136 };
137
138 static std::string make_key(const std::string& node_name,
139 const std::string& state_hash,
140 const std::string& scope_key);
141 void evict_one_locked();
142
143 mutable std::mutex mu_;
144 std::set<std::string> enabled_nodes_;
145 std::unordered_map<std::string, CacheKeyPolicy> policies_;
146 mutable std::unordered_map<std::string, CacheEntry> entries_;
147 mutable std::size_t hits_ = 0;
148 mutable std::size_t misses_ = 0;
149 std::size_t max_entries_ = 0;
150 std::size_t evictions_ = 0;
151 mutable std::uint64_t access_sequence_ = 0;
152};
153
157NEOGRAPH_API std::string hash_state_for_cache(const json& state_value);
158
159} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Per-node result cache.
Definition node_cache.h:75
std::size_t max_entries() const
Configured global entry bound. Zero means unbounded.
void set_max_entries(std::size_t max_entries)
Set the global entry bound.
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 std::string &scope_key) const
Lookup with the execution or caller-declared reusable scope key.
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).
std::size_t eviction_count() const
Lifetime capacity-eviction count. clear() and overwrite do not count.
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).
void store(const std::string &node_name, const std::string &state_hash, const std::string &scope_key, NodeResult result)
Store with the execution or caller-declared reusable scope key.
std::size_t hit_count() const
Lifetime hit count (incremented on lookup that returns a value).
CacheKeyPolicy policy_for(const std::string &node_name) const
Return a node's policy, or the safe execution-local default.
bool is_enabled(const std::string &node_name) const
True iff set_enabled(node_name, true) was the last call for it.
void set_policy(const std::string &node_name, CacheKeyPolicy policy)
Set a node's cache identity policy. The default is execution-local.
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.
CacheScope
Lifetime boundary for a cached node result.
Definition node_cache.h:31
Explicit cache identity policy for one node.
Definition node_cache.h:45
Extended result returned by node execution.
Definition types.h:902
Per-run dispatch metadata threaded through the engine and executor.
Definition run_context.h:71