Add disk-backed SQLite cache mode as an alternative to in-memory

This commit is contained in:
Jan Doubravský
2026-06-08 11:39:04 +02:00
parent 757a8f4eba
commit 209ae667ab
10 changed files with 280 additions and 67 deletions
+47
View File
@@ -58,3 +58,50 @@ def test_backup_and_reload(tmp_path, source_conn):
c2 = CacheManager(db_path=db_path, backup_interval=9999)
assert c2.is_table_cached("users") is True
c2.close()
# ---------------------------------------------------------------------------
# Disk-backed mode (in_memory=False)
# ---------------------------------------------------------------------------
def test_disk_mode_persists_without_backup(tmp_path, source_conn):
"""Disk mode writes straight to the file — no explicit backup/close needed."""
db_path = tmp_path / "cache.db"
c = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
c.load_table("users", ["name"], source_conn)
# Data is already on disk; a brand-new disk-mode manager sees it.
c2 = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
assert c2.is_table_cached("users") is True
c2.close()
c.close()
def test_disk_mode_file_created_immediately(tmp_path, source_conn):
db_path = tmp_path / "cache.db"
c = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
c.load_table("users", ["name"], source_conn)
assert db_path.exists()
c.close()
def test_disk_mode_reload_in_new_instance(tmp_path, source_conn):
db_path = tmp_path / "cache.db"
c1 = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
c1.load_table("users", ["name", "email"], source_conn)
c1.close()
c2 = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
rows = c2.connection.execute("SELECT name FROM users").fetchall()
assert {r[0] for r in rows} == {"alice", "bob"}
c2.close()
def test_disk_mode_reset_keeps_file(tmp_path, source_conn):
db_path = tmp_path / "cache.db"
c = CacheManager(db_path=db_path, backup_interval=9999, in_memory=False)
c.load_table("users", ["name"], source_conn)
c.reset()
# File stays (the connection is still open) but the table is gone.
assert db_path.exists()
assert c.is_table_cached("users") is False
c.close()