You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx
+28-2Lines changed: 28 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ import { Details } from "~/components"
12
12
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:
@@ -22,10 +22,31 @@ function NSLookup(type, domain) {
22
22
thrownewError('Missing parameter 2 domain name');
23
23
}
24
24
25
+
if (typeof usecache !="boolean") {
26
+
thrownewError('Only boolean values allowed in 3 use cache');
27
+
}
28
+
29
+
if (typeof cachettl !="number") {
30
+
thrownewError('Only numeric values allowed in 4 cache ttl');
31
+
}
32
+
25
33
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
+
}
26
48
27
49
var url ='https://cloudflare-dns.com/dns-query?name='+encodeURIComponent(domain) +'&type='+encodeURIComponent(type);
28
-
29
50
var options = {
30
51
muteHttpExceptions:true,
31
52
headers: {
@@ -68,6 +89,10 @@ function NSLookup(type, domain) {
68
89
69
90
var outputString =outputData.join(',');
70
91
92
+
if (usecache ==true) {
93
+
cache.put(cacheBinKey, outputString, cachettl);
94
+
}
95
+
71
96
return outputString;
72
97
}
73
98
```
@@ -76,6 +101,7 @@ function NSLookup(type, domain) {
76
101
77
102
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`.
78
103
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.
0 commit comments