From 476d21e9f07f648784e92e6e7ec4ae37910e2449 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 5 Jun 2018 14:44:13 -0400 Subject: [PATCH] alloe cache on lowdb --- src/services/lowdbStorage.service.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts index 547187d5d9..90943b34ff 100644 --- a/src/services/lowdbStorage.service.ts +++ b/src/services/lowdbStorage.service.ts @@ -12,7 +12,7 @@ export class LowdbStorageService implements StorageService { private db: lowdb.LowdbSync; private defaults: any; - constructor(defaults?: any, dir?: string) { + constructor(defaults?: any, dir?: string, private allowCache = false) { this.defaults = defaults; let adapter: lowdb.AdapterSync; @@ -22,20 +22,24 @@ export class LowdbStorageService implements StorageService { } const p = path.join(dir, 'data.json'); adapter = new FileSync(p); - } else if (Utils.isBrowser && !Utils.isNode) { - // local storage adapter for web } this.db = lowdb(adapter); } init() { if (this.defaults != null) { - this.db.read().defaults(this.defaults).write(); + if (!this.allowCache) { + this.db.read(); + } + this.db.defaults(this.defaults).write(); } } get(key: string): Promise { - const val = this.db.read().get(key).value(); + if (!this.allowCache) { + this.db.read(); + } + const val = this.db.get(key).value(); if (val == null) { return Promise.resolve(null); } @@ -43,12 +47,18 @@ export class LowdbStorageService implements StorageService { } save(key: string, obj: any): Promise { - this.db.read().set(key, obj).write(); + if (!this.allowCache) { + this.db.read(); + } + this.db.set(key, obj).write(); return Promise.resolve(); } remove(key: string): Promise { - this.db.read().unset(key).write(); + if (!this.allowCache) { + this.db.read(); + } + this.db.unset(key).write(); return Promise.resolve(); } }