Skip to content

Commit 9561776

Browse files
Merge pull request #11 from PowerShellWeb/Json-LD-Caching
JSON-LD 0.1 : JSON-LD Caching
2 parents b072ade + 422721f commit 9561776

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
> Like It? [Star It](https://github.com/PowerShellWeb/JSON-LD)
2+
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
3+
4+
## JSON-LD 0.1
5+
6+
Caching JSON-LD requests
7+
8+
## JSON-LD 0.0.1
9+
10+
Get Linked Data from any page
11+
12+
* Initial Release of JSON-LD Module (#1)
13+
* `Get-JsonLD` gets linked data (#2)
14+
* `Get-JsonLD` is aliased to `jsonLD` and `json-ld`

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 |

JSON-LD.psd1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@{
22
RootModule = 'JSON-LD.psm1'
3-
ModuleVersion = '0.0.1'
3+
ModuleVersion = '0.1'
44
GUID = '4e65477c-012c-4077-87c7-3e07964636ce'
5-
Author = 'JamesBrundage'
5+
Author = 'James Brundage'
66
CompanyName = 'Start-Automating'
77
Copyright = '(c) 2025 Start-Automating.'
88
Description = 'Get JSON Linked Data with PowerShell'
99
FunctionsToExport = 'Get-JsonLD'
10-
AliasesToExport = 'jsonLD', 'json-ld'
10+
AliasesToExport = 'jsonLD', 'json-ld'
1111
PrivateData = @{
1212
PSData = @{
1313
# Tags applied to this module. These help with module discovery in online galleries.
@@ -20,6 +20,10 @@
2020
> Like It? [Star It](https://github.com/PowerShellWeb/JSON-LD)
2121
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
2222
23+
## JSON-LD 0.1
24+
25+
Caching JSON-LD requests
26+
2327
Get Linked Data from any page
2428
2529
## JSON-LD 0.0.1

0 commit comments

Comments
 (0)