use localcache for multidb

master
Bel LaPointe 2021-02-27 13:29:46 -06:00
parent 61b6bdd3aa
commit ece8dd7f60
1 changed files with 34 additions and 7 deletions

View File

@ -1284,7 +1284,33 @@
this.databases.push(arguments[i]); this.databases.push(arguments[i]);
} }
this.deltas = new Deltas(this.primary()); this.deltas = new Deltas(this.primary());
this.cachedb = new NamespacedDatabase("cache", new Local()),
this.scheduleSync(); 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() primary()
@ -1294,15 +1320,17 @@
replicas() replicas()
{ {
return this.databases.slice(1); return [this.cache()].concat(this.databases.slice(1));
} }
scheduleSync() 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(); var n = this.pendingSyncCount();
try try
@ -1338,7 +1366,7 @@
replicate(key) replicate(key)
{ {
var value = this.primary().get(key); var value = this.cache().get(key);
this.replicas().forEach((database) => { this.replicas().forEach((database) => {
if (value) if (value)
database.set(key, value); database.set(key, value);
@ -1349,7 +1377,7 @@
get(key) get(key)
{ {
return this.primary().get(key); return this.cache().get(key);
} }
set(key, value) set(key, value)
@ -1388,7 +1416,7 @@
forEach(foo) forEach(foo)
{ {
this.databases[0].forEach((key) => { this.cache().forEach((key) => {
foo(key); foo(key);
}); });
} }
@ -1438,7 +1466,6 @@
var k = this.deprefix(key); var k = this.deprefix(key);
if (k) if (k)
{ {
console.log("parsed key", k);
foo(k); foo(k);
} }
}); });