Skip to content

API Reference parrot.bots.database.cache

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.bots.database.cache

View source

Multi-database cache with partitioned namespaces.

Replaces the monolithic SchemaMetadataCache with a CacheManager that creates CachePartition instances per database. Each partition has its own LRU sizing and TTL while optionally sharing a Redis connection pool and a vector store for similarity search.

Alias AbstractStore

Re-exported from parrot.stores.abstract.AbstractStore.

Class CacheManager

Manages namespaced cache partitions with shared Redis + vector store.

Parameter Type Default Description
redis_url Optional[str] None Optional Redis connection string. None for LRU-only mode.
vector_store Optional['AbstractStore'] None Optional AbstractStore for similarity search.
owns_vector_store bool True When True (default) :meth:close disposes the vector_store (releasing its connection pool / SQLAlchemy engine). Set to False when the store is shared and its lifecycle is managed elsewhere.

Method CacheManager.create_partition

Create a new cache partition with the given configuration.

create_partition(config: CachePartitionConfig) -> CachePartition

Parameter Type Default Description
config CachePartitionConfig Partition configuration.

Returns: CachePartition — The newly created CachePartition.

Raises:

  • ValueError — If a partition with the same namespace already exists.

Method CacheManager.get_partition

Return the partition for namespace, or None.

get_partition(namespace: str) -> Optional[CachePartition]

Parameter Type Default Description
namespace str

Returns: Optional[CachePartition]

Method CacheManager.search_across_databases

Search for tables across all partitions.

search_across_databases(query: str, limit: int = 5) -> List[TableMetadata]

Parameter Type Default Description
query str Natural-language search string.
limit int 5 Maximum total results.

Returns: List[TableMetadata] — Merged list of TableMetadata from every partition.

Method CacheManager.close

Close shared resources (Redis pool + owned vector store).

Disposing the vector_store here is what prevents its SQLAlchemy async engine (e.g. PgvectorStore) from lingering until interpreter teardown, where the event loop is already closing and the pool's connection.close() raises CancelledError. Skipped when owns_vector_store is False.

close() -> None

Class CachePartition

Namespaced cache partition with the same API as SchemaMetadataCache.

Each partition owns:

  • An independent TTLCache (LRU tier)
  • A schema-level cache (Dict[str, SchemaMetadata])
  • Access statistics for hot-table tracking

Optionally uses a shared Redis pool and vector store passed by the CacheManager.

Parameter Type Default Description
namespace str
lru_maxsize int 500
lru_ttl int 1800
redis_ttl int 3600
redis_pool Any None
vector_store Optional['AbstractStore'] None
ttl_by_completeness Optional[Dict[int, int]] None
plane Optional['SchemaPlaneReader'] None
plane_write bool False
origin Optional[str] None
dialect Optional[str] None

Attributes:

Attribute Type Description
ttl_by_completeness Dict[int, int]
hot_cache TTLCache
schema_cache Dict[str, SchemaMetadata]
table_access_stats Dict[str, int]

Method CachePartition.get

Return metadata only when completeness and freshness requirements are met.

Resolution order: LRU → schema cache → Redis → vector store. Returns None when:

  • entry not found in any tier
  • entry.completeness < required
  • now - entry.loaded_at > effective_max_age (effective_max_age = max_age if provided else ttl_by_completeness[completeness])

get(schema_name: str, table_name: str, *, required: Completeness = Completeness.NAME_ONLY, max_age: Optional[timedelta] = None) -> Optional[TableMetadata]

Parameter Type Default Description
schema_name str
table_name str
required Completeness Completeness.NAME_ONLY
max_age Optional[timedelta] None

Returns: Optional[TableMetadata]

Method CachePartition.get_table_metadata

Deprecated — use get() instead.

get_table_metadata(schema_name: str, table_name: str) -> Optional[TableMetadata]

Parameter Type Default Description
schema_name str
table_name str

Returns: Optional[TableMetadata]

Method CachePartition.store_table_metadata

Store table metadata across all available tiers.

store_table_metadata(metadata: TableMetadata) -> None

Parameter Type Default Description
metadata TableMetadata

Method CachePartition.list

Return all cached tables in schema_names filtered by completeness and age.

list(schema_names: List[str], *, completeness_min: Completeness = Completeness.NAME_ONLY, max_age: Optional[timedelta] = None, limit: Optional[int] = None) -> List[TableMetadata]

Parameter Type Default Description
schema_names List[str]
completeness_min Completeness Completeness.NAME_ONLY
max_age Optional[timedelta] None
limit Optional[int] None

Returns: List[TableMetadata]

Method CachePartition.search

Search for tables within schema_names filtered by completeness and age.

Vector candidates are filtered post-hoc via get(). Cache-only path applies the same gates directly from schema_cache to avoid Redis RTTs.

search(schema_names: List[str], search_term: str, *, completeness_min: Completeness = Completeness.NAME_ONLY, max_age: Optional[timedelta] = None, limit: int = 20) -> List[TableMetadata]

Parameter Type Default Description
schema_names List[str]
search_term str
completeness_min Completeness Completeness.NAME_ONLY
max_age Optional[timedelta] None
limit int 20

Returns: List[TableMetadata]

Method CachePartition.search_similar_tables

Deprecated — use search() instead.

search_similar_tables(schema_names: List[str], query: str, limit: int = 5) -> List[TableMetadata]

Parameter Type Default Description
schema_names List[str]
query str
limit int 5

Returns: List[TableMetadata]

Method CachePartition.get_schema_overview

Get complete schema overview.

get_schema_overview(schema_name: str) -> Optional[SchemaMetadata]

Parameter Type Default Description
schema_name str

Returns: Optional[SchemaMetadata]

Method CachePartition.get_hot_tables

Get most frequently accessed tables across allowed schemas.

get_hot_tables(schema_names: List[str], limit: int = 10) -> List[tuple[str, str, int]]

Parameter Type Default Description
schema_names List[str]
limit int 10

Returns: List[tuple[str, str, int]]

Class CachePartitionConfig

Configuration for a single cache partition.

Fields:

Field Type Default Description
namespace str Unique identifier for this partition
lru_maxsize int 500 Max items in LRU cache
lru_ttl int 1800 LRU TTL in seconds
redis_ttl int 3600 Redis TTL in seconds
ttl_by_completeness Dict[int, int] Per-completeness TTL cap in seconds (keyed by Completeness int value)

Alias Completeness

Re-exported from parrot.bots.database.models.Completeness.

Alias SchemaMetadata

Re-exported from parrot.bots.database.models.SchemaMetadata.

Class SchemaMetadataCache

Backward-compatible wrapper around CachePartition.

Preserves the old constructor signature::

SchemaMetadataCache(vector_store=None, lru_maxsize=500, lru_ttl=1800)

so that existing code (e.g. abstract.py) continues to work until the cleanup task (TASK-579) removes it.

Parameter Type Default Description
vector_store Optional['AbstractStore'] None
lru_maxsize int 500
lru_ttl int 1800

Alias SchemaPlaneReader

Re-exported from parrot.knowledge.wiki.schema.service.SchemaPlaneReader.

Alias TableMetadata

Re-exported from parrot.bots.database.models.TableMetadata.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally