Fixing db maintenance

This commit is contained in:
ZhymabekRoman 2024-02-23 14:14:30 +06:00
parent 92b73d3e27
commit aa2c4b3f2f
2 changed files with 10 additions and 7 deletions

View file

@ -26,8 +26,9 @@ class CacheResponse:
return self.data
class SQLiteCacheBackend:
__slots__ = ('connection', 'cursor')
__slots__ = ('connection', 'cursor', 'database')
def __init__(self, database: str):
self.database = database
self.connection = sqlite3.connect(database)
self.connection.enable_load_extension(True) # Enable loading of extensions
self.connection.execute("PRAGMA foreign_keys = ON;") # Need for working with foreign keys in db
@ -98,13 +99,15 @@ class SQLiteCacheBackend:
logger.debug(f"Attempted to delete non-existing key: {key}")
def maintenance(self, time: int = None, blocking_time: int = 0.5):
with self.connection:
connection = sqlite3.connect(self.database)
cursor = connection.cursor()
with connection:
if time is not None:
self.cursor.execute("SELECT zstd_incremental_maintenance(?, ?);", (time, blocking_time))
cursor.execute("SELECT zstd_incremental_maintenance(?, ?);", (time, blocking_time))
else:
self.cursor.execute("SELECT zstd_incremental_maintenance(null, ?);", (blocking_time,))
self.cursor.execute("VACUUM")
self.cursor.execute("ANALYZE")
cursor.execute("SELECT zstd_incremental_maintenance(null, ?);", (blocking_time,))
cursor.execute("VACUUM")
cursor.execute("ANALYZE")
def maintenance_thread(self):
maintenance_thread = threading.Thread(target=self.maintenance, daemon=True)

View file

@ -29,7 +29,7 @@ class TestSQLiteCacheBackend(unittest.TestCase):
return {generate_random_key(5): random.randint(1, 1000) for _ in range(3)}
def generate_data():
for _ in range(300):
for _ in range(12):
yield generate_random_key(), generate_random_value()
for key, value in generate_data():