11__all__ = ["ServiceCache" ]
22
33import sqlite3
4+ from collections .abc import Generator
5+ from contextlib import contextmanager
46from dataclasses import dataclass
57from datetime import datetime
68
@@ -20,11 +22,17 @@ def __init__(self, service: str) -> None:
2022 self ._db_path = get_cache_root () / f"{ service } .sqlite"
2123 self .initialize ()
2224
23- def _connect (self ) -> sqlite3 .Connection :
24- conn = sqlite3 .connect (self ._db_path )
25- conn .row_factory = sqlite3 .Row
26- conn .execute ("PRAGMA foreign_keys = ON" )
27- return conn
25+ @contextmanager
26+ def _connect (self ) -> Generator [sqlite3 .Connection ]:
27+ conn = None
28+ try :
29+ conn = sqlite3 .connect (self ._db_path )
30+ conn .row_factory = sqlite3 .Row
31+ conn .execute ("PRAGMA foreign_keys = ON" )
32+ yield conn
33+ finally :
34+ if conn :
35+ conn .close ()
2836
2937 def initialize (self ) -> None :
3038 with self ._connect () as conn :
@@ -74,3 +82,9 @@ def insert(
7482 """ ,
7583 (str (object_id ), str (file_type ), creator , set_id , last_updated .isoformat ()),
7684 )
85+
86+ def delete (self , object_id : int | str , file_type : FileType ) -> None :
87+ with self ._connect () as conn :
88+ conn .execute (
89+ "DELETE FROM cache WHERE id = ? AND type = ?;" , (str (object_id ), str (file_type ))
90+ )
0 commit comments