From 3622960ea118f6a39a2a76537a28779537895b6d Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Tue, 17 Dec 2024 18:28:38 -0800 Subject: [PATCH] feat: Get-GQL -Cache ( Fixes #2, Fixes #31 ) --- Commands/Get-GQL.ps1 | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Commands/Get-GQL.ps1 b/Commands/Get-GQL.ps1 index 6489447..202acc4 100644 --- a/Commands/Get-GQL.ps1 +++ b/Commands/Get-GQL.ps1 @@ -68,7 +68,13 @@ function Get-GQL # * Format the output object any way we want [Alias('Decorate','Decoration','PSTypeNames','TypeName','TypeNames','Type')] [string[]] - $PSTypeName + $PSTypeName, + + # If set, will cache the results of the query. + # This can be useful for queries that would be run frequently, but change infrequently. + [Parameter(ValueFromPipelineByPropertyName)] + [switch] + $Cache ) process { @@ -144,6 +150,17 @@ function Get-GQL } #endregion Check for File or Cached Query + if ($Cache -and -not $script:GraphQLOutputCache) { + $script:GraphQLOutputCache = [Ordered]@{} + } + + if ($script:GraphQLOutputCache.$gqlQuery -and + -not $Parameter.Count + ) { + $script:GraphQLOutputCache.$gqlQuery + continue nextQuery + } + #region Run the Query $invokeSplat.Body = [Ordered]@{query = $gqlQuery} if ($Parameter) { @@ -183,6 +200,9 @@ function Get-GQL $gqlOutput.data.pstypenames.add($pstypename[$goBackwards]) } } + if ($Cache) { + $script:GraphQLOutputCache[$gqlQuery] = $gqlOutput.data + } $gqlOutput.data } elseif ($gqlOutput) { @@ -192,6 +212,9 @@ function Get-GQL $gqlOutput.pstypenames.add($pstypename[$goBackwards]) } } + if ($Cache) { + $script:GraphQLOutputCache[$gqlQuery] = $gqlOutput + } $gqlOutput } #endregion Run the Query