Modules/AzureDevOpsDsc.Common/Api/Functions/Private/Cache/Get-CacheItem.ps1
|
<#
.SYNOPSIS Get a cache item from the cache. .DESCRIPTION Get a cache item from the cache. .PARAMETER Key The key of the cache item to get. .EXAMPLE Get-CacheItem -Key 'MyKey' .NOTES This function is private and should not be used directly. #> function Get-CacheItem { [CmdletBinding()] [OutputType([CacheItem])] param ( [Parameter(Mandatory = $true)] [string] $Key, [Parameter(Mandatory = $true)] [ValidateScript({$_ -in (Get-AzDoCacheObjects)})] [string] $Type, [Parameter()] [scriptblock] $Filter ) try { [System.Collections.Generic.List[CacheItem]]$cache = Get-CacheObject -CacheType $Type # A dictionary lookup rather than a scan of the whole cache on every read. $index = Get-CacheKeyIndex -Type $Type -Cache $cache if ($null -ne $index) { $found = $null $cacheItem = if ($index.TryGetValue($Key, [ref]$found)) { @($found) } else { $null } } else { # The cache holds a duplicate key, so it cannot be indexed. Scan it instead. $cacheItem = $cache.Where({$_.Key -eq $Key}) } } catch { $cacheItem = $null Write-Verbose $_ } if ($null -eq $cacheItem) { return $null } if ($Filter -ne $null) { $cacheItem = $cacheItem | Where-Object $Filter } return $cacheItem.Value } |