Caching¶
Classes for caching loaded configurations.
CachedLoader¶
Loader wrapper that caches results.
CachedLoader
¶
CachedLoader(
loader: Any,
cache: CacheBackend[
DaprAgentConfig | WorkflowDefinition
]
| None = None,
*,
default_ttl: float | None = 300.0,
)
A caching wrapper around DaprAgentSpecLoader.
Provides transparent caching for loaded specifications to avoid reparsing the same content multiple times.
Example
from dapr_agents_oas_adapter import DaprAgentSpecLoader
from dapr_agents_oas_adapter.cache import CachedLoader, InMemoryCache
# Create a cached loader with 5-minute TTL
cache = InMemoryCache(default_ttl=300)
loader = CachedLoader(DaprAgentSpecLoader(), cache)
# First call parses the JSON
config1 = loader.load_json(json_string)
# Second call returns a cached result
config2 = loader.load_json(json_string)
Initialize the cached loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
Any
|
The underlying DaprAgentSpecLoader instance |
required |
cache
|
CacheBackend[DaprAgentConfig | WorkflowDefinition] | None
|
Cache backend to use (creates InMemoryCache if None) |
None
|
default_ttl
|
float | None
|
Default TTL for cached entries in seconds |
300.0
|
Source code in src/dapr_agents_oas_adapter/cache.py
Attributes¶
Functions¶
load_yaml
¶
Load an OAS specification from an YAML string with caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_content
|
str
|
YAML string containing the OAS specification |
required |
use_cache
|
bool
|
Whether to use caching (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Source code in src/dapr_agents_oas_adapter/cache.py
load_json
¶
Load an OAS specification from a JSON string with caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_content
|
str
|
JSON string containing the OAS specification |
required |
use_cache
|
bool
|
Whether to use caching (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Source code in src/dapr_agents_oas_adapter/cache.py
load_dict
¶
load_dict(
spec_dict: dict[str, Any], *, use_cache: bool = True
) -> DaprAgentConfig | WorkflowDefinition
Load an OAS specification from a dictionary with caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_dict
|
dict[str, Any]
|
Dictionary containing the OAS specification |
required |
use_cache
|
bool
|
Whether to use caching (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Source code in src/dapr_agents_oas_adapter/cache.py
InMemoryCache¶
Thread-safe in-memory cache with TTL support.
InMemoryCache
¶
Bases: CacheBackend[T]
Thread-safe in-memory cache with TTL support.
Initialize the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_ttl
|
float | None
|
Default time-to-live in seconds (None = no expiration) |
None
|
max_size
|
int | None
|
Maximum number of entries (None = unlimited) |
None
|
Source code in src/dapr_agents_oas_adapter/cache.py
Functions¶
get
¶
Get a value from the cache.
Source code in src/dapr_agents_oas_adapter/cache.py
set
¶
Set a value in the cache.
Source code in src/dapr_agents_oas_adapter/cache.py
delete
¶
Delete a value from the cache.
clear
¶
Clear all entries from the cache.
CacheBackend¶
Abstract base class for cache implementations.
CacheBackend
¶
Bases: ABC
Abstract cache backend interface.
Functions¶
get
abstractmethod
¶
Get a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
The cached value or None if not found/expired |
set
abstractmethod
¶
Set a value in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
value
|
T
|
The value to cache |
required |
ttl
|
float | None
|
Time-to-live in seconds (None = no expiration) |
None
|
Source code in src/dapr_agents_oas_adapter/cache.py
delete
abstractmethod
¶
Delete a value from the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The cache key |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the key was deleted, False if not found |
clear
abstractmethod
¶
CacheStats¶
Cache statistics tracking.
CacheStats
dataclass
¶
Usage Examples¶
Basic Caching¶
from dapr_agents_oas_adapter import (
CachedLoader,
DaprAgentSpecLoader,
InMemoryCache
)
# Create cache
cache = InMemoryCache(
max_size=100,
ttl_seconds=300
)
# Create cached loader
loader = CachedLoader(
loader=DaprAgentSpecLoader(),
cache=cache
)
# Load (cache miss)
config = loader.load_yaml(yaml_content)
# Load again (cache hit)
config = loader.load_yaml(yaml_content)
# Check stats
print(f"Hits: {loader.stats.hits}")
print(f"Misses: {loader.stats.misses}")
Custom Cache Backend¶
from dapr_agents_oas_adapter import CacheBackend
from typing import TypeVar
T = TypeVar("T")
class MyCache(CacheBackend[T]):
def get(self, key: str) -> T | None:
# Custom get implementation
pass
def set(self, key: str, value: T) -> None:
# Custom set implementation
pass
def delete(self, key: str) -> bool:
# Custom delete implementation
pass
def clear(self) -> None:
# Custom clear implementation
pass
Related¶
- DaprAgentSpecLoader - Base loader
- Loading Guide - Usage guide