Skip to content

Commit 99b9cf4

Browse files
[1.1.1.1] Add NSLookup caching to dns-in-google-sheets.mdx (#17298)
* Update dns-in-google-sheets.mdx to include cache support in the NSLookup function. Adding internal caching of the results on the NSLookup function to limit the number of the DNS resolver requests and speed up the results - especially in larger Google Sheets. * Process suggested changes for Answer TTL and usecache disabled by default * Change how minCacheTTL is set - Change minCacheTTL from Math.min to Math.max - Change default value of cachettl from 1800 to 30. * Minor code style - change tab to spaces. * Apply patch + rename usecache to useCache Applying changes from #17298 (comment) + rename usecache to useCache to make all variables/constants follow lowerCamelCase. * Remove outdated cache value from descriptive paragraph * Replace one remaining var by const --------- Co-authored-by: Rebecca Tamachiro <[email protected]>
1 parent 90a7cc3 commit 99b9cf4

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Details } from "~/components"
1212
1.1.1.1 works directly inside Google Sheets. To get started, create a [Google Function](https://developers.google.com/apps-script/guides/sheets/functions) with the following code:
1313

1414
```js
15-
function NSLookup(type, domain) {
15+
function NSLookup(type, domain, useCache = false, minCacheTTL = 30) {
1616

1717
if (typeof type == 'undefined') {
1818
throw new Error('Missing parameter 1 dns type');
@@ -22,26 +22,48 @@ function NSLookup(type, domain) {
2222
throw new Error('Missing parameter 2 domain name');
2323
}
2424

25-
type = type.toUpperCase();
25+
if (typeof useCache != "boolean") {
26+
throw new Error('Only boolean values allowed in 3 use cache');
27+
}
2628

27-
var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type);
29+
if (typeof minCacheTTL != "number") {
30+
throw new Error('Only numeric values allowed in 4 min cache ttl');
31+
}
2832

29-
var options = {
33+
type = type.toUpperCase();
34+
domain = domain.toLowerCase();
35+
36+
let cache = null;
37+
if (useCache) {
38+
// Cache key and hash
39+
cacheKey = domain + "@" + type;
40+
cacheHash = Utilities.base64Encode(cacheKey);
41+
cacheBinKey = "nslookup-result-" + cacheHash;
42+
43+
cache = CacheService.getScriptCache();
44+
const cachedResult = cache.get(cacheBinKey);
45+
if (cachedResult != null) {
46+
return cachedResult;
47+
}
48+
}
49+
50+
const url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type);
51+
const options = {
3052
muteHttpExceptions: true,
3153
headers: {
3254
accept: "application/dns-json"
3355
}
3456
};
3557

36-
var result = UrlFetchApp.fetch(url, options);
37-
var rc = result.getResponseCode();
38-
var resultText = result.getContentText();
58+
const result = UrlFetchApp.fetch(url, options);
59+
const rc = result.getResponseCode();
60+
const resultText = result.getContentText();
3961

4062
if (rc !== 200) {
4163
throw new Error(rc);
4264
}
4365

44-
var errors = [
66+
const errors = [
4567
{ name: "NoError", description: "No Error"}, // 0
4668
{ name: "FormErr", description: "Format Error"}, // 1
4769
{ name: "ServFail", description: "Server Failure"}, // 2
@@ -54,19 +76,26 @@ function NSLookup(type, domain) {
5476
{ name: "NotAuth", description: "Not Authorized"} // 9
5577
];
5678

57-
var response = JSON.parse(resultText);
79+
const response = JSON.parse(resultText);
5880

5981
if (response.Status !== 0) {
6082
return errors[response.Status].name;
6183
}
6284

63-
var outputData = [];
85+
const outputData = [];
86+
let cacheTTL = 0;
6487

65-
for (var i in response.Answer) {
88+
for (const i in response.Answer) {
6689
outputData.push(response.Answer[i].data);
90+
const ttl = response.Answer[i].TTL;
91+
cacheTTL = Math.min(cacheTTL || ttl, ttl);
6792
}
6893

69-
var outputString = outputData.join(',');
94+
const outputString = outputData.join(',');
95+
96+
if (useCache) {
97+
cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL));
98+
}
7099

71100
return outputString;
72101
}
@@ -76,6 +105,7 @@ function NSLookup(type, domain) {
76105

77106
When you feed the function `NSLookup` a record type and a domain, you will get a DNS record value in the cell you called `NSLookup`.
78107

108+
To limit the number of DNS lookups and speed up the results (especially in larger Google Sheets), you can cache the returned DNS record value. Both the cache usage and the cache TTL can be controlled in arguments 3 and 4, respectively.
79109

80110
<Details header="Supported DNS record types">
81111

0 commit comments

Comments
 (0)