From b5f0d7c4fda4028db0fe4d837a6592230ee2704f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 24 Jun 2022 14:28:45 +0100 Subject: [PATCH] Adding case to handle multi-DB setups, to confirm store same doc ID to different databases and they run in different cache keys. --- .../src/cache/tests/writethrough.spec.js | 14 +++++++- .../backend-core/src/cache/writethrough.ts | 34 +++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/packages/backend-core/src/cache/tests/writethrough.spec.js b/packages/backend-core/src/cache/tests/writethrough.spec.js index eafa002b3e..68db24b325 100644 --- a/packages/backend-core/src/cache/tests/writethrough.spec.js +++ b/packages/backend-core/src/cache/tests/writethrough.spec.js @@ -9,7 +9,8 @@ tk.freeze(START_DATE) const DELAY = 5000 const db = dangerousGetDB("test") -const writethrough = new Writethrough(db, DELAY) +const db2 = dangerousGetDB("test2") +const writethrough = new Writethrough(db, DELAY), writethrough2 = new Writethrough(db2, DELAY) describe("writethrough", () => { describe("put", () => { @@ -43,5 +44,16 @@ describe("writethrough", () => { expect(response.value).toBe(3) }) }) + + describe("same doc, different databases (tenancy)", () => { + it("should be able to two different databases", async () => { + const resp1 = await writethrough.put({ _id: "db1", value: "first" }) + const resp2 = await writethrough2.put({ _id: "db1", value: "second" }) + expect(resp1.rev).toBeDefined() + expect(resp2.rev).toBeDefined() + expect((await db.get("db1")).value).toBe("first") + expect((await db2.get("db1")).value).toBe("second") + }) + }) }) diff --git a/packages/backend-core/src/cache/writethrough.ts b/packages/backend-core/src/cache/writethrough.ts index 472c78a72d..b9817d7d93 100644 --- a/packages/backend-core/src/cache/writethrough.ts +++ b/packages/backend-core/src/cache/writethrough.ts @@ -17,6 +17,10 @@ async function getCache() { return CACHE } +function makeCacheKey(db: PouchDB.Database, key: string) { + return db.name + key +} + function makeCacheItem(doc: any, lastWrite: number | null = null): CacheItem { return { doc, lastWrite: lastWrite || Date.now() } } @@ -28,31 +32,39 @@ export async function put( ) { const cache = await getCache() const key = doc._id - let cacheItem: CacheItem | undefined = await cache.get(key) + let cacheItem: CacheItem | undefined = await cache.get(makeCacheKey(db, key)) const updateDb = !cacheItem || cacheItem.lastWrite < Date.now() - writeRateMs let output = doc if (updateDb) { - // doc should contain the _id and _rev - const response = await db.put(doc) - output = { - ...doc, - _id: response.id, - _rev: response.rev, + try { + // doc should contain the _id and _rev + const response = await db.put(doc) + output = { + ...doc, + _id: response.id, + _rev: response.rev, + } + } catch (err: any) { + // ignore 409s, some other high speed write has hit it first, just move straight to caching + if (err.status !== 409) { + throw err + } } } // if we are updating the DB then need to set the lastWrite to now cacheItem = makeCacheItem(output, updateDb ? null : cacheItem?.lastWrite) - await cache.store(key, cacheItem) + await cache.store(makeCacheKey(db, key), cacheItem) return { ok: true, id: output._id, rev: output._rev } } export async function get(db: PouchDB.Database, id: string): Promise { const cache = await getCache() - let cacheItem: CacheItem = await cache.get(id) + const cacheKey = makeCacheKey(db, id) + let cacheItem: CacheItem = await cache.get(cacheKey) if (!cacheItem) { const doc = await db.get(id) cacheItem = makeCacheItem(doc) - await cache.store(id, cacheItem) + await cache.store(cacheKey, cacheItem) } return cacheItem.doc } @@ -69,7 +81,7 @@ export async function remove( const id = typeof docOrId === "string" ? docOrId : docOrId._id rev = typeof docOrId === "string" ? rev : docOrId._rev try { - await cache.delete(id) + await cache.delete(makeCacheKey(db, id)) } finally { await db.remove(id, rev) }