Cache#
Thread-safe LRU caching system with configurable TTL, size limits, and namespace support. Used internally for statement caching, expression caching, and builder result caching.
CacheConfig#
- class sqlspec.core.cache.CacheConfig[source]#
Bases:
objectGlobal cache configuration for SQLSpec.
- __init__(*, compiled_cache_enabled=True, sql_cache_enabled=True, fragment_cache_enabled=True, optimized_cache_enabled=True, sql_cache_size=1000, fragment_cache_size=5000, optimized_cache_size=2000)[source]#
Initialize cache configuration.
- Parameters:
compiled_cache_enabled¶ (
bool) -- Master switch for namespaced caches and compiled SQL caching.sql_cache_enabled¶ (
bool) -- Enable statement and builder caching.fragment_cache_enabled¶ (
bool) -- Enable expression, parameter, and file caching.optimized_cache_enabled¶ (
bool) -- Enable optimized expression caching.sql_cache_size¶ (
int) -- Maximum statement/builder cache entries.fragment_cache_size¶ (
int) -- Maximum expression/parameter/file cache entries.optimized_cache_size¶ (
int) -- Maximum optimized cache entries.
LRUCache#
- class sqlspec.core.cache.LRUCache[source]#
Bases:
objectCache with LRU eviction and TTL support.
- Parameters:
NamespacedCache#
- class sqlspec.core.cache.NamespacedCache[source]#
Bases:
objectSingle cache with namespace isolation.
Uses per-namespace LRU caches sized by CacheConfig to keep memory usage predictable while avoiding stringly-typed cache access.
- delete_statement(key, dialect=None)[source]#
Delete cached statement data.
External/extension API: not called internally.
- delete_expression(key, dialect=None)[source]#
Delete cached expression data.
External/extension API: not called internally.
- delete_optimized(key, dialect=None)[source]#
Delete cached optimized expression data.
External/extension API: not called internally.
- delete_builder(key, dialect=None)[source]#
Delete cached builder statement data.
External/extension API: not called internally.
- delete_file(key, dialect=None)[source]#
Delete cached SQL file data.
External/extension API: not called internally.
CachedStatement#
- class sqlspec.core.cache.CachedStatement[source]#
Bases:
objectImmutable cached statement result.
This class stores compiled SQL and parameters in an immutable format that can be safely shared between different parts of the system without risk of mutation. List parameters are preserved for execute_many operations where drivers require list type.
Supporting Types#
- class sqlspec.core.cache.CacheKey[source]#
Bases:
objectImmutable cache key.
- Parameters:
key_data¶ (
tuple[typing.Any,...]) -- Tuple of hashable values that uniquely identify the cached item
- class sqlspec.core.cache.CacheStats[source]#
Bases:
objectCache statistics tracking.
Tracks cache metrics including hit rates, evictions, and memory usage.
- class sqlspec.core.cache.CacheNode[source]#
Bases:
objectInternal cache node for LRU linked list implementation.
- class sqlspec.core.cache.FiltersView[source]#
Bases:
objectRead-only view of filters without copying.
Provides zero-copy access to filters with methods for querying, iteration, and canonical representation generation.
Note
FiltersView and its to_canonical method operate on lightweight Filter records (field_name, operation, value) for cache key generation. This is distinct from StatementFilter canonicalization in sqlspec.core.filters.canonicalize_filters, which handles full SQL AST statement filter objects.
- get_by_field(field_name)[source]#
Get all filters for a specific field.
External/extension API: not called internally.
Module-Level Functions#
- sqlspec.core.cache.get_cache()[source]#
Get the namespaced cache instance.
- Return type:
- Returns:
Singleton namespaced cache instance
- sqlspec.core.cache.get_default_cache()[source]#
Get the default LRU cache instance.
- Return type:
- Returns:
Singleton default cache instance
- sqlspec.core.cache.get_cache_config()[source]#
Get the global cache configuration.
- Return type:
- Returns:
Current global cache configuration instance
- sqlspec.core.cache.update_cache_config(config)[source]#
Update the global cache configuration.
Clears all existing caches when configuration changes.
- Parameters:
config¶ (
CacheConfig) -- New cache configuration to apply globally- Return type:
- sqlspec.core.cache.get_cache_statistics()[source]#
Get statistics from all cache instances.
- Return type:
- Returns:
Dictionary mapping cache type to statistics
- sqlspec.core.cache.create_cache_key(namespace, key, dialect=None)[source]#
Create optimized cache key using string concatenation.