33
44This example shows how to:
55- Use PydanticAdapter for type-safe price data storage
6- - Apply CompressionWrapper for efficient historical data storage
6+ - Use LoggingWrapper for observability and debugging
77- Use PassthroughCacheWrapper for multi-tier caching (memory + disk)
88- Use RetryWrapper for handling transient failures
99- Use StatisticsWrapper to track cache hit/miss metrics
1717from key_value .aio .adapters .pydantic import PydanticAdapter
1818from key_value .aio .stores .disk .store import DiskStore
1919from key_value .aio .stores .memory .store import MemoryStore
20- from key_value .aio .wrappers .compression .wrapper import CompressionWrapper
20+ from key_value .aio .wrappers .logging .wrapper import LoggingWrapper
2121from key_value .aio .wrappers .passthrough_cache .wrapper import PassthroughCacheWrapper
2222from key_value .aio .wrappers .retry .wrapper import RetryWrapper
2323from key_value .aio .wrappers .statistics .wrapper import StatisticsWrapper
2424from pydantic import BaseModel
2525
26- # Configure logging
27- logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
28-
2926
3027class PriceData (BaseModel ):
3128 """Trading price data for a symbol."""
@@ -38,11 +35,11 @@ class PriceData(BaseModel):
3835
3936class TradingDataCache :
4037 """
41- Trading data cache with multi-tier caching and compression .
38+ Trading data cache with multi-tier caching and logging .
4239
4340 Uses a memory cache for fast access to recent data, with disk-backed
44- persistence for historical data. Compression reduces storage requirements
45- for large datasets .
41+ persistence for historical data. Logging provides observability into
42+ cache operations .
4643 """
4744
4845 def __init__ (self , cache_dir : str = ".trading_cache" ):
@@ -52,19 +49,19 @@ def __init__(self, cache_dir: str = ".trading_cache"):
5249 # Tier 1: Memory cache for fast access
5350 memory_cache = MemoryStore ()
5451
55- # Tier 2: Disk cache with compression for historical data
52+ # Tier 2: Disk cache for historical data
5653 disk_cache = DiskStore (root_directory = cache_dir )
5754
5855 # Wrapper stack (applied inside-out):
5956 # 1. StatisticsWrapper - Track cache metrics
6057 # 2. RetryWrapper - Handle transient failures (3 retries with exponential backoff)
61- # 3. CompressionWrapper - Compress data before storage
58+ # 3. LoggingWrapper - Log operations for debugging
6259 # 4. PassthroughCacheWrapper - Two-tier caching (memory → disk)
63- disk_with_compression = CompressionWrapper (
64- key_value = RetryWrapper (key_value = StatisticsWrapper ( key_value = disk_cache ) , max_retries = 3 , base_delay = 0.1 )
65- )
60+ stats = StatisticsWrapper ( key_value = disk_cache )
61+ retry_wrapper = RetryWrapper (key_value = stats , max_retries = 3 , base_delay = 0.1 )
62+ disk_with_logging = LoggingWrapper ( key_value = retry_wrapper )
6663
67- cache_store = PassthroughCacheWrapper (cache = memory_cache , key_value = disk_with_compression )
64+ cache_store = PassthroughCacheWrapper (cache = memory_cache , key_value = disk_with_logging )
6865
6966 # PydanticAdapter for type-safe price data storage/retrieval
7067 self .adapter : PydanticAdapter [PriceData ] = PydanticAdapter [PriceData ](
@@ -73,7 +70,7 @@ def __init__(self, cache_dir: str = ".trading_cache"):
7370 )
7471
7572 # Store reference to statistics wrapper for metrics
76- self .stats_wrapper = disk_with_compression . key_value . key_value # type: ignore[attr-defined]
73+ self .stats_wrapper = stats
7774 self .cache_dir = cache_dir
7875
7976 async def store_price (self , symbol : str , price : float , volume : int , ttl : int | None = None ) -> str :
@@ -170,6 +167,9 @@ async def cleanup(self):
170167
171168async def main ():
172169 """Demonstrate the trading data cache."""
170+ # Configure logging for the demo
171+ logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" )
172+
173173 cache = TradingDataCache (cache_dir = ".demo_trading_cache" )
174174
175175 try :
0 commit comments