From ece8dd7f6026ea9cafbeaa5f4a98dec825fd81ee Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sat, 27 Feb 2021 13:29:46 -0600 Subject: [PATCH] use localcache for multidb --- nullboard.html | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/nullboard.html b/nullboard.html index fdc2eec..58e89b5 100644 --- a/nullboard.html +++ b/nullboard.html @@ -1284,7 +1284,33 @@ this.databases.push(arguments[i]); } this.deltas = new Deltas(this.primary()); + this.cachedb = new NamespacedDatabase("cache", new Local()), this.scheduleSync(); + this.syncCacheWithPrimary(); + } + + syncCacheWithPrimary() + { + this.syncAWithB(this.cache(), this.primary()); + } + + syncReplicasWithCache() + { + this.replicas().slice(1).forEach((replica) => { + this.syncAWithB(replica, this.cache()); + }); + } + + syncAWithB(a, b) + { + b.forEach((k) => { + a.set(k, b.get(k)); + }); + } + + cache() + { + return this.cachedb; } primary() @@ -1294,15 +1320,17 @@ replicas() { - return this.databases.slice(1); + return [this.cache()].concat(this.databases.slice(1)); } scheduleSync() { - setInterval(() => { this.sync(); }, 15 * 1000); + setInterval(() => { this.syncDeltas(); }, 10 * 1000); + setInterval(() => { this.syncCacheWithPrimary(); }, 120 * 1000); + setInterval(() => { this.syncReplicasWithCache(); }, 123 * 1000); } - sync() + syncDeltas() { var n = this.pendingSyncCount(); try @@ -1338,7 +1366,7 @@ replicate(key) { - var value = this.primary().get(key); + var value = this.cache().get(key); this.replicas().forEach((database) => { if (value) database.set(key, value); @@ -1349,7 +1377,7 @@ get(key) { - return this.primary().get(key); + return this.cache().get(key); } set(key, value) @@ -1388,7 +1416,7 @@ forEach(foo) { - this.databases[0].forEach((key) => { + this.cache().forEach((key) => { foo(key); }); } @@ -1438,7 +1466,6 @@ var k = this.deprefix(key); if (k) { - console.log("parsed key", k); foo(k); } });