From 746da81725f83080fcc901fd483d5bfcc53ba385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20Wypch=C5=82o?= Date: Thu, 3 Sep 2020 15:08:55 +0200 Subject: [PATCH] cache hns response for 5 minutes (#366) --- packages/handshake-api/package.json | 3 ++- packages/handshake-api/src/index.js | 6 ++++++ yarn.lock | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/handshake-api/package.json b/packages/handshake-api/package.json index 2e6850ce..fbbf3a32 100644 --- a/packages/handshake-api/package.json +++ b/packages/handshake-api/package.json @@ -6,7 +6,8 @@ "dependencies": { "express": "^4.17.1", "express-http-proxy": "^1.6.2", - "hs-client": "^0.0.9" + "hs-client": "^0.0.9", + "node-cache": "^5.1.2" }, "devDependencies": { "prettier": "^2.0.5" diff --git a/packages/handshake-api/src/index.js b/packages/handshake-api/src/index.js index 6c057ca1..fa522387 100644 --- a/packages/handshake-api/src/index.js +++ b/packages/handshake-api/src/index.js @@ -1,6 +1,7 @@ const url = require("url"); const express = require("express"); const proxy = require("express-http-proxy"); +const NodeCache = require("node-cache"); const { NodeClient } = require("hs-client"); const host = process.env.HOSTNAME || "0.0.0.0"; @@ -18,16 +19,21 @@ const clientOptions = { apiKey: hsdApiKey, }; const client = new NodeClient(clientOptions); +const cache = new NodeCache({ stdTTL: 300 }); // cache for 5 minutes // Match both `sia://HASH` and `HASH` links. const startsWithSkylinkRegExp = /^(sia:\/\/)?[a-zA-Z0-9_-]{46}/; const getDomainRecords = async (name) => { + if (cache.has(name)) return cache.get(name); + const response = await client.execute("getnameresource", [name]); const records = response?.records ?? null; console.log(`${name} => ${JSON.stringify(records)}`); + cache.set(name, records); + return records; }; diff --git a/yarn.lock b/yarn.lock index 5c958731..815f171e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3928,6 +3928,11 @@ clone-response@1.0.2, clone-response@^1.0.2: dependencies: mimic-response "^1.0.0" +clone@2.x: + version "2.1.2" + resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f" + integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18= + coa@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/coa/-/coa-2.0.2.tgz#43f6c21151b4ef2bf57187db0d73de229e3e7ec3" @@ -10238,6 +10243,13 @@ node-addon-api@^3.0.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.0.0.tgz#812446a1001a54f71663bed188314bba07e09247" integrity sha512-sSHCgWfJ+Lui/u+0msF3oyCgvdkhxDbkCS6Q8uiJquzOimkJBvX6hl5aSSA7DR1XbMpdM8r7phjcF63sF4rkKg== +node-cache@^5.1.2: + version "5.1.2" + resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d" + integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg== + dependencies: + clone "2.x" + node-emoji@^1.10.0: version "1.10.0" resolved "https://registry.yarnpkg.com/node-emoji/-/node-emoji-1.10.0.tgz#8886abd25d9c7bb61802a658523d1f8d2a89b2da"