Files
learn-trading/persistent_cache.py
T

35 lines
1.1 KiB
Python
Raw Normal View History

"""
Use the persistent defeatbeta httpfs cache at ~/.cache/defeatbeta/.
Import this at the top of any notebook or script before using Ticker().
Run warmup_cache.py once first to populate the cache.
from persistent_cache import enable_persistent_cache
enable_persistent_cache()
from defeatbeta_api.data.ticker import Ticker
t = Ticker("AAPL")
"""
from pathlib import Path
CACHE_DIR = Path.home() / ".cache" / "defeatbeta"
def enable_persistent_cache() -> None:
import defeatbeta_api.utils.util as _util
import defeatbeta_api.client.duckdb_client as _duckdb_mod
from defeatbeta_api.client.duckdb_conf import Configuration
if not CACHE_DIR.exists():
raise RuntimeError(
f"Cache directory not found: {CACHE_DIR}\n"
"Run warmup_cache.py first to populate it."
)
# Redirect cache directory to the persistent location
_util.validate_httpfs_cache_directory = lambda: str(CACHE_DIR)
# Reset singleton so DuckDB reinitialises pointing at the new cache dir
_duckdb_mod._instance = None
print(f"[persistent_cache] cache → {CACHE_DIR}")