⚡️ use transactions for autocomplete cache

This commit is contained in:
Nate Sesti 2024-06-13 12:56:48 -07:00
parent 582f85b87b
commit cd3d95b7eb
3 changed files with 54 additions and 24 deletions

View file

@ -1,3 +1,4 @@
import { Mutex } from "async-mutex";
import { open } from "sqlite";
import sqlite3 from "sqlite3";
import { DatabaseConnection } from "../indexing/refreshIndex.js";
@ -5,6 +6,7 @@ import { getTabAutocompleteCacheSqlitePath } from "../util/paths.js";
export class AutocompleteLruCache {
private static capacity = 1000;
private mutex = new Mutex();
db: DatabaseConnection;
@ -55,33 +57,51 @@ export class AutocompleteLruCache {
}
async put(prefix: string, completion: string) {
const result = await this.db.get(
"SELECT key FROM cache WHERE key = ?",
prefix,
);
const release = await this.mutex.acquire();
try {
await this.db.run("BEGIN TRANSACTION");
if (result) {
await this.db.run(
"UPDATE cache SET value = ?, timestamp = ? WHERE key = ?",
completion,
Date.now(),
prefix,
);
} else {
const count = await this.db.get("SELECT COUNT(*) as count FROM cache");
if (count.count >= AutocompleteLruCache.capacity) {
await this.db.run(
"DELETE FROM cache WHERE key = (SELECT key FROM cache ORDER BY timestamp ASC LIMIT 1)",
try {
const result = await this.db.get(
"SELECT key FROM cache WHERE key = ?",
prefix,
);
}
await this.db.run(
"INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)",
prefix,
completion,
Date.now(),
);
if (result) {
await this.db.run(
"UPDATE cache SET value = ?, timestamp = ? WHERE key = ?",
completion,
Date.now(),
prefix,
);
} else {
const count = await this.db.get(
"SELECT COUNT(*) as count FROM cache",
);
if (count.count >= AutocompleteLruCache.capacity) {
await this.db.run(
"DELETE FROM cache WHERE key = (SELECT key FROM cache ORDER BY timestamp ASC LIMIT 1)",
);
}
await this.db.run(
"INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)",
prefix,
completion,
Date.now(),
);
}
await this.db.run("COMMIT");
} catch (error) {
await this.db.run("ROLLBACK");
throw error;
}
} catch (e) {
console.error("Error creating transaction: ", e);
} finally {
release();
}
}
}

View file

@ -19,6 +19,7 @@
"@typescript-eslint/parser": "^7.8.0",
"@xenova/transformers": "2.14.0",
"adf-to-md": "^1.1.0",
"async-mutex": "^0.5.0",
"axios": "^1.6.7",
"cheerio": "^1.0.0-rc.12",
"commander": "^12.0.0",
@ -5389,6 +5390,14 @@
"node": ">=0.8"
}
},
"node_modules/async-mutex": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz",
"integrity": "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==",
"dependencies": {
"tslib": "^2.4.0"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",

View file

@ -39,6 +39,7 @@
"@typescript-eslint/parser": "^7.8.0",
"@xenova/transformers": "2.14.0",
"adf-to-md": "^1.1.0",
"async-mutex": "^0.5.0",
"axios": "^1.6.7",
"cheerio": "^1.0.0-rc.12",
"commander": "^12.0.0",