From dc32f5c46e4faa7fd98bdbb30cef023b9ff88ba7 Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Thu, 3 Oct 2024 13:48:00 +0200 Subject: [PATCH 1/7] 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. --- .../dns-in-google-sheets.mdx | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index 71e6c516ad1401a..c64bf59be500169 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -12,7 +12,7 @@ import { Details } from "~/components" 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: ```js -function NSLookup(type, domain) { +function NSLookup(type, domain, usecache = true, cachettl = 1800) { if (typeof type == 'undefined') { throw new Error('Missing parameter 1 dns type'); @@ -22,10 +22,31 @@ function NSLookup(type, domain) { throw new Error('Missing parameter 2 domain name'); } + if (typeof usecache != "boolean") { + throw new Error('Only boolean values allowed in 3 use cache'); + } + + if (typeof cachettl != "number") { + throw new Error('Only numeric values allowed in 4 cache ttl'); + } + type = type.toUpperCase(); + domain = domain.toLowerCase(); + + if (usecache == true) { + // Cache key and hash + cacheKey = domain + "@" + type; + cacheHash = Utilities.base64Encode(cacheKey); + cacheBinKey = "nslookup-result-" + cacheHash; + + var cache = CacheService.getScriptCache(); + var cachedResult = cache.get(cacheBinKey); + if (cachedResult != null) { + return cachedResult; + } + } var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type); - var options = { muteHttpExceptions: true, headers: { @@ -68,6 +89,10 @@ function NSLookup(type, domain) { var outputString = outputData.join(','); + if (usecache == true) { + cache.put(cacheBinKey, outputString, cachettl); + } + return outputString; } ``` @@ -76,6 +101,7 @@ function NSLookup(type, domain) { 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`. +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.
From 9711bc07a6839666ac4599500add3d242e6c5c78 Mon Sep 17 00:00:00 2001 From: Rebecca Tamachiro Date: Mon, 21 Oct 2024 18:20:14 +0100 Subject: [PATCH 2/7] Process suggested changes for Answer TTL and usecache disabled by default --- .../dns-in-google-sheets.mdx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index c64bf59be500169..6216ae5f23fc54c 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -12,7 +12,7 @@ import { Details } from "~/components" 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: ```js -function NSLookup(type, domain, usecache = true, cachettl = 1800) { +function NSLookup(type, domain, usecache = false, cachettl = 1800) { if (typeof type == 'undefined') { throw new Error('Missing parameter 1 dns type'); @@ -33,7 +33,7 @@ function NSLookup(type, domain, usecache = true, cachettl = 1800) { type = type.toUpperCase(); domain = domain.toLowerCase(); - if (usecache == true) { + if (usecache) { // Cache key and hash cacheKey = domain + "@" + type; cacheHash = Utilities.base64Encode(cacheKey); @@ -89,10 +89,15 @@ function NSLookup(type, domain, usecache = true, cachettl = 1800) { var outputString = outputData.join(','); - if (usecache == true) { + if (usecache) { cache.put(cacheBinKey, outputString, cachettl); } - + + for (var i in response.Answer) { + outputData.push(response.Answer[i].data); + minCacheTTL = Math.min(minCacheTTL, response.Answer[i].TTL); + } + return outputString; } ``` @@ -101,7 +106,7 @@ function NSLookup(type, domain, usecache = true, cachettl = 1800) { 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`. -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. +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 for 1800 seconds. Both the cache usage and the cache TTL can be controlled in arguments 3 and 4, respectively.
From 6e8fdcda6572940ffe0c613062948b62ef5ebbec Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Mon, 21 Oct 2024 21:49:54 +0200 Subject: [PATCH 3/7] Change how minCacheTTL is set - Change minCacheTTL from Math.min to Math.max - Change default value of cachettl from 1800 to 30. --- .../dns-in-google-sheets.mdx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index 6216ae5f23fc54c..42d9a9b878ce768 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -12,7 +12,7 @@ import { Details } from "~/components" 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: ```js -function NSLookup(type, domain, usecache = false, cachettl = 1800) { +function NSLookup(type, domain, usecache = false, cachettl = 30) { if (typeof type == 'undefined') { throw new Error('Missing parameter 1 dns type'); @@ -82,20 +82,17 @@ function NSLookup(type, domain, usecache = false, cachettl = 1800) { } var outputData = []; + var minCacheTTL = cachettl; for (var i in response.Answer) { outputData.push(response.Answer[i].data); + minCacheTTL = Math.max(minCacheTTL, response.Answer[i].TTL); } var outputString = outputData.join(','); if (usecache) { - cache.put(cacheBinKey, outputString, cachettl); - } - - for (var i in response.Answer) { - outputData.push(response.Answer[i].data); - minCacheTTL = Math.min(minCacheTTL, response.Answer[i].TTL); + cache.put(cacheBinKey, outputString, minCacheTTL); } return outputString; From 06459cee3a41f89f4eef997590c30315e9cc872f Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Mon, 21 Oct 2024 21:52:33 +0200 Subject: [PATCH 4/7] Minor code style - change tab to spaces. --- .../1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index 42d9a9b878ce768..df02454ac5b7872 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -82,7 +82,7 @@ function NSLookup(type, domain, usecache = false, cachettl = 30) { } var outputData = []; - var minCacheTTL = cachettl; + var minCacheTTL = cachettl; for (var i in response.Answer) { outputData.push(response.Answer[i].data); From 4eb8671f94840cbe0aa646ffb96c67b6a42c08d1 Mon Sep 17 00:00:00 2001 From: Jens Beltofte Date: Wed, 23 Oct 2024 21:38:54 +0200 Subject: [PATCH 5/7] Apply patch + rename usecache to useCache Applying changes from https://github.com/cloudflare/cloudflare-docs/pull/17298#issuecomment-2433142066 + rename usecache to useCache to make all variables/constants follow lowerCamelCase. --- .../dns-in-google-sheets.mdx | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index df02454ac5b7872..a23651b067c74e1 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -12,7 +12,7 @@ import { Details } from "~/components" 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: ```js -function NSLookup(type, domain, usecache = false, cachettl = 30) { +function NSLookup(type, domain, useCache = false, minCacheTTL = 30) { if (typeof type == 'undefined') { throw new Error('Missing parameter 1 dns type'); @@ -22,47 +22,48 @@ function NSLookup(type, domain, usecache = false, cachettl = 30) { throw new Error('Missing parameter 2 domain name'); } - if (typeof usecache != "boolean") { + if (typeof useCache != "boolean") { throw new Error('Only boolean values allowed in 3 use cache'); } - if (typeof cachettl != "number") { - throw new Error('Only numeric values allowed in 4 cache ttl'); + if (typeof minCacheTTL != "number") { + throw new Error('Only numeric values allowed in 4 min cache ttl'); } type = type.toUpperCase(); domain = domain.toLowerCase(); - if (usecache) { + let cache = null; + if (useCache) { // Cache key and hash cacheKey = domain + "@" + type; cacheHash = Utilities.base64Encode(cacheKey); cacheBinKey = "nslookup-result-" + cacheHash; - var cache = CacheService.getScriptCache(); - var cachedResult = cache.get(cacheBinKey); + cache = CacheService.getScriptCache(); + const cachedResult = cache.get(cacheBinKey); if (cachedResult != null) { return cachedResult; } } - var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type); - var options = { + const url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type); + const options = { muteHttpExceptions: true, headers: { accept: "application/dns-json" } }; - var result = UrlFetchApp.fetch(url, options); - var rc = result.getResponseCode(); - var resultText = result.getContentText(); + const result = UrlFetchApp.fetch(url, options); + const rc = result.getResponseCode(); + const resultText = result.getContentText(); if (rc !== 200) { throw new Error(rc); } - var errors = [ + const errors = [ { name: "NoError", description: "No Error"}, // 0 { name: "FormErr", description: "Format Error"}, // 1 { name: "ServFail", description: "Server Failure"}, // 2 @@ -75,24 +76,25 @@ function NSLookup(type, domain, usecache = false, cachettl = 30) { { name: "NotAuth", description: "Not Authorized"} // 9 ]; - var response = JSON.parse(resultText); + const response = JSON.parse(resultText); if (response.Status !== 0) { return errors[response.Status].name; } - var outputData = []; - var minCacheTTL = cachettl; + const outputData = []; + let cacheTTL = 0; for (var i in response.Answer) { outputData.push(response.Answer[i].data); - minCacheTTL = Math.max(minCacheTTL, response.Answer[i].TTL); + const ttl = response.Answer[i].TTL; + cacheTTL = Math.min(cacheTTL || ttl, ttl); } - var outputString = outputData.join(','); + const outputString = outputData.join(','); - if (usecache) { - cache.put(cacheBinKey, outputString, minCacheTTL); + if (useCache) { + cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL)); } return outputString; From 7f6d534df299f2675756915e230b26b4d3132339 Mon Sep 17 00:00:00 2001 From: Rebecca Tamachiro Date: Thu, 24 Oct 2024 09:53:29 +0100 Subject: [PATCH 6/7] Remove outdated cache value from descriptive paragraph --- .../1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index a23651b067c74e1..128bc4a19544c8a 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -105,7 +105,7 @@ function NSLookup(type, domain, useCache = false, minCacheTTL = 30) { 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`. -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 for 1800 seconds. Both the cache usage and the cache TTL can be controlled in arguments 3 and 4, respectively. +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.
From 887401107d7425c94d9c72fb7239a5e53595a0dc Mon Sep 17 00:00:00 2001 From: Rebecca Tamachiro Date: Thu, 24 Oct 2024 10:27:42 +0100 Subject: [PATCH 7/7] Replace one remaining var by const --- .../1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx index 128bc4a19544c8a..912e75eb5bb4da3 100644 --- a/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx +++ b/src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx @@ -85,7 +85,7 @@ function NSLookup(type, domain, useCache = false, minCacheTTL = 30) { const outputData = []; let cacheTTL = 0; - for (var i in response.Answer) { + for (const i in response.Answer) { outputData.push(response.Answer[i].data); const ttl = response.Answer[i].TTL; cacheTTL = Math.min(cacheTTL || ttl, ttl);