Skip to main content

Module cache

Module cache 

Source
Available on crate features graph and node-cache only.
Expand description

Node-level caching for graph execution.

This module provides a caching layer that stores node execution results keyed by a blake3 hash of the node name and its input state. Cached results are returned on subsequent executions with identical inputs, avoiding redundant computation.

§Backends

Two cache backends are supported:

  • InMemory (default) — an LRU cache with configurable maximum entries.
  • Redis (behind the redis-cache feature) — a Redis-backed cache using the fred client.

§Example

use adk_graph::cache::{CacheBackend, NodeCachePolicy, NodeCache, compute_cache_key};
use std::time::Duration;
use serde_json::json;
use std::collections::HashMap;

// Define a cache policy with in-memory backend
let policy = NodeCachePolicy {
    backend: CacheBackend::InMemory { max_entries: 128 },
    ttl: Some(Duration::from_secs(300)),
};

// Create a cache from the policy
let cache = NodeCache::from_policy(&policy);

// Compute a cache key from node name and input state
let mut state = HashMap::new();
state.insert("input".to_string(), json!("hello"));
let key = compute_cache_key("my_node", &state);

// Use the cache
assert!(cache.get(&key).await.is_none());
cache.set(&key, json!({"result": 42}), policy.ttl).await;
assert_eq!(cache.get(&key).await, Some(json!({"result": 42})));

Structs§

NodeCache
Node cache that stores and retrieves execution results.
NodeCachePolicy
Cache policy for a graph node.

Enums§

CacheBackend
Cache backend selection.

Functions§

compute_cache_key
Computes a deterministic cache key from a node name and its input state.