Move geo cache commit handling into repository layer
This commit is contained in:
@@ -104,6 +104,19 @@ async def upsert_entry(
|
||||
)
|
||||
|
||||
|
||||
async def upsert_entry_and_commit(
|
||||
db: aiosqlite.Connection,
|
||||
ip: str,
|
||||
country_code: str | None,
|
||||
country_name: str | None,
|
||||
asn: str | None,
|
||||
org: str | None,
|
||||
) -> None:
|
||||
"""Insert or update a resolved geo cache entry and commit."""
|
||||
await upsert_entry(db, ip, country_code, country_name, asn, org)
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def upsert_neg_entry(db: aiosqlite.Connection, ip: str) -> None:
|
||||
"""Record a failed lookup attempt as a negative entry."""
|
||||
await db.execute(
|
||||
@@ -112,6 +125,12 @@ async def upsert_neg_entry(db: aiosqlite.Connection, ip: str) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def upsert_neg_entry_and_commit(db: aiosqlite.Connection, ip: str) -> None:
|
||||
"""Record a failed lookup attempt and commit the transaction."""
|
||||
await upsert_neg_entry(db, ip)
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def bulk_upsert_entries(
|
||||
db: aiosqlite.Connection,
|
||||
rows: Sequence[tuple[str, str | None, str | None, str | None, str | None]],
|
||||
@@ -146,3 +165,40 @@ async def bulk_upsert_neg_entries(db: aiosqlite.Connection, ips: list[str]) -> i
|
||||
[(ip,) for ip in ips],
|
||||
)
|
||||
return len(ips)
|
||||
|
||||
|
||||
async def bulk_upsert_entries_and_commit(
|
||||
db: aiosqlite.Connection,
|
||||
rows: Sequence[tuple[str, str | None, str | None, str | None, str | None]],
|
||||
) -> int:
|
||||
"""Bulk insert or update multiple geo cache entries and commit."""
|
||||
count = await bulk_upsert_entries(db, rows)
|
||||
await db.commit()
|
||||
return count
|
||||
|
||||
|
||||
async def bulk_upsert_neg_entries_and_commit(db: aiosqlite.Connection, ips: list[str]) -> int:
|
||||
"""Bulk insert negative lookup entries and commit."""
|
||||
count = await bulk_upsert_neg_entries(db, ips)
|
||||
await db.commit()
|
||||
return count
|
||||
|
||||
|
||||
async def bulk_upsert_entries_and_neg_entries_and_commit(
|
||||
db: aiosqlite.Connection,
|
||||
rows: Sequence[tuple[str, str | None, str | None, str | None, str | None]],
|
||||
ips: list[str],
|
||||
) -> tuple[int, int]:
|
||||
"""Persist positive and negative geo cache rows together, then commit."""
|
||||
positive_count = 0
|
||||
negative_count = 0
|
||||
|
||||
if rows:
|
||||
positive_count = await bulk_upsert_entries(db, rows)
|
||||
if ips:
|
||||
negative_count = await bulk_upsert_neg_entries(db, ips)
|
||||
|
||||
if rows or ips:
|
||||
await db.commit()
|
||||
|
||||
return positive_count, negative_count
|
||||
|
||||
Reference in New Issue
Block a user