Skip to content

Commit eaac39e

Browse files
committed
fix: updated caching
1 parent 7961e6c commit eaac39e

File tree

9 files changed

+150
-676
lines changed

9 files changed

+150
-676
lines changed

sqlspec/base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
)
1616
from sqlspec.core.cache import (
1717
CacheConfig,
18-
CacheStatsAggregate,
1918
get_cache_config,
20-
get_cache_stats,
19+
get_cache_statistics,
2120
log_cache_stats,
2221
reset_cache_stats,
2322
update_cache_config,
@@ -532,13 +531,13 @@ def update_cache_config(config: CacheConfig) -> None:
532531
update_cache_config(config)
533532

534533
@staticmethod
535-
def get_cache_stats() -> CacheStatsAggregate:
534+
def get_cache_stats() -> "dict[str, Any]":
536535
"""Get current cache statistics.
537536
538537
Returns:
539538
Cache statistics object with detailed metrics.
540539
"""
541-
return get_cache_stats()
540+
return get_cache_statistics()
542541

543542
@staticmethod
544543
def reset_cache_stats() -> None:

sqlspec/builder/_base.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from sqlglot.optimizer import optimize
1414
from typing_extensions import Self
1515

16-
from sqlspec.core.cache import CacheKey, get_cache_config, get_default_cache
16+
from sqlspec.core.cache import get_cache, get_cache_config
1717
from sqlspec.core.hashing import hash_optimized_expression
1818
from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig
1919
from sqlspec.core.statement import SQL, StatementConfig
@@ -429,9 +429,8 @@ def _optimize_expression(self, expression: exp.Expression) -> exp.Expression:
429429
expression, dialect=dialect_name, schema=self.schema, optimizer_settings=optimizer_settings
430430
)
431431

432-
cache_key_obj = CacheKey((cache_key,))
433-
unified_cache = get_default_cache()
434-
cached_optimized = unified_cache.get(cache_key_obj)
432+
cache = get_cache()
433+
cached_optimized = cache.get("optimized", cache_key)
435434
if cached_optimized:
436435
return cast("exp.Expression", cached_optimized)
437436

@@ -440,7 +439,7 @@ def _optimize_expression(self, expression: exp.Expression) -> exp.Expression:
440439
expression, schema=self.schema, dialect=self.dialect_name, optimizer_settings=optimizer_settings
441440
)
442441

443-
unified_cache.put(cache_key_obj, optimized)
442+
cache.put("optimized", cache_key, optimized)
444443

445444
except Exception:
446445
return expression
@@ -461,15 +460,14 @@ def to_statement(self, config: "Optional[StatementConfig]" = None) -> "SQL":
461460
return self._to_statement(config)
462461

463462
cache_key_str = self._generate_builder_cache_key(config)
464-
cache_key = CacheKey((cache_key_str,))
465463

466-
unified_cache = get_default_cache()
467-
cached_sql = unified_cache.get(cache_key)
464+
cache = get_cache()
465+
cached_sql = cache.get("builder", cache_key_str)
468466
if cached_sql is not None:
469467
return cast("SQL", cached_sql)
470468

471469
sql_statement = self._to_statement(config)
472-
unified_cache.put(cache_key, sql_statement)
470+
cache.put("builder", cache_key_str, sql_statement)
473471

474472
return sql_statement
475473

sqlspec/core/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"""
9191

9292
from sqlspec.core import filters
93-
from sqlspec.core.cache import CacheConfig, CacheStats, UnifiedCache, get_statement_cache
93+
from sqlspec.core.cache import CacheConfig, CacheStats, MultiLevelCache, UnifiedCache, get_cache
9494
from sqlspec.core.compiler import OperationType, SQLProcessor
9595
from sqlspec.core.filters import StatementFilter
9696
from sqlspec.core.hashing import (
@@ -115,6 +115,7 @@
115115
"ArrowResult",
116116
"CacheConfig",
117117
"CacheStats",
118+
"MultiLevelCache",
118119
"OperationType",
119120
"ParameterConverter",
120121
"ParameterProcessor",
@@ -129,7 +130,7 @@
129130
"TypedParameter",
130131
"UnifiedCache",
131132
"filters",
132-
"get_statement_cache",
133+
"get_cache",
133134
"hash_expression",
134135
"hash_expression_node",
135136
"hash_optimized_expression",

0 commit comments

Comments
 (0)