Spaces:
Sleeping
Sleeping
Update app/core/cache.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940"""Cache Management for Pegos Data API-----------------------------------Dataset cache ve zaman yönetimi."""import timefrom datetime import datetime, timezoneimport pandas as pdfrom huggingface_hub import hf_hub_download_CACHE = {"df": None, "ts": 0.0}_CACHE_TTL = 180.0 # 3 dakika cachedef last_update_iso() -> str:"""Cache zamanını ISO formatında döndürür."""ts = _CACHE.get("ts", 0.0)if ts <= 0:return datetime(1970, 1, 1, tzinfo=timezone.utc).isoformat()return datetime.fromtimestamp(ts, tz=timezone.utc).isoformat()def load_dataset(repo_id: str, filename: str, token: str) -> pd.DataFrame:"""Dataset'i Hugging Face'ten indirir veya cache'ten döndürür."""now = time.time()if _CACHE["df"] is not None and (now - _CACHE["ts"] < _CACHE_TTL):return _CACHE["df"]local_path = hf_hub_download(repo_id=repo_id,filename=filename,repo_type="dataset",token=token,force_download=True,)df = pd.read_csv(local_path)_CACHE["df"], _CACHE["ts"] = df, nowprint(f"[INFO] Dataset sync ✅ {len(df)} rows @ {last_update_iso()}")return df123456789101112131415161718192021222324252627# =====================================================# app/core/cache.py# RAM tabanlı basit cache yönetimi# =====================================================import timefrom app.core.config import settings_cache = {"data": None, "timestamp": 0.0}def get_cache():"""Cache'te bulunan veri geçerliyse döndürür."""now = time.time()if _cache["data"] is not None and (now - _cache["timestamp"] < settings.CACHE_TTL):return _cache["data"]return Nonedef set_cache(data):"""Yeni veriyi cache'e kaydeder."""_cache["data"] = data_cache["timestamp"] = time.time()def get_cache_age():"""Cache’in kaç saniyedir geçerli olduğunu döndürür."""if _cache["timestamp"] == 0:return Nonereturn round(time.time() - _cache["timestamp"], 2)
app/core/cache.py+21-34