add im memory cache for dns ids

This commit is contained in:
hzrd149
2023-03-02 19:46:23 -06:00
parent 06e66c1b9d
commit 9221309831
3 changed files with 23 additions and 8 deletions

View File

@@ -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",

View File

@@ -59,21 +59,36 @@ async function fetchIdentity(address: string) {
return getIdentityFromJson(name, domain, json);
}
const inMemoryCache = new Map<string, DnsIdentity>();
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);

View File

@@ -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"