/
zimaaadev
/
Parser
Обзор
Документация
Войти
/
zimaaadev
/
Parser
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
utils/cache.py
37 строк
1 KB
zimaaadev
Ищет природные объекты, набнрежные и т.п.
24 дек 2025, 17:18
24 дек 2025, 17:18
2d73bc2
Код
Авторство
О чём код?
# utils/cache.py import aiosqlite import json import time class AsyncSQLiteCache: def __init__(self, db_path: str): self.db_path = db_path async def init(self): async with aiosqlite.connect(self.db_path) as db: await db.execute(""" CREATE TABLE IF NOT EXISTS cache ( key TEXT PRIMARY KEY, value TEXT NOT NULL, expires_at REAL NOT NULL ) """) await db.commit() async def get(self, key: str): async with aiosqlite.connect(self.db_path) as db: async with db.execute( "SELECT value FROM cache WHERE key = ? AND expires_at > ?", (key, time.time()) ) as cursor: row = await cursor.fetchone() return json.loads(row[0]) if row else None async def set(self, key: str, value, ttl: int = 3600): expires_at = time.time() + ttl async with aiosqlite.connect(self.db_path) as db: await db.execute( "INSERT OR REPLACE INTO cache (key, value, expires_at) VALUES (?, ?, ?)", (key, json.dumps(value), expires_at) ) await db.commit()