Skip to content

Commit dc32f5c

Browse files
authored
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.
1 parent 889116f commit dc32f5c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 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 = true, cachettl = 1800) {
1616

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

25+
if (typeof usecache != "boolean") {
26+
throw new Error('Only boolean values allowed in 3 use cache');
27+
}
28+
29+
if (typeof cachettl != "number") {
30+
throw new Error('Only numeric values allowed in 4 cache ttl');
31+
}
32+
2533
type = type.toUpperCase();
34+
domain = domain.toLowerCase();
35+
36+
if (usecache == true) {
37+
// Cache key and hash
38+
cacheKey = domain + "@" + type;
39+
cacheHash = Utilities.base64Encode(cacheKey);
40+
cacheBinKey = "nslookup-result-" + cacheHash;
41+
42+
var cache = CacheService.getScriptCache();
43+
var cachedResult = cache.get(cacheBinKey);
44+
if (cachedResult != null) {
45+
return cachedResult;
46+
}
47+
}
2648

2749
var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type);
28-
2950
var options = {
3051
muteHttpExceptions: true,
3152
headers: {
@@ -68,6 +89,10 @@ function NSLookup(type, domain) {
6889

6990
var outputString = outputData.join(',');
7091

92+
if (usecache == true) {
93+
cache.put(cacheBinKey, outputString, cachettl);
94+
}
95+
7196
return outputString;
7297
}
7398
```
@@ -76,6 +101,7 @@ function NSLookup(type, domain) {
76101

77102
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`.
78103

104+
The function by default cache the returned DNS record value for 1800 seconds, to limit the number of DNS lookups and speed up the results especially in larger Google Sheets. Both the cache usage and the cache TTL can be controlled in argument 3 and 4.
79105

80106
<Details header="Supported DNS record types">
81107

0 commit comments

Comments
 (0)