From 4c1c237fc31817c429e6502b810c7fd67ebaa87d Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 18 Oct 2024 01:55:47 +0800 Subject: [PATCH] Cache SQLite statement --- Build/lib/cache-filesystem.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/Build/lib/cache-filesystem.ts b/Build/lib/cache-filesystem.ts index 5f163354..4750b204 100644 --- a/Build/lib/cache-filesystem.ts +++ b/Build/lib/cache-filesystem.ts @@ -1,5 +1,5 @@ import createDb from 'better-sqlite3'; -import type { Database } from 'better-sqlite3'; +import type { Database, Statement } from 'better-sqlite3'; import os from 'node:os'; import path from 'node:path'; import { mkdirSync } from 'node:fs'; @@ -80,7 +80,7 @@ function ensureETag(headers: IncomingHttpHeaders | Headers) { } export class Cache { - db: Database; + private db: Database; /** Time before deletion */ tbd = 60 * 1000; /** SQLite file path */ @@ -89,6 +89,13 @@ export class Cache { tableName: string; type: S extends string ? 'string' : 'buffer'; + private statement: { + updateTtl: Statement<[number, string]>, + del: Statement<[string]>, + insert: Statement<[unknown]>, + get: Statement<[string], { ttl: number, value: S }> + }; + constructor({ cachePath = path.join(os.tmpdir() || '/tmp', 'hdc'), tbd, @@ -118,6 +125,14 @@ export class Cache { db.prepare(`CREATE TABLE IF NOT EXISTS ${this.tableName} (key TEXT PRIMARY KEY, value ${this.type === 'string' ? 'TEXT' : 'BLOB'}, ttl REAL NOT NULL);`).run(); db.prepare(`CREATE INDEX IF NOT EXISTS cache_ttl ON ${this.tableName} (ttl);`).run(); + /** cache stmt */ + this.statement = { + updateTtl: db.prepare(`UPDATE ${this.tableName} SET ttl = ? WHERE key = ?;`), + del: db.prepare(`DELETE FROM ${this.tableName} WHERE key = ?`), + insert: db.prepare(`INSERT INTO ${this.tableName} (key, value, ttl) VALUES ($key, $value, $valid) ON CONFLICT(key) DO UPDATE SET value = $value, ttl = $valid`), + get: db.prepare(`SELECT ttl, value FROM ${this.tableName} WHERE key = ? LIMIT 1`) + } as const; + const date = new Date(); // perform purge on startup @@ -142,13 +157,9 @@ export class Cache { } set(key: string, value: string, ttl = 60 * 1000): void { - const insert = this.db.prepare( - `INSERT INTO ${this.tableName} (key, value, ttl) VALUES ($key, $value, $valid) ON CONFLICT(key) DO UPDATE SET value = $value, ttl = $valid` - ); - const valid = Date.now() + ttl; - insert.run({ + this.statement.insert.run({ $key: key, key, $value: value, @@ -159,9 +170,7 @@ export class Cache { } get(key: string): S | null { - const rv = this.db.prepare( - `SELECT ttl, value FROM ${this.tableName} WHERE key = ? LIMIT 1` - ).get(key); + const rv = this.statement.get.get(key); if (!rv) return null; @@ -179,11 +188,11 @@ export class Cache { } updateTtl(key: string, ttl: number): void { - this.db.prepare(`UPDATE ${this.tableName} SET ttl = ? WHERE key = ?;`).run(Date.now() + ttl, key); + this.statement.updateTtl.run(Date.now() + ttl, key); } del(key: string): void { - this.db.prepare(`DELETE FROM ${this.tableName} WHERE key = ?`).run(key); + this.statement.del.run(key); } async applyWithHttp304(