From 92213098313f20e08280e32af4679e2779f3c6c3 Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Thu, 2 Mar 2023 19:46:23 -0600 Subject: [PATCH] add im memory cache for dns ids --- package.json | 2 +- src/services/dns-identity.ts | 21 ++++++++++++++++++--- yarn.lock | 8 ++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c124a0194..5471823e7 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "light-bolt11-decoder": "^2.1.0", "moment": "^2.29.4", "noble-secp256k1": "^1.2.14", - "nostr-tools": "^1.5.0", + "nostr-tools": "^1.7.4", "react": "^18.2.0", "react-dom": "^18.2.0", "react-error-boundary": "^3.1.4", diff --git a/src/services/dns-identity.ts b/src/services/dns-identity.ts index 69e27ca68..db63e167b 100644 --- a/src/services/dns-identity.ts +++ b/src/services/dns-identity.ts @@ -59,21 +59,36 @@ async function fetchIdentity(address: string) { return getIdentityFromJson(name, domain, json); } +const inMemoryCache = new Map(); + async function addToCache(domain: string, json: IdentityJson) { const now = moment().unix(); const transaction = db.transaction("dnsIdentifiers", "readwrite"); for (const name of Object.keys(json.names)) { const identity = getIdentityFromJson(name, domain, json); - if (identity && transaction.store.put) { - await transaction.store.put({ ...identity, updated: now }, `${name}@${domain}`); + if (identity) { + const id = `${name}@${domain}`; + + // add to memory cache + inMemoryCache.set(id, identity); + + // ad to db cache + if (transaction.store.put) { + await transaction.store.put({ ...identity, updated: now }, id); + } } } await transaction.done; } async function getIdentity(address: string, alwaysFetch = false) { - const cached = await db.get("dnsIdentifiers", address); + if (!inMemoryCache.has(address)) { + const fromDb = await db.get("dnsIdentifiers", address); + if (fromDb) inMemoryCache.set(address, fromDb); + } + + const cached = inMemoryCache.get(address); if (cached && !alwaysFetch) return cached; return fetchIdentity(address); diff --git a/yarn.lock b/yarn.lock index f70580002..a16232217 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3483,10 +3483,10 @@ node-releases@^2.0.6: resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.8.tgz#0f349cdc8fcfa39a92ac0be9bc48b7706292b9ae" integrity sha512-dFSmB8fFHEH/s81Xi+Y/15DQY6VHW81nXRj86EMSL3lmuTmK1e+aT4wrFCkTbm+gSwkw4KpX+rT/pMM2c1mF+A== -nostr-tools@^1.5.0: - version "1.5.0" - resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.5.0.tgz#b8b8d4d9e122c4acbdfe8b580e82f539f0866b42" - integrity sha512-9zZdS3OF1hC8Tzpx2ycFLFx0AYSXBkLZ5EaXcPWIdZlQgSkpDrHl/TUYk2E8K7OocK+3VNbo4+7K2N38qdCjsg== +nostr-tools@^1.7.4: + version "1.7.4" + resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.7.4.tgz#8f14ffde8f6674c3758bb49207a1ee4227a4b9fe" + integrity sha512-YowDJ+S3UW9KCYPDZfZXXMITrJSMjiCmFOK5HohyKkg+w6EipFUTkFRBPRA2BTLXO/qw8gukKXfL0B7Dv3jtcQ== dependencies: "@noble/hashes" "1.0.0" "@noble/secp256k1" "^1.7.1"