Skip to content

Commit

Permalink
Merge pull request #58 from ThreeSixtyGiving/rg/fix-cache-sqlite
Browse files Browse the repository at this point in the history
cache: Switch to apsw for cache SQLite db
  • Loading branch information
R2ZER0 authored May 23, 2024
2 parents 02f37d3 + 2a02bee commit ca95e7b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
11 changes: 4 additions & 7 deletions getter/cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import shutil
import sqlite3
import apsw
import hashlib

DATABASE_NAME = "cache_datagetter.db"
Expand All @@ -12,15 +12,14 @@ class DatagetterCacheError(Exception):

def setup_database():
try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()
cur.execute(
"""CREATE TABLE IF NOT EXISTS cache
(original_file_name TEXT NOT NULL UNIQUE,
hash TEXT NOT NULL UNIQUE,
json_file TEXT NOT NULL UNIQUE);"""
)
con.commit()
con.close()
except Exception as e:
raise DatagetterCacheError(e)
Expand Down Expand Up @@ -52,7 +51,7 @@ def hash_file(original_file_path):

def get_file(file_hash_str):
try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()
cur.execute("SELECT json_file FROM cache WHERE hash = ?", (file_hash_str,))
row = cur.fetchone()
Expand All @@ -76,7 +75,7 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type):
# Todo clean up cache functionality for orphaned files or to reset the cache

try:
con = sqlite3.connect(DATABASE_NAME)
con = apsw.Connection(DATABASE_NAME)
cur = con.cursor()

cur.execute(
Expand All @@ -96,8 +95,6 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type):
),
)

con.commit()

shutil.copy(json_file_name, CACHE_DIR)
except Exception as e:
raise DatagetterCacheError(e)
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ jsonschema
strict-rfc3339
rfc3987
requests[socks]
apsw>=3.35,<4

# Compatibility with CoVE
attrs>=20.3.0
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pip-compile
#
apsw==3.45.3.0
# via -r requirements.in
attrs==21.2.0
# via
# -r requirements.in
Expand Down

0 comments on commit ca95e7b

Please sign in to comment.