Skip to content

Commit 4807e3c

Browse files
author
James Brundage
committed
feat: JSON-LD caching ( Fixes #10 )
1 parent b072ade commit 4807e3c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Commands/Get-JsonLD.ps1

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ function Get-JsonLD {
2222
# The URL that may contain JSON-LD data
2323
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
2424
[Uri]
25-
$Url
25+
$Url,
26+
27+
# If set, will force the request to be made even if the URL has already been cached.
28+
[switch]
29+
$Force
2630
)
2731

2832
begin {
33+
# Create a pattern to match the JSON-LD script tag
2934
$linkedDataRegex = [Regex]::new(@'
3035
(?<HTML_LinkedData>
3136
<script # Match <script tag
@@ -39,10 +44,21 @@ application/ld\+json # The type that indicates linked d
3944
(?<JsonContent>(?:.|\s){0,}?(?=\z|</script>)) # Anything until the end tag is JSONContent
4045
)
4146
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')
47+
48+
# Initialize the cache for JSON-LD requests
49+
if (-not $script:JsonLDRequestCache) {
50+
$script:JsonLDRequestCache = [Ordered]@{}
51+
}
4252
}
4353

4454
process {
45-
$restResponse = Invoke-RestMethod -Uri $Url
55+
$restResponse =
56+
if ($Force -or -not $script:JsonLDRequestCache[$url]) {
57+
$script:JsonLDRequestCache[$url] = Invoke-RestMethod -Uri $Url
58+
$script:JsonLDRequestCache[$url]
59+
} else {
60+
$script:JsonLDRequestCache[$url]
61+
}
4662
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
4763
foreach ($jsonObject in
4864
$match.Groups['JsonContent'].Value |

0 commit comments

Comments
 (0)