Skip to content

OSSS.ai.orchestration.graph_cache

OSSS.ai.orchestration.graph_cache

Graph compilation caching for OSSS LangGraph backend.

This module provides caching functionality for compiled StateGraphs to improve performance by avoiding repeated graph compilation for the same configurations.

CacheConfig dataclass

Configuration for graph caching.

CacheEntry dataclass

Entry in the graph cache.

is_expired(ttl_seconds)

Check if this cache entry has expired.

touch()

Update last accessed time and increment access count.

CacheStats dataclass

Cache statistics.

hit_rate property

Calculate cache hit rate.

total_requests property

Total cache requests.

GraphCache

LRU cache for compiled StateGraphs with TTL and size management.

This cache stores compiled graphs to avoid repeated compilation overhead. It supports: - LRU eviction policy - TTL-based expiration - Size-based eviction - Thread-safe operations - Performance statistics

__init__(config=None)

Initialize the graph cache.

Parameters

config : CacheConfig, optional Cache configuration. If None, default config is used.

make_cache_key_from_config(cfg)

Build a cache key from a GraphConfig-like object OR a dict.

This prevents failures like

'dict' object has no attribute 'agents_to_run'

get_cached_graph(pattern_name, agents, checkpoints_enabled, version=None, *, compile_variant=DEFAULT_COMPILE_VARIANT)

Retrieve a cached compiled graph.

cache_graph(pattern_name, agents, checkpoints_enabled, compiled_graph, version=None, *, compile_variant=DEFAULT_COMPILE_VARIANT)

Cache a compiled graph.

get_cached_graph_for_config(cfg)

Retrieve cached graph using a GraphConfig-like object OR dict.

This is the safest entry point for callers that currently have a config object.

cache_graph_for_config(cfg, compiled_graph)

Cache graph using a GraphConfig-like object OR dict.

clear()

Clear all cached graphs.

get_stats()

Get cache statistics.

get_cache_keys()

Get all current cache keys.

remove_pattern(pattern_name, version=None)

Remove cached graphs for a specific pattern.

  • If version is provided: remove only that version for the pattern.
  • If version is None: remove all versions (and legacy keys) for the pattern.

optimize()

Optimize the cache by removing expired entries and defragmenting.

Returns

Dict[str, int] Optimization statistics