Spaces:
Running
Running
| # ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ํค์๋+๋ ์ง ๋จ์๋ก ์บ์ฑํ๋ SQLite ๋ํผ (์ฌ์กฐํ ์ ์ฆ์ ๋ฐํ) | |
| import os | |
| import json | |
| import sqlite3 | |
| from datetime import date | |
| DB_PATH = os.path.join(os.path.dirname(__file__), "cache.db") | |
| def _conn(): | |
| return sqlite3.connect(DB_PATH) | |
| def init(): | |
| # ์บ์ ํ ์ด๋ธ ์์ฑ (์์ผ๋ฉด) | |
| with _conn() as c: | |
| c.execute( | |
| """ | |
| create table if not exists analysis_cache ( | |
| keyword text not null, | |
| collected_date text not null, | |
| payload text not null, | |
| primary key (keyword, collected_date) | |
| ) | |
| """ | |
| ) | |
| def get(keyword: str, day: str | None = None) -> dict | None: | |
| # ๊ฐ์ ๋ ์ง์ ์บ์๊ฐ ์์ผ๋ฉด ๋ฐํ, ์์ผ๋ฉด None | |
| day = day or date.today().isoformat() | |
| with _conn() as c: | |
| row = c.execute( | |
| "select payload from analysis_cache where keyword=? and collected_date=?", | |
| (keyword, day), | |
| ).fetchone() | |
| return json.loads(row[0]) if row else None | |
| def put(keyword: str, payload: dict, day: str | None = None): | |
| # ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ (๊ฐ์ ํค์๋+๋ ์ง๋ ๋ฎ์ด์) | |
| day = day or date.today().isoformat() | |
| with _conn() as c: | |
| c.execute( | |
| "insert or replace into analysis_cache values (?, ?, ?)", | |
| (keyword, day, json.dumps(payload, ensure_ascii=False)), | |
| ) | |