mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-25 19:23:45 +02:00
add im memory cache for dns ids
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
"light-bolt11-decoder": "^2.1.0",
|
"light-bolt11-decoder": "^2.1.0",
|
||||||
"moment": "^2.29.4",
|
"moment": "^2.29.4",
|
||||||
"noble-secp256k1": "^1.2.14",
|
"noble-secp256k1": "^1.2.14",
|
||||||
"nostr-tools": "^1.5.0",
|
"nostr-tools": "^1.7.4",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"react-error-boundary": "^3.1.4",
|
"react-error-boundary": "^3.1.4",
|
||||||
|
@@ -59,21 +59,36 @@ async function fetchIdentity(address: string) {
|
|||||||
return getIdentityFromJson(name, domain, json);
|
return getIdentityFromJson(name, domain, json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const inMemoryCache = new Map<string, DnsIdentity>();
|
||||||
|
|
||||||
async function addToCache(domain: string, json: IdentityJson) {
|
async function addToCache(domain: string, json: IdentityJson) {
|
||||||
const now = moment().unix();
|
const now = moment().unix();
|
||||||
const transaction = db.transaction("dnsIdentifiers", "readwrite");
|
const transaction = db.transaction("dnsIdentifiers", "readwrite");
|
||||||
|
|
||||||
for (const name of Object.keys(json.names)) {
|
for (const name of Object.keys(json.names)) {
|
||||||
const identity = getIdentityFromJson(name, domain, json);
|
const identity = getIdentityFromJson(name, domain, json);
|
||||||
if (identity && transaction.store.put) {
|
if (identity) {
|
||||||
await transaction.store.put({ ...identity, updated: now }, `${name}@${domain}`);
|
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;
|
await transaction.done;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getIdentity(address: string, alwaysFetch = false) {
|
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;
|
if (cached && !alwaysFetch) return cached;
|
||||||
|
|
||||||
return fetchIdentity(address);
|
return fetchIdentity(address);
|
||||||
|
@@ -3483,10 +3483,10 @@ node-releases@^2.0.6:
|
|||||||
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.8.tgz#0f349cdc8fcfa39a92ac0be9bc48b7706292b9ae"
|
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==
|
integrity sha512-dFSmB8fFHEH/s81Xi+Y/15DQY6VHW81nXRj86EMSL3lmuTmK1e+aT4wrFCkTbm+gSwkw4KpX+rT/pMM2c1mF+A==
|
||||||
|
|
||||||
nostr-tools@^1.5.0:
|
nostr-tools@^1.7.4:
|
||||||
version "1.5.0"
|
version "1.7.4"
|
||||||
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.5.0.tgz#b8b8d4d9e122c4acbdfe8b580e82f539f0866b42"
|
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.7.4.tgz#8f14ffde8f6674c3758bb49207a1ee4227a4b9fe"
|
||||||
integrity sha512-9zZdS3OF1hC8Tzpx2ycFLFx0AYSXBkLZ5EaXcPWIdZlQgSkpDrHl/TUYk2E8K7OocK+3VNbo4+7K2N38qdCjsg==
|
integrity sha512-YowDJ+S3UW9KCYPDZfZXXMITrJSMjiCmFOK5HohyKkg+w6EipFUTkFRBPRA2BTLXO/qw8gukKXfL0B7Dv3jtcQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@noble/hashes" "1.0.0"
|
"@noble/hashes" "1.0.0"
|
||||||
"@noble/secp256k1" "^1.7.1"
|
"@noble/secp256k1" "^1.7.1"
|
||||||
|
Reference in New Issue
Block a user