From aade1e3c7c3d32438c5f161f2bb7f52ee0e4686c Mon Sep 17 00:00:00 2001 From: ximu3 Date: Sun, 15 Dec 2024 16:41:05 +0800 Subject: [PATCH] feat: Adding a caching mechanism to the database --- src/main/database/common.ts | 46 +++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main/database/common.ts b/src/main/database/common.ts index 59bd07b..71ff34b 100644 --- a/src/main/database/common.ts +++ b/src/main/database/common.ts @@ -6,6 +6,10 @@ type JsonObject = { [key: string]: JsonObject | any } class DBManager { private static instances: { [key: string]: Low } = {} private static queues: { [key: string]: Promise } = {} + // Add memory cache + private static cache: { [key: string]: JsonObject } = {} + // Add cache status tags + private static cacheInitialized: { [key: string]: boolean } = {} private static getInstance(filePath: string): Low { if (!this.instances[filePath]) { @@ -15,6 +19,15 @@ class DBManager { return this.instances[filePath] } + private static async initializeCache(filePath: string): Promise { + if (!this.cacheInitialized[filePath]) { + const db = this.getInstance(filePath) + await db.read() + this.cache[filePath] = JSON.parse(JSON.stringify(db.data || {})) + this.cacheInitialized[filePath] = true + } + } + private static async executeOperation( filePath: string, operation: () => Promise @@ -31,22 +44,30 @@ class DBManager { static async setValue(filePath: string, path: string[], value: any): Promise { return this.executeOperation(filePath, async () => { + // Make sure the cache is initialized + await this.initializeCache(filePath) + const db = this.getInstance(filePath) - await db.read() if (path[0] === '#all') { db.data = value + this.cache[filePath] = JSON.parse(JSON.stringify(value)) } else { let current = db.data + let currentCache = this.cache[filePath] + for (let i = 0; i < path.length; i++) { const key = path[i] if (i === path.length - 1) { current[key] = value + currentCache[key] = JSON.parse(JSON.stringify(value)) } else { if (!(key in current)) { current[key] = {} + currentCache[key] = {} } current = current[key] + currentCache = currentCache[key] } } } @@ -57,19 +78,22 @@ class DBManager { static async getValue(filePath: string, path: string[], defaultValue: T): Promise { return this.executeOperation(filePath, async () => { - const db = this.getInstance(filePath) - await db.read() + // Make sure the cache is initialized + await this.initializeCache(filePath) if (path[0] === '#all') { - return db.data as T + return this.cache[filePath] as T } - let current = db.data + let current = this.cache[filePath] for (let i = 0; i < path.length; i++) { const key = path[i] if (i === path.length - 1) { if (!(key in current)) { + // If the value is not in the cache, update the cache and the file + const db = this.getInstance(filePath) current[key] = defaultValue + db.data = JSON.parse(JSON.stringify(this.cache[filePath])) await db.write() } return current[key] as T @@ -84,7 +108,19 @@ class DBManager { return defaultValue }) } + + // Add a method to clear the cache (which can be called when needed) + static clearCache(filePath?: string): void { + if (filePath) { + delete this.cache[filePath] + delete this.cacheInitialized[filePath] + } else { + this.cache = {} + this.cacheInitialized = {} + } + } } export const setValue = DBManager.setValue.bind(DBManager) export const getValue = DBManager.getValue.bind(DBManager) +export const clearCache = DBManager.clearCache.bind(DBManager)