Spaces:
Sleeping
Sleeping
| """๋ฆฌ๋๋ณด๋ DataFrame ์ ์ญ ์บ์ + ํ์ผ ๊ธฐ๋ฐ ์๊ทธ๋. | |
| ๋งค ํ์ด์ง ๋ก๋๋ง๋ค Google Sheets ๋ฅผ ๋ค์ ์น๋ฉด ๋๋ฆฌ๊ณ 429(rate limit) ์ํ์ด | |
| ์์ผ๋ฏ๋ก, ์ต์ด 1ํ๋ง fetch ํด์ ๋ฉ๋ชจ๋ฆฌ์ ์บ์ํ๋ค. Refresh ๋ฒํผ์ ๋๋ฅด๋ฉด | |
| invalidate_cache() ๋ก ๊ฐ์ ์ฌfetch. ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ์๊ทธ๋ ํ์ผ์ ์ต์ | |
| ํ์์คํฌํ๋ฅผ ๊ธฐ๋กํ๋ฉด refresh_if_signal_newer() ๊ฐ ์ด๋ฅผ ๊ฐ์งํด ๊ฐฑ์ ํ๋ค. | |
| (์๋ณธ ๋ ํฌ์ DB ์ฝ๊ธฐ ๋ถ๊ธฐ๋ ์ด Space ์์ ๋ถํ์ํ๋ฏ๋ก ์ ๊ฑฐ โ Google Sheets ์ง์ ์ฝ๊ธฐ๋ง ๋จ๊น) | |
| """ | |
| import os | |
| import threading | |
| import time | |
| from sheet_manager.sheet_loader.sheet2df import sheet2df | |
| _lock = threading.Lock() | |
| _cached_df = None | |
| _cache_timestamp = 0.0 | |
| SIGNAL_FILE = "/tmp/pia_leaderboard_cache_signal" | |
| def get_cached_df(): | |
| """์บ์๋ DataFrame ๋ฐํ. ์ต์ด ํธ์ถ ์ fetch.""" | |
| global _cached_df, _cache_timestamp | |
| with _lock: | |
| if _cached_df is None: | |
| _cached_df = sheet2df() | |
| _cache_timestamp = time.time() | |
| return _cached_df.copy() | |
| def invalidate_cache(): | |
| """์บ์๋ฅผ ์ฆ์ ๊ฐฑ์ (Google Sheets ์์ ๋ค์ ์ฝ๊ธฐ).""" | |
| global _cached_df, _cache_timestamp | |
| new_df = sheet2df() | |
| with _lock: | |
| _cached_df = new_df | |
| _cache_timestamp = time.time() | |
| def refresh_if_signal_newer(): | |
| """์๊ทธ๋ ํ์ผ์ด ์บ์๋ณด๋ค ์ต์ ์ด๋ฉด ๊ฐฑ์ ํ ๋ฐํ. ์๋๋ฉด ์บ์ ๊ทธ๋๋ก.""" | |
| try: | |
| if os.path.exists(SIGNAL_FILE): | |
| with open(SIGNAL_FILE) as f: | |
| signal_ts = float(f.read().strip()) | |
| if signal_ts > _cache_timestamp: | |
| invalidate_cache() | |
| except (ValueError, OSError): | |
| pass | |
| return get_cached_df() | |